From 1890d59428dfcca861ea1b7820411d80cc60d713 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 22 Jan 2023 22:34:17 +0100 Subject: Added list version cmd, fixed some todos --- src/commands/modification.rs | 45 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) (limited to 'src/commands/modification.rs') diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 7d4be8d..6a03b35 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,33 +1,24 @@ -use std::io::{Error, ErrorKind}; +use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, ModOptions}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}}; -use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, Subcmd}, get_current_list, files::{delete_version, download_versions}, List}; - -pub async fn modification(config: Cfg, input: Input) -> Result<(), Box> { - match input.subcommand.as_ref().ok_or("")? { - Subcmd::Add => { +pub async fn modification(config: Cfg, input: Input) -> MLE<()> { + match input.clone().mod_options.ok_or("").unwrap() { + ModOptions::Add => { add(config, input).await }, - Subcmd::Remove => { - remove(config, input.args.ok_or("")?) + ModOptions::Remove => { + remove(config, input) }, - _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "SUBCOMMAND_NOT_AVAILABLE"))) } } -async fn add(config: Cfg, input: Input) -> Result<(), Box> { - - let args = input.args.ok_or("")?; +async fn add(config: Cfg, input: Input) -> MLE<()> { - if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; - - let current_list = get_current_list(config.clone())?; - - mod_add(config, vec![String::from(&args[0])], current_list, input.disable_download).await?; + mod_add(config, vec![String::from(input.mod_id.unwrap())], input.list.unwrap(), input.direct_download).await?; Ok(()) } -pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_download: bool) -> Result<(), Box> { +pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_download: bool) -> MLE<()> { println!("Adding mod(s) {:?}", mod_id); let projects = if mod_id.len() == 1 { @@ -50,7 +41,7 @@ pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_downl current_version_id = current_version.clone().unwrap().id; - file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url; + file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url; for ver in available_versions { available_versions_vec.push(ver.id); @@ -67,7 +58,7 @@ pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_downl match userlist_get_all_ids(config.clone(), list.clone().id) { Ok(mods) => { if mods.contains(&project.id) { - return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } + return Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_LIST")); } else { userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; } @@ -88,24 +79,22 @@ pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_downl }, }; - if !disable_download && current_version.is_some() { download_versions(list.clone(), vec![current_version.unwrap()]).await?; }; + if !disable_download && current_version.is_some() { download_versions(list.clone(), config.clone(), vec![current_version.unwrap()]).await?; }; } Ok(()) } -fn remove(config: Cfg, args: Vec) -> Result<(), Box> { - if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; +fn remove(config: Cfg, input: Input) -> MLE<()> { - let current_list = get_current_list(config.clone())?; - let mod_id = mods_get_id(config.clone(), String::from(&args[0]))?; + let mod_id = mods_get_id(config.clone(), input.clone().mod_id.unwrap())?; - let version = userlist_get_current_version(config.clone(), String::from(¤t_list.id), String::from(&mod_id))?; + let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; //TODO implement remove from modlist if not in any other lists && config clean is true - userlist_remove(config.clone(), String::from(¤t_list.id), String::from(&mod_id))?; - delete_version(current_list, version)?; + userlist_remove(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; + delete_version(input.list.unwrap(), version)?; let list_ids = lists_get_all_ids(config.clone())?; -- cgit v1.2.3