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/list.rs | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) (limited to 'src/commands/list.rs') diff --git a/src/commands/list.rs b/src/commands/list.rs index 23a9f0f..db8a831 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,10 +1,14 @@ use indicatif::{ProgressBar, ProgressStyle}; use crate::{ - config::Cfg, data::modloader::Modloader, db::{ - config_change_current_list, lists_get, - lists_get_all_ids, lists_insert, lists_remove, lists_version, - }, error::{EType, MLErr, MLE}, update, STYLE_OPERATION + config::Cfg, + data::modloader::Modloader, + db::{ + config_change_current_list, lists_get, lists_get_all_ids, lists_insert, + lists_remove, lists_version, + }, + errors::{Error, MLE}, + update, STYLE_OPERATION, }; /// # Errors @@ -16,11 +20,7 @@ pub fn add( directory: &str, ) -> MLE<()> { let p = ProgressBar::new_spinner(); - p.set_style( - ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })?, - ); + p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); p.set_message(format!("Create {id}")); lists_insert(config, id, mc_version, modloader, directory)?; p.finish_with_message(format!("Created {id}")); @@ -30,15 +30,11 @@ pub fn add( /// # Errors pub fn change(config: &Cfg, id: &str) -> MLE<()> { let p = ProgressBar::new_spinner(); - p.set_style( - ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })?, - ); + p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); p.set_message(format!("Change default list to {id}")); if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) { - return Err(MLErr::new(EType::ArgumentError, "List not found")); + return Err(Error::ListNotFound); }; config_change_current_list(config, id)?; @@ -49,11 +45,7 @@ pub fn change(config: &Cfg, id: &str) -> MLE<()> { /// # Errors pub fn remove(config: &Cfg, id: &str) -> MLE<()> { let p = ProgressBar::new_spinner(); - p.set_style( - ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })?, - ); + p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); p.set_message(format!("Remove {id}")); lists_remove(config, id)?; p.finish_with_message(format!("Removed {id}")); @@ -75,11 +67,7 @@ pub async fn version( delete: bool, ) -> MLE<()> { let p = ProgressBar::new_spinner(); - p.set_style( - ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })?, - ); + p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); p.set_message(format!( "Change version for list {id} to minecraft version: {mc_version}" )); -- cgit v1.2.3