From f7a6d2e9c67c1fdf8fc17fa0461a201fd2720537 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 19 Jan 2023 18:37:42 +0100 Subject: input mostly inplemented, mods missing --- src/commands/download.rs | 11 +++++--- src/commands/io.rs | 30 +++++++++----------- src/commands/list.rs | 73 ++++++++++++++---------------------------------- src/commands/mod.rs | 12 ++++---- src/commands/update.rs | 18 +++++++----- 5 files changed, 58 insertions(+), 86 deletions(-) (limited to 'src/commands') diff --git a/src/commands/download.rs b/src/commands/download.rs index b958bf3..0f63876 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,7 +1,7 @@ -use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version}, db::{userlist_get_all_current_versions_with_mods, lists_get_all_ids, lists_get}, modrinth::get_raw_versions}; +use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version}, db::{userlist_get_all_current_versions_with_mods, lists_get_all_ids, lists_get}, modrinth::get_raw_versions, error::{MLE, ErrorType, MLError}}; use crate::{List, get_current_list, config::Cfg, input::Input}; -pub async fn download(config: Cfg, input: Input) -> Result<(), Box> { +pub async fn download(config: Cfg, input: Input) -> MLE<()> { let mut liststack: Vec = vec![]; if input.all_lists { @@ -18,7 +18,10 @@ pub async fn download(config: Cfg, input: Input) -> Result<(), Box Ok(i), + Err(e) => Err(MLError::new(ErrorType::DBError, e.to_string().as_str())), + }?; let mut to_download: Vec = vec![]; //(mod_id, version_id) @@ -33,7 +36,7 @@ pub async fn download(config: Cfg, input: Input) -> Result<(), Box Result> { + pub fn from(config: Cfg, list_id: String, download: bool) -> MLE { let list = lists_get(config.clone(), String::from(&list_id))?; @@ -32,26 +32,22 @@ impl ExportList { } } -pub async fn io(config: Cfg, input: Input) -> Result<(), Box> { +pub async fn io(config: Cfg, input: Input) -> MLE<()> { - match input.subcommand.clone().ok_or("INVALID_INPUT")? { - Subcmd::Export => { export(config, input)? }, - Subcmd::Import => { import(config, input.args).await? }, - _ => { }, + match input.clone().io_options.unwrap() { + IoOptions::Export => { export(config, input)? }, + IoOptions::Import => { import(config, input).await? }, } Ok(()) } -fn export(config: Cfg, input: Input) -> Result<(), Box> { +fn export(config: Cfg, input: Input) -> MLE<()> { let mut list_ids: Vec = vec![]; if input.all_lists { list_ids = lists_get_all_ids(config.clone())?; } else { - let args = input.args.ok_or("NO_ARGS")?; - for arg in args { - list_ids.push(lists_get(config.clone(), arg)?.id); - } + list_ids.push(lists_get(config.clone(), input.list.unwrap().id)?.id); } let mut lists: Vec = vec![]; for list_id in list_ids { @@ -68,10 +64,10 @@ fn export(config: Cfg, input: Input) -> Result<(), Box> { Ok(()) } -async fn import(config: Cfg, args: Option>) -> Result<(), Box> { +async fn import(config: Cfg, input: Input) -> MLE<()> { - let filestr: String = match args { - Some(args) => String::from(&args[0]), + let filestr: String = match input.file { + Some(args) => String::from(args), None => String::from(devdir(dirs::home_dir().unwrap().join("mlexport.toml").into_os_string().into_string().unwrap().as_str())), }; @@ -83,14 +79,14 @@ async fn import(config: Cfg, args: Option>) -> Result<(), Box = exportlist.mods.split("|").collect(); let mut mod_ids = vec![]; for mod_id in mods { mod_ids.push(String::from(mod_id)); }; - mod_add(config.clone(), mod_ids, list.clone(), false).await?; + //mod_add(config.clone(), mod_ids, list.clone(), false).await?; } Ok(()) } diff --git a/src/commands/list.rs b/src/commands/list.rs index 2fec1c7..bc58787 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,6 +1,4 @@ -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, ListOptions}, /*cmd_update,*/ error::{MLE, ErrorType, MLError}, /*modrinth::MCVersionType*/}; +use crate::{db::{lists_insert, lists_remove, config_change_current_list, config_get_current_list, lists_get}, Modloader, config::Cfg, input::{Input, ListOptions}, /*cmd_update,*/ error::MLE, /*modrinth::MCVersionType*/}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct List { @@ -9,78 +7,49 @@ pub struct List { pub modloader: Modloader, pub download_folder: String, } -/* -pub async fn list(config: Cfg, input: Input) -> Result<(), Box> { - match input.list_options.ok_or("")? { +pub async fn list(config: Cfg, input: Input) -> MLE<()> { + + match input.clone().list_options.unwrap() { ListOptions::Add => { - match add(config, input.args.ok_or("")?) { - Ok(..) => Ok(()), - Err(e) => Err(Box::new(e)) - } + add(config, input) }, ListOptions::Change => { - change(config, input.args) + change(config, input) }, ListOptions::Remove => { - match remove(config, input.args.ok_or("")?) { - Ok(..) => Ok(()), - Err(e) => Err(Box::new(e)) - } + remove(config, input) }, + /* Subcmd::Version => { match version(config, Some(input.args.ok_or("NO_VERSION")?), Some(MCVersionType::Release)).await { Ok(..) => Ok(()), Err(e) => Err(Box::new(e)) } - } - _ => { - Err(Box::new(Error::new(ErrorKind::InvalidInput, "WRONG_SUBCOMMAND"))) - } + }*/ } } -*/ + 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) -> MLE<()> { - match args.len() { - 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]); - let mod_loader = Modloader::from(&args[2])?; - let download_folder = String::from(&args[3]); - lists_insert(config, id, mc_version, mod_loader, download_folder) - }, - 5.. => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_MANY_ARGUMENTS")), - _ => panic!("list arguments should never be zero or lower"), - } +fn add(config: Cfg, input: Input) -> MLE<()> { + let id = input.list_id.unwrap(); + let mc_version = input.list_mcversion.unwrap(); + let mod_loader = input.modloader.unwrap(); + let download_folder = input.directory.unwrap(); + lists_insert(config, id, mc_version, mod_loader, download_folder) } -fn change(config: Cfg, args: Option>) -> Result<(), Box> { - let lists = lists_get_all_ids(config.clone())?; - if args.is_none() { println!("Currently selected list: {}", get_current_list(config)?.id); return Ok(()) }; - let argsvec = args.ok_or("BAH")?; - match argsvec.len() { - 1 => { - let list = String::from(&argsvec[0]); - if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); }; - config_change_current_list(config, list) - }, - 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), - _ => panic!("list arguments should never lower than zero"), - } +fn change(config: Cfg, input: Input) -> MLE<()> { + //TODO reimplement current list + config_change_current_list(config, input.list.unwrap().id) } -fn remove(config: Cfg, args: Vec) -> MLE<()> { - match args.len() { - 1 => lists_remove(config, String::from(&args[0])), - 2.. => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_MANY_ARGUMENTS")), - _ => panic!("list arguments should never be zero or lower"), - } +fn remove(config: Cfg, input: Input) -> MLE<()> { + lists_remove(config, input.list.unwrap().id) } /* diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 29fc600..527afc7 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,13 +1,13 @@ //pub mod modification; pub mod list; -//pub mod update; +pub mod update; //pub mod setup; -//pub mod download; -//pub mod io; +pub mod download; +pub mod io; //pub use modification::*; pub use list::*; -//pub use update::*; +pub use update::*; //pub use setup::*; -//pub use download::*; -//pub use io::*; +pub use download::*; +pub use io::*; diff --git a/src/commands/update.rs b/src/commands/update.rs index ca28130..068c3f3 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,5 +1,3 @@ -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}, error::{MLE, MLError, ErrorType}}; pub async fn update(config: Cfg, input: Input) -> MLE<()> { @@ -93,7 +91,7 @@ pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_d Ok(()) } -async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> Result> { +async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> MLE { println!("Checking update for '{}' in {}", project.title, list.id); let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; @@ -114,14 +112,20 @@ async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) //get new versions print!(" | getting new version"); let current_str = extract_current_version(applicable_versions.clone())?; - let current_ver = applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("")?; + let current_ver = match applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("!no current version in applicable_versions") { + Ok(v) => Ok(v), + Err(e) => Err(MLError::new(ErrorType::Other, e)), + }?; current.push(current_ver.clone()); - let link = current_ver.files.into_iter().find(|f| f.primary).ok_or("")?.url; - userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id)?; + let link = match current_ver.files.into_iter().find(|f| f.primary).ok_or("!no primary in links") { + Ok(p) => Ok(p), + Err(e) => Err(MLError::new(ErrorType::Other, e)), + }?.url; + userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id); } - if current.is_empty() { return Err(Box::new(Error::new(ErrorKind::NotFound, "NO_UPDATE_AVAILABLE"))) }; + if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) }; println!(" | ✔️"); Ok(current[0].clone()) -- cgit v1.2.3