From ecc4743fdec43eb578e9c35bb008c68909f1517e Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 17:32:19 +0200 Subject: better error handling --- src/commands/io.rs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) (limited to 'src/commands/io.rs') diff --git a/src/commands/io.rs b/src/commands/io.rs index dea0d84..80bc7d6 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -4,10 +4,14 @@ use std::fs::File; use std::io::prelude::*; use crate::{ - config::Cfg, data::modification::{AddMod, IDSelector}, db::{ + config::Cfg, + data::modification::{AddMod, IDSelector}, + db::{ lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids, userlist_get_current_version, userlist_get_set_version, - }, error::{EType, MLErr, MLE}, mod_add, List, Modloader, STYLE_OPERATION + }, + errors::{ConversionError, Error, MLE}, + mod_add, List, Modloader, STYLE_OPERATION, }; #[derive(Debug, Serialize, Deserialize)] @@ -67,22 +71,13 @@ impl ExportList { /// # Errors pub fn export(config: &Cfg, list: Option) -> 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)?); let mut list_ids: Vec = vec![]; if list.is_none() { list_ids = lists_get_all_ids(config)?; } else { - list_ids.push( - lists_get( - config, - &list.ok_or(MLErr::new(EType::Other, "nolist"))?, - )? - .id, - ); + list_ids.push(lists_get(config, &list.ok_or(Error::ListNotFound)?)?.id); } let mut lists: Vec = vec![]; @@ -95,11 +90,11 @@ pub fn export(config: &Cfg, list: Option) -> MLE<()> { let toml = toml::to_string(&Export { lists })?; let filestr = dirs::home_dir() - .ok_or(MLErr::new(EType::Other, "no home"))? + .ok_or(Error::SysDirNotFound("home".to_string()))? .join("mlexport.toml") .into_os_string() .into_string() - .map_err(|_| MLErr::new(EType::IoError, "No String"))?; + .map_err(|_| ConversionError::InvalidPath)?; progress.set_message("Create file"); let mut file = File::create(&filestr)?; @@ -124,10 +119,10 @@ pub async fn import( let list = List { id: exportlist.id, mc_version: exportlist.mc_version, - modloader: Modloader::from(&exportlist.launcher)?, + modloader: Modloader::try_from(exportlist.launcher.as_str())?, download_folder: exportlist .download_folder - .ok_or(MLErr::new(EType::Other, "NO_DL"))?, + .ok_or(Error::NoDownloadFolder)?, }; lists_insert( config, -- cgit v1.2.3