From 29635b9e3833296b2c908914104ba7165d22d3d5 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 12:03:13 +0200 Subject: remove `# Panics` and fix clippy --- src/apis/modrinth.rs | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) (limited to 'src/apis/modrinth.rs') diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index 75e65e6..7cdc719 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs @@ -131,7 +131,6 @@ pub enum GameVersionType { } /// # Errors -/// # Panics async fn get( api: &str, path: &str, @@ -156,54 +155,57 @@ async fn get( } /// # Errors -/// # Panics -pub async fn project(api: &str, name: &str) -> Project { +pub async fn project(api: &str, name: &str) -> MLE { let url = format!("project/{name}"); - let data = get(api, &url).await.unwrap().unwrap(); + let data = get(api, &url).await + .map_err(|_| MLErr::new(EType::Other, "geterr"))? + .ok_or(MLErr::new(EType::Other, "geterr2"))?; - serde_json::from_slice(&data).unwrap() + serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from project")) } /// # Errors -/// # Panics -pub async fn projects(api: &str, ids: Vec) -> Vec { +pub async fn projects(api: &str, ids: Vec) -> MLE> { let all = ids.join(r#"",""#); let url = format!(r#"projects?ids=["{all}"]"#); - let data = get(api, &url).await.unwrap().unwrap(); + let data = get(api, &url).await + .map_err(|_| MLErr::new(EType::Other, "geterr"))? + .ok_or(MLErr::new(EType::Other, "geterr2"))?; - serde_json::from_slice(&data).unwrap() + serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from projects")) } ///Get applicable versions from `mod_id` with list context /// # Errors -/// # Panics -pub async fn versions(api: &str, id: String, list: List) -> Vec { +pub async fn versions(api: &str, id: String, list: List) -> MLE> { let url = format!( r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, id, list.modloader, list.mc_version ); - let data = get(api, &url).await.unwrap(); + let data = get(api, &url).await + .map_err(|_| MLErr::new(EType::Other, "geterr"))?; - match data { - Some(data) => serde_json::from_slice(&data).unwrap(), + Ok(match data { + Some(data) => serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from version"))?, None => Vec::new(), - } + }) } ///Get version with the version ids /// # Errors -/// # Panics pub async fn get_raw_versions( api: &str, versions: Vec, -) -> Vec { +) -> MLE> { let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); - let data = get(api, &url).await.unwrap().unwrap(); + let data = get(api, &url).await + .map_err(|_| MLErr::new(EType::Other, "geterr"))? + .ok_or(MLErr::new(EType::Other, "geterr2"))?; - serde_json::from_slice(&data).unwrap() + serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from raw version")) } /// # Errors @@ -224,12 +226,11 @@ pub fn extract_current_version(versions: Vec) -> MLE { } /// # Errors -/// # Panics -pub async fn get_game_versions() -> Vec { +pub async fn get_game_versions() -> MLE> { let data = get("https://api.modrinth.com/v2/", "tag/game_version") .await - .unwrap() - .unwrap(); + .map_err(|_| MLErr::new(EType::Other, "geterr"))? + .ok_or(MLErr::new(EType::Other, "geterr2"))?; - serde_json::from_slice(&data).unwrap() + serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from game version")) } -- cgit v1.2.3