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/io.rs | 2 +- src/commands/list.rs | 32 +++++++++++++++++++++++--------- src/commands/update.rs | 8 ++++---- 3 files changed, 28 insertions(+), 14 deletions(-) (limited to 'src/commands') diff --git a/src/commands/io.rs b/src/commands/io.rs index 39f92d5..6c4a4d3 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -28,7 +28,7 @@ impl ExportList { let mods = userlist_get_all_ids(config, list_id)?.join("|"); - Ok(Self { id: list.id, mods, launcher: list.modloader.stringify(), mc_version: list.mc_version, download_folder: dl_folder }) + Ok(Self { id: list.id, mods, launcher: list.modloader.to_string(), mc_version: list.mc_version, download_folder: dl_folder }) } } 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 } diff --git a/src/commands/update.rs b/src/commands/update.rs index 11f283e..ca28130 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,8 +1,8 @@ use std::io::{Error, ErrorKind}; -use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, mods_change_versions}, List, input::Input, files::{delete_version, download_versions, disable_version}}; +use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, mods_change_versions}, List, input::Input, files::{delete_version, download_versions, disable_version}, error::{MLE, MLError, ErrorType}}; -pub async fn update(config: Cfg, input: Input) -> Result<(), Box> { +pub async fn update(config: Cfg, input: Input) -> MLE<()> { let mut liststack: Vec = vec![]; if input.all_lists { @@ -19,7 +19,7 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box, clean: bool, direct_download: bool, delete_old: bool) -> Result<(), Box> { +pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_download: bool, delete_old: bool) -> MLE<()> { for current_list in liststack { let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; @@ -37,7 +37,7 @@ pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_d let current_version = &versions[index]; let p_id = String::from(&project.id); let v_id = ¤t_version.mod_id; - if &p_id != v_id { return Err(Box::new(Error::new(ErrorKind::Other, "SORTING_ERROR"))) }; + if &p_id != v_id { return Err(MLError::new(ErrorType::Other, "SORTING_ERROR")) }; //Getting current installed version for disable or delete let disable_version = userlist_get_current_version(config.clone(), String::from(¤t_list.id), String::from(&project.id))?; -- cgit v1.2.3