diff --git a/src-tauri/src/game/client.rs b/src-tauri/src/game/client.rs index 2c86081e..983021a3 100644 --- a/src-tauri/src/game/client.rs +++ b/src-tauri/src/game/client.rs @@ -73,6 +73,7 @@ pub trait GameClient: Send + Sync { async fn get_all_version_urls() -> PolyResult> where Self: Sized; + async fn get_minecraft_manifest( version: String, ) -> PolyResult diff --git a/src-tauri/src/game/clients/vanilla.rs b/src-tauri/src/game/clients/vanilla.rs index 754e1d3d..279fa305 100644 --- a/src-tauri/src/game/clients/vanilla.rs +++ b/src-tauri/src/game/clients/vanilla.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, error::Error}; +use std::collections::HashMap; use anyhow::anyhow; use async_trait::async_trait; @@ -52,18 +52,18 @@ impl GameClient for VanillaClient { let mut map = HashMap::new(); for version in versions { - let version = version.as_object().ok_or("Invalid version object")?; + let version = version.as_object().ok_or(anyhow!("Invalid version object"))?; let id = version .get("id") - .ok_or("No id object")? + .ok_or(anyhow!("No id object"))? .as_str() - .ok_or("Invalid id object")?; + .ok_or(anyhow!("Invalid id object"))?; let url = version .get("url") - .ok_or("No url object")? + .ok_or(anyhow!("No url object"))? .as_str() - .ok_or("Invalid url object")?; + .ok_or(anyhow!("Invalid url object"))?; map.insert(id.to_string(), url.to_string()); } @@ -73,8 +73,8 @@ impl GameClient for VanillaClient { async fn get_minecraft_manifest( version_url: String, - ) -> Result> { - let request = http::create_client().get(version_url).send().await?; + ) -> PolyResult { + let request = http::create_client()?.get(version_url).send().await?; match request.json::().await { Ok(manifest) => Ok(manifest), @@ -94,12 +94,12 @@ impl GameClient for VanillaClient { &self.get_details().client_type } - async fn setup(&self) -> Result<(), Box> { + async fn setup(&self) -> PolyResult<()> { let libraries_dir = self.get_handle().path().app_config_dir()?.join("libraries"); let versions = Self::get_all_version_urls().await?; let version = versions .get(&self.get_details().version) - .ok_or("Version not found")? + .ok_or(anyhow!("Version not found"))? .to_owned(); let manifest = Self::get_minecraft_manifest(version).await?; @@ -110,7 +110,7 @@ impl GameClient for VanillaClient { Ok(()) } - async fn launch(&self) -> Result<(), Box> { + async fn launch(&self) -> PolyResult<()> { println!("Launching Vanilla client"); tokio::time::sleep(std::time::Duration::from_secs(5)).await; println!("Vanilla client launched"); @@ -122,40 +122,22 @@ impl GameClient for VanillaClient { } pub mod vanilla_impl { - use std::{error::Error, fs, path::PathBuf}; + use std::{fs, path::PathBuf}; - use serde_json::{Map, Value}; - use tauri::AppHandle; + use anyhow::anyhow; - use crate::utils::http::download_file; +use crate::{utils::http::download_file, PolyResult}; - use super::vanilla_manifest::{self, Library}; + use super::vanilla_manifest::Library; pub async fn setup_libraries( libraries: &Vec, libraries_folder: &PathBuf, - ) -> Result, Box> { + ) -> PolyResult> { let mut natives_ret: Vec<&Library> = vec![]; let mut libraries_ret: Vec = vec![]; for library in libraries { - // let library: &Map = library.as_object().unwrap(); - - // if let Some(_) = library.get("natives") { - // natives_ret.push(library); - // continue; - // } - - // let artifact = library.get("downloads").ok_or("No downloads object")? - // .as_object().ok_or("Invalid downloads object")? - // .get("artifact").ok_or("No artifact object")? - // .as_object().ok_or("Invalid artifact object")?; - - // let path = artifact.get("path").ok_or("No path object")? - // .as_str().ok_or("Invalid path object")?; - // let url = artifact.get("url").ok_or("No url object")? - // .as_str().ok_or("Invalid url object")?; - if let Some(_) = library.natives { natives_ret.push(library); continue; @@ -165,14 +147,14 @@ pub mod vanilla_impl { .downloads .artifact .clone() - .ok_or("No artifact object")?; + .ok_or(anyhow!("No artifact object"))?; let path = artifact.path; let url = artifact.url; // TODO: Add checks for rules + platform let dest = libraries_folder.join(path); - fs::create_dir_all(dest.parent().ok_or("Couldn't get library parent")?)?; + fs::create_dir_all(dest.parent().ok_or(anyhow!("Couldn't get library parent"))?)?; if !dest.exists() { download_file(url.as_str(), &dest).await?; @@ -180,7 +162,7 @@ pub mod vanilla_impl { libraries_ret.push( dest.to_str() - .ok_or("Couldn't get library path")? + .ok_or(anyhow!("Couldn't get library path"))? .to_string(), ); } @@ -188,11 +170,11 @@ pub mod vanilla_impl { Ok(libraries_ret) } - pub async fn setup_natives() -> Result<(), Box> { + pub async fn setup_natives() -> PolyResult<()> { Ok(()) } - pub async fn setup_assets() -> Result<(), Box> { + pub async fn setup_assets() -> PolyResult<()> { Ok(()) } } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 032c83cf..104cef2f 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -56,4 +56,4 @@ pub enum PolyError { } /// Alias for a [`Result`] with the error type [`PolyError`]. -pub type PolyResult = std::result::Result; +pub type PolyResult = anyhow::Result;