From a9a55e91ba313c2d30f5cb806aac9c4fceddaf2e Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Wed, 21 Dec 2022 10:27:19 +0100 Subject: finished io functionality, some mod add changes --- src/commands/io.rs | 35 ++++++++----- src/commands/modification.rs | 115 ++++++++++++++++++++++--------------------- 2 files changed, 84 insertions(+), 66 deletions(-) (limited to 'src/commands') diff --git a/src/commands/io.rs b/src/commands/io.rs index 47991c5..2edb95d 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -34,17 +34,25 @@ impl ExportList { pub async fn io(config: Cfg, input: Input) -> Result<(), Box> { - match input.subcommand.ok_or("INVALID_INPUT")? { - Subcmd::Export => { export(config, input.args)? }, - Subcmd::Import => { import(config).await? }, + match input.subcommand.clone().ok_or("INVALID_INPUT")? { + Subcmd::Export => { export(config, input)? }, + Subcmd::Import => { import(config, input.args).await? }, _ => { }, } Ok(()) } -fn export(config: Cfg, _args: Option>) -> Result<(), Box> { - let list_ids = lists_get_all_ids(config.clone())?; +fn export(config: Cfg, input: Input) -> Result<(), Box> { + 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); + } + } let mut lists: Vec = vec![]; for list_id in list_ids { lists.push(ExportList::from(config.clone(), String::from(list_id), true)?); @@ -58,9 +66,14 @@ fn export(config: Cfg, _args: Option>) -> Result<(), Box Result<(), Box> { +async fn import(config: Cfg, args: Option>) -> Result<(), Box> { + + let filestr: String = match args { + Some(args) => String::from(&args[0]), + None => String::from("export.toml"), + }; - let mut file = File::open("export.toml")?; + let mut file = File::open(filestr)?; let mut content = String::new(); file.read_to_string(&mut content)?; let export: Export = toml::from_str(&content)?; @@ -70,12 +83,12 @@ async fn import(config: Cfg) -> Result<(), Box> { for exportlist in export.lists { let list = List { id: exportlist.id, mc_version: exportlist.mc_version, modloader: Modloader::from(&exportlist.launcher)?, download_folder: exportlist.download_folder.ok_or("NO_DL")? }; lists_insert(config.clone(), list.id.clone(), list.mc_version.clone(), list.modloader.clone(), String::from(&list.download_folder))?; - //TODO currently workaround, too many requests let mods: Vec<&str> = exportlist.mods.split("|").collect(); + let mut mod_ids = vec![]; for mod_id in mods { - println!("Adding {}", mod_id); - mod_add(config.clone(), mod_id, list.clone(), false).await?; - } + mod_ids.push(String::from(mod_id)); + }; + mod_add(config.clone(), mod_ids, list.clone(), false).await?; } Ok(()) } diff --git a/src/commands/modification.rs b/src/commands/modification.rs index f66ce28..7d4be8d 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,9 +1,8 @@ use std::io::{Error, ErrorKind}; -use crate::{modrinth::{project, versions, extract_current_version, Version}, 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}; +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 => { add(config, input).await @@ -23,69 +22,75 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box let current_list = get_current_list(config.clone())?; - mod_add(config, &args[0], current_list, input.disable_download).await?; + mod_add(config, vec![String::from(&args[0])], current_list, input.disable_download).await?; Ok(()) } -pub async fn mod_add(config: Cfg, mod_id: &str, list: List, disable_download: bool) -> Result<(), Box> { +pub async fn mod_add(config: Cfg, mod_id: Vec, list: List, disable_download: bool) -> Result<(), Box> { - println!("Adding mod {}", mod_id); - - let project = project(String::from(&config.apis.modrinth), &mod_id).await; - - let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; - - let mut available_versions_vec: Vec = Vec::new(); - let current_version: Option; - let current_version_id: String; - let file: String; - if !available_versions.is_empty() { - let current_id = extract_current_version(available_versions.clone())?; - - current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); - - 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; - - for ver in available_versions { - available_versions_vec.push(ver.id); - }; + println!("Adding mod(s) {:?}", mod_id); + let projects = if mod_id.len() == 1 { + vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await] } else { - println!("There's currently no mod version for your specified target"); - current_version = None; - current_version_id = String::from("NONE"); - file = String::from("NONE"); - available_versions_vec.push(String::from("NONE")); - } - - //add to current list and mod table - 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"))); } - else { - userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; - } - }, - Err(..) => userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?, + projects(String::from(&config.apis.modrinth), mod_id).await }; - match mods_get_all_ids(config.clone()) { - Ok(mods) => { - if mods.contains(&project.id) { - //return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) - } else { + for project in projects { + let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; + + let mut available_versions_vec: Vec = Vec::new(); + let current_version: Option; + let current_version_id: String; + let file: String; + if !available_versions.is_empty() { + let current_id = extract_current_version(available_versions.clone())?; + + current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); + + 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; + + for ver in available_versions { + available_versions_vec.push(ver.id); + }; + } else { + println!("There's currently no mod version for your specified target"); + current_version = None; + current_version_id = String::from("NONE"); + file = String::from("NONE"); + available_versions_vec.push(String::from("NONE")); + } + + //add to current list and mod table + 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"))); } + else { + userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; + } + }, + Err(..) => userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?, + }; + + match mods_get_all_ids(config.clone()) { + Ok(mods) => { + if mods.contains(&project.id) { + //return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) + } else { + mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; + } + }, + Err(..) => { mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; - } - }, - Err(..) => { - mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; - }, - }; + }, + }; - if !disable_download && current_version.is_some() { download_versions(list, vec![current_version.unwrap()]).await?; }; + if !disable_download && current_version.is_some() { download_versions(list.clone(), vec![current_version.unwrap()]).await?; }; + + } Ok(()) } -- cgit v1.2.3