From 89193143f90e1b8cbb91445d14942fa39509acbb Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 9 Jan 2023 23:12:52 +0100 Subject: implemented more Error (dumb) --- src/commands/list.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'src/commands/list.rs') diff --git a/src/commands/list.rs b/src/commands/list.rs index 585efe2..526b434 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,6 +1,6 @@ use std::io::{Error, ErrorKind}; -use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, Subcmd}}; +use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, Subcmd}, cmd_update, error::{MLE, ErrorType, MLError}}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct List { @@ -14,7 +14,10 @@ pub async fn list(config: Cfg, input: Input) -> Result<(), Box { - add(config, input.args.ok_or("")?) + match add(config, input.args.ok_or("")?) { + Ok(..) => Ok(()), + Err(e) => Err(Box::new(e)) + } }, Subcmd::Change => { change(config, input.args) @@ -23,7 +26,10 @@ pub async fn list(config: Cfg, input: Input) -> Result<(), Box { - version(config, input.args.ok_or("NO_VERSION")?) + match version(config, input.args.ok_or("NO_VERSION")?).await { + Ok(..) => Ok(()), + Err(e) => Err(Box::new(e)) + } } _ => { Err(Box::new(Error::new(ErrorKind::InvalidInput, "WRONG_SUBCOMMAND"))) @@ -31,14 +37,14 @@ pub async fn list(config: Cfg, input: Input) -> Result<(), Box Result> { +pub fn get_current_list(config: Cfg) -> MLE { let id = config_get_current_list(config.clone())?; lists_get(config, id) } -fn add(config: Cfg, args: Vec) -> Result<(), Box> { +fn add(config: Cfg, args: Vec) -> MLE<()> { match args.len() { - 1 | 2 | 3 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))), + 1 | 2 | 3 => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_FEW_ARGUMENTS")), 4 => { let id = String::from(&args[0]); let mc_version = String::from(&args[1]); @@ -46,7 +52,7 @@ fn add(config: Cfg, args: Vec) -> Result<(), Box> let download_folder = String::from(&args[3]); lists_insert(config, id, mc_version, mod_loader, download_folder) }, - 5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), + 5.. => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_MANY_ARGUMENTS")), _ => panic!("list arguments should never be zero or lower"), } } @@ -74,7 +80,15 @@ fn remove(config: Cfg, args: Vec) -> Result<(), Box) -> Result<(), Box> { - lists_version(config.clone(), config_get_current_list(config.clone())?, String::from(&args[0])) +///Changing the current lists version and updating it +/// #Arguments +/// +/// * `config` - The current config +/// * `args` - All args, to extract the new version +async fn version(config: Cfg, args: Vec) -> MLE<()> { + let current_list = lists_get(config.clone(), config_get_current_list(config.clone())?)?; + + lists_version(config.clone(), String::from(¤t_list.id), String::from(&args[0]))?; //update the list & with -- args + cmd_update(config, vec![current_list], true, true, false).await } -- cgit v1.2.3