use std::io::{Error, ErrorKind}; use crate::{modrinth::{project, versions, extract_current_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_id = extract_current_version(available_versions.clone())?; let current_version = available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap(); let file = current_version.files.into_iter().find(|f| f.primary).unwrap().url; //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.id, available_versions, file)?; } }, Err(..) => insert_mod_in_list(config.clone(), current_list, String::from(&project.id), current_version.id, available_versions, file)?, }; 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(()), } }