From 96cc5257de09682df345e768dc2a91303f9b36c9 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 3 Nov 2022 21:34:04 +0100 Subject: added update beginnings; init of tests --- src/commands/add.rs | 52 -------------------------- src/commands/list.rs | 16 +++++++- src/commands/mod.rs | 6 ++- src/commands/modification.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++ src/commands/update.rs | 40 ++++++++++++++++++++ 5 files changed, 146 insertions(+), 56 deletions(-) delete mode 100644 src/commands/add.rs create mode 100644 src/commands/modification.rs create mode 100644 src/commands/update.rs (limited to 'src/commands') diff --git a/src/commands/add.rs b/src/commands/add.rs deleted file mode 100644 index ed4a6d8..0000000 --- a/src/commands/add.rs +++ /dev/null @@ -1,52 +0,0 @@ -use std::io::{Error, ErrorKind}; - -use crate::{modrinth::{project, versions}, config::Cfg, db::insert_mod, Modloader, input::Input}; - -pub async fn modification(config: Cfg, args: Option>) -> Result<(), Box> { - - if args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) } - - let arguments = Input::from(args.unwrap().join(" "))?; - - if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; - - match arguments.command.as_str() { - "add" => { - add(config, arguments.args.unwrap()).await - }, - _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND"))) - } -} - -pub async fn add(config: Cfg, args: Vec) -> Result<(), Box> { - - if args.len() < 1 { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; - - let project = project(String::from(&config.apis.modrinth), &args[0]).await; - - dbg!(&project); - - let loader = Modloader::Fabric; - - if project.versions.is_empty() { panic!("This should never happen"); }; - - let current_version = get_current(config, String::from(&project.id)).await?; - - //add to current list and mod table - match insert_mod(project.id, project.title, current_version, project.versions, loader, String::from("1.19.2")) { - Err(err) => { Err(Box::new(err)) }, - Ok(()) => Ok(()), - } - -} - -async fn get_current(config: Cfg, id: String) -> Result> { - let available_versions = versions(config.apis.modrinth, id, Modloader::Fabric, String::from("1.19.2")).await; - - match available_versions.len() { - 0 => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_VERSIONS_AVAILABLE"))), - //TODO compare publish dates - 1.. => Ok(available_versions[0].id.to_string()), - _ => panic!("available_versions should never be negative"), - } -} diff --git a/src/commands/list.rs b/src/commands/list.rs index 6c260ce..3dfe1ad 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,12 +1,19 @@ use std::io::{Error, ErrorKind}; -use crate::{db::{insert_list, remove_list, change_list, get_lists, get_current_list}, Modloader, config::Cfg, input::Input}; +use crate::{db::{insert_list, remove_list, change_list, get_lists, get_current_list_id, get_list}, Modloader, config::Cfg, input::Input}; + +#[derive(Clone)] +pub struct List { + pub id: String, + pub mc_version: String, + pub modloader: Modloader, +} pub fn list(config: Cfg, args: Option>) -> Result<(), Box> { if args.is_none() { let lists = get_lists(config.clone())?; - let current_list = get_current_list(config)?; + let current_list = get_current_list_id(config)?; println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list); return Ok(()); } @@ -29,6 +36,11 @@ pub fn list(config: Cfg, args: Option>) -> Result<(), Box Result> { + let id = get_current_list_id(config.clone())?; + get_list(config, id) +} + fn add(config: Cfg, args: Vec) -> Result<(), Box> { match args.len() { 1 | 2 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))), diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 6432746..5d008fd 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,5 +1,7 @@ -pub mod add; +pub mod modification; pub mod list; +pub mod update; -pub use add::*; +pub use modification::*; pub use list::*; +pub use update::*; diff --git a/src/commands/modification.rs b/src/commands/modification.rs new file mode 100644 index 0000000..43e2180 --- /dev/null +++ b/src/commands/modification.rs @@ -0,0 +1,88 @@ +use std::io::{Error, ErrorKind}; + +use crate::{modrinth::{project, versions, Version}, config::Cfg, db::{insert_mod, remove_mod_from_list, get_mod_id, insert_mod_in_list, get_mods, get_mods_from_list}, input::Input, get_current_list}; + +pub async fn modification(config: Cfg, args: Option>) -> Result<(), Box> { + + if args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) } + + let arguments = Input::from(args.unwrap().join(" "))?; + + if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; + + match arguments.command.as_str() { + "add" => { + add(config, arguments.args.unwrap()).await + }, + "remove" => { + remove(config, arguments.args.unwrap()) + }, + _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND"))) + } +} + +async fn add(config: Cfg, args: Vec) -> Result<(), Box> { + + if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; + + let current_list = get_current_list(config.clone())?; + + let project = project(String::from(&config.apis.modrinth), &args[0]).await; + + dbg!(&project); + + if project.versions.is_empty() { panic!("This should never happen"); }; + + let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await; + + let current_version = extract_current_version(available_versions.clone())?; + + //add to current list and mod table + match get_mods_from_list(config.clone(), current_list.clone()) { + Ok(mods) => { + dbg!(&mods); + if mods.contains(&project.id) { + return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } + else { + insert_mod_in_list(config.clone(), current_list.clone(), String::from(&project.id), current_version, available_versions)?; + } + }, + Err(..) => insert_mod_in_list(config.clone(), current_list, String::from(&project.id), current_version, available_versions)?, + }; + + match get_mods(config.clone()) { + Ok(mods) => { + dbg!(&mods); + if mods.contains(&project.id) { + return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) + } else { + insert_mod(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; + } + }, + Err(..) => insert_mod(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?, + }; + + Ok(()) +} + +fn remove(config: Cfg, args: Vec) -> Result<(), Box> { + if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; + + let current_list = get_current_list(config.clone())?; + let mod_id = get_mod_id(config.clone(), String::from(&args[0]))?; + + //TODO implement remove from modlist if not in any other lists && config clean is true + match remove_mod_from_list(config, current_list, mod_id) { + Err(err) => { Err(Box::new(err)) }, + Ok(()) => Ok(()), + } +} + +fn extract_current_version(versions: Vec) -> Result> { + match versions.len() { + 0 => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_VERSIONS_AVAILABLE"))), + //TODO compare publish dates + 1.. => Ok(versions[0].id.to_string()), + _ => panic!("available_versions should never be negative"), + } +} diff --git a/src/commands/update.rs b/src/commands/update.rs new file mode 100644 index 0000000..14c37ec --- /dev/null +++ b/src/commands/update.rs @@ -0,0 +1,40 @@ +use std::io::{Error, ErrorKind}; + +use crate::{config::Cfg, modrinth::projects, get_current_list, db::{get_mods_from_list, get_versions}}; + +pub async fn update(config: Cfg) -> Result<(), Box> { + + let current_list = get_current_list(config.clone())?; + + let mods = get_mods_from_list(config.clone(), current_list)?; + + let mut projects = projects(String::from(&config.apis.modrinth), mods.clone()).await; + + let mut versions = get_versions(config, mods)?; + + projects.sort_by_key(|p| p.id.clone()); + + versions.sort_by_key(|v| v.mod_id.clone()); + + let mut update_stack: Vec = vec![]; + + for (index, project) in projects.iter().enumerate() { + + let cmp_version = &versions[index]; + + let p_id = &project.id; + let v_id = &cmp_version.mod_id; + + if p_id != v_id { return Err(Box::new(Error::new(ErrorKind::Other, "COMPARE_SORTING_ERR"))); }; + println!("{}:{}", p_id, v_id); + + if project.versions.join("|") != cmp_version.versions { + update_stack.push(String::from(&project.id)); + }; + }; + + //TODO UPDATE + dbg!(update_stack); + + Ok(()) +} -- cgit v1.2.3