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 ++++++++++++++++++++-------------------- src/cache.rs | 31 +++++++++++-------------- src/commands/download.rs | 48 +++++++++++++++++++-------------------- src/commands/io.rs | 3 +-- src/commands/modification.rs | 54 +++++++++++++++++++++++++++++--------------- src/commands/update.rs | 22 +++++++++++------- src/files.rs | 18 ++++----------- src/lib.rs | 6 +---- 8 files changed, 119 insertions(+), 112 deletions(-) (limited to 'src') 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")) } diff --git a/src/cache.rs b/src/cache.rs index ef096f7..6ffeb52 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -3,17 +3,15 @@ use std::{ fs::{copy, read_dir}, }; -/// . -/// -/// # Panics -/// -/// Panics if . -#[must_use] pub fn get_cached_versions(path: &str) -> HashMap { +use crate::error::{EType, MLErr, MLE}; + +/// # Errors +pub fn get_cached_versions(path: &str) -> MLE> { let mut versions: HashMap = HashMap::new(); - for file in read_dir(path).unwrap() { - let path = file.unwrap().path(); - if path.is_file() && path.extension().ok_or("BAH").unwrap() == "jar" { - let pathstr = path.to_str().ok_or("BAH").unwrap(); + for file in read_dir(path).map_err(|_| MLErr::new(EType::IoError, "readdir"))? { + let path = file.map_err(|_| MLErr::new(EType::IoError, "file"))?.path(); + if path.is_file() && path.extension().ok_or(MLErr::new(EType::IoError, "ext"))? == "jar" { + let pathstr = path.to_str().ok_or(MLErr::new(EType::IoError, "path"))?; let namesplit: Vec<&str> = pathstr.split('.').collect(); versions.insert( String::from(namesplit[namesplit.len() - 2]), @@ -21,17 +19,14 @@ use std::{ ); } } - versions + Ok(versions) } -/// . -/// -/// # Panics -/// -/// Panics if . -pub fn copy_cached_version(version_path: &str, download_path: &str) { +/// # Errors +pub fn copy_cached_version(version_path: &str, download_path: &str) -> MLE<()> { let versplit: Vec<&str> = version_path.split('/').collect(); let download = format!("{}/{}", download_path, versplit[versplit.len() - 1]); - copy(version_path, download).unwrap(); + copy(version_path, download).map_err(|err| MLErr::new(EType::IoError, &err.to_string()))?; + Ok(()) } diff --git a/src/commands/download.rs b/src/commands/download.rs index 7321832..7af1066 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -15,7 +15,6 @@ use crate::{ use crate::{PROGRESS_CHARS, STYLE_BAR_POS}; /// # Errors -/// # Panics pub async fn download( config: &Cfg, liststack: Vec, @@ -23,29 +22,31 @@ pub async fn download( delete_old: bool, ) -> MLE<()> { let mp = MultiProgress::new(); - let download_p = - mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); + let download_p = mp.add(ProgressBar::new( + liststack + .len() + .try_into() + .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, + )); download_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) - .unwrap() + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? .progress_chars(PROGRESS_CHARS), ); for current_list in liststack { download_p.set_message(format!("Download in {}", current_list.id)); - let downloaded_versions = - get_downloaded_versions(¤t_list)?; + let downloaded_versions = get_downloaded_versions(¤t_list)?; let current_version_ids = match userlist_get_all_current_versions_with_mods( config, ¤t_list.id, ) { Ok(i) => Ok(i), - Err(e) => Err(MLErr::new( - EType::DBError, - e.to_string().as_str(), - )), + Err(e) => { + Err(MLErr::new(EType::DBError, e.to_string().as_str())) + } }?; let mut to_download: Vec = vec![]; @@ -62,8 +63,7 @@ pub async fn download( to_download.push(current_version); } else { let downloaded_version = current_download - .ok_or("SOMETHING_HAS_REALLY_GONE_WRONG") - .unwrap(); + .ok_or(MLErr::new(EType::Other, "IDK, WTF"))?; if ¤t_version != downloaded_version { to_disable.push(( mod_id.clone(), @@ -87,7 +87,7 @@ pub async fn download( download_versions( current_list.clone(), config.clone(), - get_raw_versions(&config.apis.modrinth, to_download).await, + get_raw_versions(&config.apis.modrinth, to_download).await?, &mp, &download_p, ) @@ -95,13 +95,18 @@ pub async fn download( } if !to_disable.is_empty() { - let d_p = mp.insert_before( - &download_p, - ProgressBar::new(to_disable.len().try_into().unwrap()), - ); + let d_p = + mp.insert_before( + &download_p, + ProgressBar::new(to_disable.len().try_into().map_err( + |_| MLErr::new(EType::Other, "ListStackLen"), + )?), + ); d_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) - .unwrap() + .map_err(|_| { + MLErr::new(EType::LibIndicatif, "template error") + })? .progress_chars(PROGRESS_CHARS), ); for ver in to_disable { @@ -112,12 +117,7 @@ pub async fn download( } else { d_p.set_message(format!("Disable version {}", ver.1)); d_p.inc(1); - disable_version( - config, - ¤t_list, - ver.1, - ver.0, - )?; + disable_version(config, ¤t_list, ver.1, ver.0)?; }; } diff --git a/src/commands/io.rs b/src/commands/io.rs index c9691c4..3e171f1 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -68,7 +68,6 @@ impl ExportList { } /// # Errors -/// # Panics pub fn export(config: &Cfg, list: Option) -> MLE<()> { let progress = ProgressBar::new_spinner(); progress.set_style( @@ -103,7 +102,7 @@ pub fn export(config: &Cfg, list: Option) -> MLE<()> { .join("mlexport.toml") .into_os_string() .into_string() - .unwrap(); + .map_err(|_| MLErr::new(EType::IoError, "No String"))?; progress.set_message("Create file"); let mut file = File::create(&filestr)?; diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 6e6213f..aa1174a 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -44,7 +44,6 @@ pub struct ProjectInfo { } /// # Errors -/// # Panics pub async fn mod_add( config: &Cfg, mods: Vec, @@ -56,11 +55,14 @@ pub async fn mod_add( let mut mod_ids: Vec<(String, bool)> = Vec::new(); let mut ver_ids: Vec<(String, bool)> = Vec::new(); - let add_p = mp.add(ProgressBar::new(mods.len().try_into().map_err(|_| MLErr::new(EType::Other, "MODSLENTRY"))?)); + let add_p = mp.add(ProgressBar::new( + mods.len() + .try_into() + .map_err(|_| MLErr::new(EType::Other, "MODSLENTRY"))?, + )); add_p.set_style( - ProgressStyle::with_template(STYLE_BAR_POS).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })? + ProgressStyle::with_template(STYLE_BAR_POS) + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? .progress_chars(PROGRESS_CHARS), ); add_p.set_message("Sort ids"); @@ -98,11 +100,16 @@ pub async fn mod_add( //Adding each mod to the lists and downloadstack let project_p = mp.insert_before( &add_p, - ProgressBar::new(projectinfo.len().try_into().unwrap()), + ProgressBar::new( + projectinfo + .len() + .try_into() + .map_err(|_| MLErr::new(EType::Other, "infolen"))?, + ), ); project_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) - .unwrap() + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? .progress_chars(PROGRESS_CHARS), ); @@ -112,7 +119,11 @@ pub async fn mod_add( let current_version_id = if project.current_version.is_none() { String::from("NONE") } else { - project.current_version.clone().unwrap().id + project + .current_version + .clone() + .ok_or(MLErr::new(EType::Other, "cur_ver"))? + .id }; match userlist_insert( @@ -158,7 +169,11 @@ pub async fn mod_add( }?; if project.current_version.is_some() { - downloadstack.push(project.current_version.unwrap()); + downloadstack.push( + project + .current_version + .ok_or(MLErr::new(EType::Other, "cur_ver"))?, + ); }; project_p.inc(1); @@ -202,8 +217,8 @@ async fn get_mod_infos( //Get required information from mod_ids let m_projects = match ids.len() { - 1 => vec![project(&config.apis.modrinth, &ids[0]).await], - 2.. => projects(&config.apis.modrinth, ids).await, + 1 => vec![project(&config.apis.modrinth, &ids[0]).await?], + 2.. => projects(&config.apis.modrinth, ids).await?, _ => panic!("PANIC"), }; for project in m_projects { @@ -212,7 +227,7 @@ async fn get_mod_infos( String::from(&project.id), list.clone(), ) - .await; + .await?; let mut available_versions_vec: Vec = Vec::new(); let current_version: Option; @@ -228,7 +243,9 @@ async fn get_mod_infos( current_version, applicable_versions: available_versions_vec, download_link: file, - set_version: *setmap.get(&project.id).unwrap(), + set_version: *setmap + .get(&project.id) + .ok_or(MLErr::new(EType::Other, "not in setmap"))?, }); } else { let current_id = @@ -285,12 +302,12 @@ async fn get_ver_info( let mut projectinfo: Vec = Vec::new(); //Get required information from ver_ids - let mut v_versions = get_raw_versions(&config.apis.modrinth, ids).await; + let mut v_versions = get_raw_versions(&config.apis.modrinth, ids).await?; let mut v_mod_ids: Vec = Vec::new(); for ver in v_versions.clone() { v_mod_ids.push(ver.project_id); } - let mut v_projects = projects(&config.apis.modrinth, v_mod_ids).await; + let mut v_projects = projects(&config.apis.modrinth, v_mod_ids).await?; v_versions.sort_by(|a, b| a.project_id.cmp(&b.project_id)); v_projects.sort_by(|a, b| a.id.cmp(&b.id)); @@ -328,9 +345,10 @@ async fn get_ver_info( /// # Errors pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> { let progress = ProgressBar::new_spinner(); - progress.set_style(ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })?); + progress.set_style( + ProgressStyle::with_template(STYLE_OPERATION) + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?, + ); let mod_id = mods_get_id(&config.data, id)?; diff --git a/src/commands/update.rs b/src/commands/update.rs index d0b930d..c7965e3 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -18,7 +18,6 @@ use crate::{ }; /// # Errors -/// # Panics pub async fn update( config: &Cfg, liststack: Vec, @@ -44,19 +43,26 @@ pub async fn update( update_p.set_message(format!("Update {}", current_list.id)); let list_p = mp.insert_before(&update_p, ProgressBar::new(2)); - list_p - .set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + list_p.set_style( + ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { + MLErr::new(EType::LibIndicatif, "template error") + })?, + ); list_p.set_message("Update mods"); let mods = userlist_get_all_ids(config, ¤t_list.id)?; let list_u_p = mp.insert_before( &list_p, - ProgressBar::new(mods.len().try_into().unwrap()), + ProgressBar::new( + mods.len() + .try_into() + .map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, + ), ); list_u_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) - .unwrap() + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? .progress_chars(PROGRESS_CHARS), ); @@ -129,12 +135,12 @@ pub async fn update( let d_p = mp.insert_before( &list_p, ProgressBar::new( - current_versions.len().try_into().unwrap(), + current_versions.len().try_into().map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?, ), ); d_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) - .unwrap() + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? .progress_chars(PROGRESS_CHARS), ); for ver in current_versions { @@ -175,7 +181,7 @@ async fn specific_update( progress: &ProgressBar, ) -> MLE { let applicable_versions = - versions(&config.apis.modrinth, String::from(id), list.clone()).await; + versions(&config.apis.modrinth, String::from(id), list.clone()).await?; let mut versions: Vec = vec![]; diff --git a/src/files.rs b/src/files.rs index 59f9ed1..636c934 100644 --- a/src/files.rs +++ b/src/files.rs @@ -19,7 +19,6 @@ use crate::{ }; /// # Errors -/// # Panics pub async fn download_versions( list: List, config: Cfg, @@ -27,19 +26,19 @@ pub async fn download_versions( progress: &MultiProgress, progress_before: &ProgressBar, ) -> MLE<()> { - let cached = get_cached_versions(&config.cache); + let cached = get_cached_versions(&config.cache)?; let mut js = JoinSet::new(); - let style_spinner = ProgressStyle::with_template(STYLE_SPINNER).unwrap(); + let style_spinner = ProgressStyle::with_template(STYLE_SPINNER).map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?; let all = progress.insert_before( progress_before, - ProgressBar::new(versions.len().try_into().unwrap()), + ProgressBar::new(versions.len().try_into().map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?), ); all.set_style( ProgressStyle::with_template(STYLE_BAR_POS) - .unwrap() + .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? .progress_chars(PROGRESS_CHARS), ); all.set_message(format!("âś“Downloading {}", list.id)); @@ -66,7 +65,6 @@ pub async fn download_versions( } /// # Errors -/// # Panics async fn download_version( config: Cfg, list: List, @@ -86,7 +84,7 @@ async fn download_version( if c.is_some() { progress.set_message(format!("Get {} from cache", version.id)); cache_msg = " (cached)"; - copy_cached_version(&c.unwrap(), &dl_path); + copy_cached_version(&c.unwrap(), &dl_path)?; } else { let files = version.files; let file = match files.clone().into_iter().find(|f| f.primary) { @@ -124,7 +122,6 @@ async fn download_version( } /// # Errors -/// # Panics async fn download_file( url: &str, path: &str, @@ -166,7 +163,6 @@ async fn download_file( } /// # Errors -/// # Panics pub fn disable_version( config: &Cfg, current_list: &List, @@ -184,7 +180,6 @@ pub fn disable_version( } /// # Errors -/// # Panics pub fn delete_version(list: &List, version: &str) -> MLE<()> { let file = get_file_path(list, version)?; @@ -194,7 +189,6 @@ pub fn delete_version(list: &List, version: &str) -> MLE<()> { } /// # Errors -/// # Panics pub fn get_file_path(list: &List, versionid: &str) -> MLE { let mut names: HashMap = HashMap::new(); for file in read_dir(&list.download_folder)? { @@ -220,7 +214,6 @@ pub fn get_file_path(list: &List, versionid: &str) -> MLE { } /// # Errors -/// # Panics pub fn get_downloaded_versions(list: &List) -> MLE> { let mut versions: HashMap = HashMap::new(); for file in read_dir(&list.download_folder)? { @@ -243,7 +236,6 @@ pub fn get_downloaded_versions(list: &List) -> MLE> { } /// # Errors -/// # Panics pub fn clean_list_dir(list: &List) -> MLE<()> { let dl_path = &list.download_folder; for entry in std::fs::read_dir(dl_path)? { diff --git a/src/lib.rs b/src/lib.rs index 750580f..be63ff8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,6 @@ pub enum Modloader { impl Modloader { /// # Errors - /// # Panics pub fn from(string: &str) -> MLE { match string { "forge" => Ok(Modloader::Forge), @@ -75,7 +74,6 @@ pub enum VersionLevel { /// Checks if update needed (time) /// if yes: get versions, update /// # Errors -/// # Panics pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { let p = ProgressBar::new(1); p.set_style(ProgressStyle::with_template(STYLE_MESSAGE).map_err(|_| { @@ -90,7 +88,7 @@ pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { return Ok(()); } - let versions = get_game_versions().await; + let versions = get_game_versions().await?; remove_file(path)?; let mut file = File::create(path)?; file.write_all(serde_json::to_string_pretty(&versions)?.as_bytes())?; @@ -121,8 +119,6 @@ impl VersionLevel { /// . /// - /// # Panics - /// /// Panics if . /// # Errors pub async fn get( -- cgit v1.2.3