From 48393b209396db9ddd44251b2bb445d3ad7533fb Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 25 May 2023 17:23:52 +0200 Subject: changed a whole lot og references, fuck rust --- src/commands/download.rs | 6 +++--- src/commands/io.rs | 34 +++++++++++++++++----------------- src/commands/list.rs | 33 +++++++++++++++++---------------- src/commands/modification.rs | 30 ++++++++++++++---------------- src/commands/update.rs | 32 +++++++++++++++----------------- 5 files changed, 66 insertions(+), 69 deletions(-) (limited to 'src/commands') diff --git a/src/commands/download.rs b/src/commands/download.rs index 6831714..fea3f34 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -9,13 +9,13 @@ use crate::{ modrinth::get_raw_versions, }; -pub async fn download(config: Cfg, liststack: Vec, clean: bool, delete_old: bool) -> MLE<()> { +pub async fn download(config: &Cfg, liststack: Vec, clean: bool, delete_old: bool) -> MLE<()> { for current_list in liststack { println!("Downloading current versions of mods in {}", current_list.id); let downloaded_versions = get_downloaded_versions(current_list.clone())?; // println!("To download: {:#?}", downloaded_versions); let current_version_ids = match userlist_get_all_current_versions_with_mods( - config.clone(), + config, String::from(¤t_list.id), ) { Ok(i) => Ok(i), @@ -66,7 +66,7 @@ pub async fn download(config: Cfg, liststack: Vec, clean: bool, delete_old // println!("Deleting version {} for mod {}", ver.1, ver.0); delete_version(current_list.clone(), ver.1)?; } else { - disable_version(config.clone(), current_list.clone(), ver.1, ver.0)?; + disable_version(config, current_list.clone(), ver.1, ver.0)?; }; } } diff --git a/src/commands/io.rs b/src/commands/io.rs index 2a26f1d..43e642a 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -21,9 +21,9 @@ struct ExportVersion { } impl ExportVersion { - fn from(config: Cfg, list_id: &str, mod_id: &str) -> MLE { + fn from(config: &Cfg, list_id: &str, mod_id: &str) -> MLE { Ok(Self { - version: userlist_get_current_version(config.clone(), list_id, mod_id)?, + version: userlist_get_current_version(config, list_id, mod_id)?, set: userlist_get_set_version(config, list_id, mod_id)? }) } @@ -39,18 +39,18 @@ struct ExportList { } impl ExportList { - pub fn from(config: Cfg, list_id: String, download: bool) -> MLE { - let list = lists_get(config.clone(), String::from(&list_id))?; + pub fn from(config: &Cfg, list_id: String, download: bool) -> MLE { + let list = lists_get(config, String::from(&list_id))?; let mut dl_folder = None; if download { dl_folder = Some(list.download_folder) }; - let mods = userlist_get_all_ids(config.clone(), &list_id)?; + let mods = userlist_get_all_ids(config, &list_id)?; let mut versions = vec![]; for m in mods { - versions.push(ExportVersion::from(config.clone(), &list_id, &m)?) + versions.push(ExportVersion::from(config, &list_id, &m)?) } Ok(Self { @@ -63,16 +63,16 @@ impl ExportList { } } -pub fn export(config: Cfg, list: Option) -> MLE<()> { +pub fn export(config: &Cfg, list: Option) -> MLE<()> { let mut list_ids: Vec = vec![]; if list.is_none() { - list_ids = lists_get_all_ids(config.clone())?; + list_ids = lists_get_all_ids(config)?; } else { - list_ids.push(lists_get(config.clone(), list.unwrap())?.id); + list_ids.push(lists_get(config, list.unwrap())?.id); } let mut lists: Vec = vec![]; for list_id in list_ids { - lists.push(ExportList::from(config.clone(), list_id, true)?); + lists.push(ExportList::from(config, list_id, true)?); } let toml = toml::to_string(&Export { lists })?; @@ -85,7 +85,7 @@ pub fn export(config: Cfg, list: Option) -> MLE<()> { Ok(()) } -pub async fn import(config: Cfg, file_str: String, direct_download: bool) -> MLE<()> { +pub async fn import(config: &Cfg, file_str: String, direct_download: bool) -> MLE<()> { let mut file = File::open(file_str)?; let mut content = String::new(); file.read_to_string(&mut content)?; @@ -99,18 +99,18 @@ pub async fn import(config: Cfg, file_str: String, direct_download: bool) -> MLE download_folder: exportlist.download_folder.ok_or("NO_DL").unwrap(), }; lists_insert( - config.clone(), - list.id.clone(), - list.mc_version.clone(), - list.modloader.clone(), - String::from(&list.download_folder), + config, + &list.id, + &list.mc_version, + &list.modloader, + &list.download_folder, )?; let mut ver_ids = vec![]; for id in exportlist.versions { ver_ids.push(AddMod { id: IDSelector::VersionID(id.version), set_version: id.set} ); } - mod_add(config.clone(), ver_ids, list, direct_download).await?; + mod_add(config, ver_ids, list, direct_download).await?; } Ok(()) } diff --git a/src/commands/list.rs b/src/commands/list.rs index c07823b..95f9927 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -16,30 +16,31 @@ pub struct List { pub download_folder: String, } -pub fn get_current_list(config: Cfg) -> MLE { - let id = config_get_current_list(config.clone())?; +pub fn get_current_list(config: &Cfg) -> MLE { + let id = config_get_current_list(config)?; lists_get(config, id) } pub fn list_add( - config: Cfg, - id: String, - mc_version: String, - modloader: Modloader, - directory: String, + config: &Cfg, + id: &str, + mc_version: &str, + modloader: &Modloader, + directory: &str, ) -> MLE<()> { lists_insert(config, id, mc_version, modloader, directory) } -pub fn list_change(config: Cfg, id: String) -> MLE<()> { - if !lists_get_all_ids(config.clone())?.into_iter().any(|l| l == id) { +pub fn list_change(config: &Cfg, id: String) -> MLE<()> { + if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) { return Err(MLError::new(ErrorType::ArgumentError, "List not found")); }; println!("Change default list to: {}", id); config_change_current_list(config, id) } -pub fn list_remove(config: Cfg, id: String) -> MLE<()> { +pub fn list_remove(config: &Cfg, id: String) -> MLE<()> { + //TODO add logging lists_remove(config, id) } @@ -50,7 +51,7 @@ pub fn list_remove(config: Cfg, id: String) -> MLE<()> { /// * `config` - The current config /// * `args` - All args, to extract the new version pub async fn list_version( - config: Cfg, + config: &Cfg, id: String, mc_version: String, download: bool, @@ -61,20 +62,20 @@ pub async fn list_version( id, mc_version ); - lists_version(config.clone(), &id, &mc_version)?; + lists_version(config, &id, &mc_version)?; println!( "\nCheck for updates for new minecraft version in list {}", id ); - let list = lists_get(config.clone(), id)?; + let list = lists_get(config, id)?; update(config, vec![list], true, download, delete).await } -pub fn list_list(config: Cfg) -> MLE<()> { - let lists = lists_get_all_ids(config.clone())?; +pub fn list_list(config: &Cfg) -> MLE<()> { + let lists = lists_get_all_ids(config)?; for list in lists { - let l = lists_get(config.clone(), list)?; + let l = lists_get(config, list)?; println!("{}: | {} | {}", l.id, l.mc_version, l.modloader) } Ok(()) diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 730583d..31931f8 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -11,11 +11,9 @@ use crate::{ error::{ErrorType, MLError, MLE}, files::{delete_version, download_versions}, modrinth::{extract_current_version, get_raw_versions, project, projects, versions, Version}, - List, + List, PROGRESS_CHARS, }; -const PROGRESS_CHARS: &str = "#>-"; - #[derive(Debug, Clone)] pub struct AddMod { pub id: IDSelector, @@ -40,7 +38,7 @@ pub struct ProjectInfo { } pub async fn mod_add( - config: Cfg, + config: &Cfg, mods: Vec, list: List, direct_download: bool, @@ -74,11 +72,11 @@ pub async fn mod_add( info_p.set_style(bar_style.clone()); let mut projectinfo: Vec = Vec::new(); if !mod_ids.is_empty() { - projectinfo.append(&mut get_mod_infos(config.clone(), mod_ids, list.clone()).await?); + projectinfo.append(&mut get_mod_infos(config, mod_ids, list.clone()).await?); info_p.inc(1); }; if !ver_ids.is_empty() { - projectinfo.append(&mut get_ver_info(config.clone(), ver_ids).await?); + projectinfo.append(&mut get_ver_info(config, ver_ids).await?); info_p.inc(1); }; @@ -105,7 +103,7 @@ pub async fn mod_add( }; match userlist_insert( - config.clone(), + config, &list.id, &project.mod_id, ¤t_version_id, @@ -128,7 +126,7 @@ pub async fn mod_add( }?; match mods_insert( - config.clone(), + config, &project.mod_id, &project.slug, &project.title, @@ -161,7 +159,7 @@ pub async fn mod_add( Ok(()) } -async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> MLE> { +async fn get_mod_infos(config: &Cfg, mod_ids: Vec<(String, bool)>, list: List) -> MLE> { let mut setmap: HashMap = HashMap::new(); @@ -255,7 +253,7 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> Ok(projectinfo) } -async fn get_ver_info(config: Cfg, ver_ids: Vec<(String, bool)>) -> MLE> { +async fn get_ver_info(config: &Cfg, ver_ids: Vec<(String, bool)>) -> MLE> { let mut setmap: HashMap = HashMap::new(); @@ -306,16 +304,16 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec<(String, bool)>) -> MLE MLE<()> { +pub fn mod_remove(config: &Cfg, id: &str, list: List) -> MLE<()> { let mod_id = mods_get_id(&config.data, id)?; - println!("Remove mod {} from {}", mods_get_info(&config, &mod_id)?.title, list.id); - let version = userlist_get_current_version(config.clone(), &list.id, &mod_id)?; + println!("Remove mod {} from {}", mods_get_info(config, &mod_id)?.title, list.id); + let version = userlist_get_current_version(config, &list.id, &mod_id)?; print!(" └Remove from list"); //Force flush of stdout, else print! doesn't print instantly std::io::stdout().flush()?; - userlist_remove(config.clone(), &list.id, &mod_id)?; + userlist_remove(config, &list.id, &mod_id)?; println!(" ✓"); print!(" └Delete file"); @@ -334,12 +332,12 @@ pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { print!(" └Clean main db table"); //Force flush of stdout, else print! doesn't print instantly std::io::stdout().flush()?; - let list_ids = lists_get_all_ids(config.clone())?; + let list_ids = lists_get_all_ids(config)?; // Remove mod from main list if not used elsewhere let mut mod_used = false; for id in list_ids { - let mods = match userlist_get_all_ids(config.clone(), &id) { + let mods = match userlist_get_all_ids(config, &id) { Ok(m) => m, Err(err) => { if err.to_string() == "Database: NO_MODS_USERLIST" { diff --git a/src/commands/update.rs b/src/commands/update.rs index 2de13f3..7482e43 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -9,13 +9,11 @@ use crate::{ error::{ErrorType, MLError, MLE}, files::{clean_list_dir, delete_version, disable_version, download_versions}, modrinth::{extract_current_version, versions, Version}, - List, + List, PROGRESS_CHARS, }; -const PROGRESS_CHARS: &str = "#>-"; - pub async fn update( - config: Cfg, + config: &Cfg, liststack: Vec, clean: bool, direct_download: bool, @@ -33,7 +31,7 @@ pub async fn update( for current_list in liststack { // println!("Update mods in {}", current_list.id); - let mods = userlist_get_all_ids(config.clone(), ¤t_list.id)?; + let mods = userlist_get_all_ids(config, ¤t_list.id)?; let list_p = mp.insert_before(&update_p, ProgressBar::new(mods.len().try_into().unwrap())); list_p.set_style(bar_style.clone()); @@ -47,11 +45,11 @@ pub async fn update( let mod_p = mp.insert_before(&list_p, ProgressBar::new(1)); mod_p.set_style(spinner_style.clone()); - let info = mods_get_info(&config, &id)?; + let info = mods_get_info(config, &id)?; mod_p.set_message(format!("Update {}", info.title)); // println!(" ├{}", info.title); - if userlist_get_set_version(config.clone(), ¤t_list.id, &id)? { + if userlist_get_set_version(config, ¤t_list.id, &id)? { // println!(" │ └Set version, skipping update"); list_p.inc(1); continue; @@ -59,16 +57,16 @@ pub async fn update( //Getting current installed version for disable or delete let disable_version = - userlist_get_current_version(config.clone(), ¤t_list.id, &id)?; + userlist_get_current_version(config, ¤t_list.id, &id)?; mod_p.inc(1); updatestack.push( match specific_update( - config.clone(), + config, clean, current_list.clone(), - String::from(&id), + &id, &mod_p ) .await @@ -112,7 +110,7 @@ pub async fn update( delete_version(current_list.clone(), ver.0)?; } else if ver.0 != "NONE" { println!(" └Disable version {}", ver.0); - disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; + disable_version(config, current_list.clone(), ver.0, ver.1)?; }; } } @@ -126,9 +124,9 @@ pub async fn update( Ok(()) } -async fn specific_update(config: Cfg, clean: bool, list: List, id: String, progress: &ProgressBar) -> MLE { +async fn specific_update(config: &Cfg, clean: bool, list: List, id: &str, progress: &ProgressBar) -> MLE { let applicable_versions = - versions(&config.apis.modrinth, String::from(&id), list.clone()).await; + versions(&config.apis.modrinth, String::from(id), list.clone()).await; let mut versions: Vec = vec![]; @@ -144,9 +142,9 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String, progr if clean || (versions.join("|") != userlist_get_applicable_versions( - config.clone(), + config, String::from(&list.id), - String::from(&id), + String::from(id), )?) { let current_str = extract_current_version(applicable_versions.clone())?; @@ -154,7 +152,7 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String, progr if clean { // println!("\t └Add version to downloadstack"); } else { - progress.println(format!("Found new version for {}", mods_get_info(&config, &id).unwrap().title)); + progress.println(format!("Found new version for {}", mods_get_info(config, id).unwrap().title)); // println!("\t └Get versions for specified minecraft versions"); // println!("\t └New current version: {}", current_str); }; @@ -178,7 +176,7 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String, progr } .url; - userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id)?; + userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id.to_string())?; } if current.is_empty() { -- cgit v1.2.3