From c7ecf3019a75dc0ab1a0aefeb9b880899fc8a231 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 29 May 2023 18:01:12 +0200 Subject: cargo fmt and add fmt file --- .rustfmt.toml | 1 + src/apis/modrinth.rs | 10 +++- src/cache.rs | 3 +- src/commands/download.rs | 39 +++++++++---- src/commands/io.rs | 6 +- src/commands/list.rs | 4 +- src/commands/modification.rs | 55 +++++++++++++++---- src/commands/update.rs | 56 +++++++++++++++---- src/config.rs | 7 ++- src/db.rs | 128 +++++++++++++++++++++++++++++-------------- src/error.rs | 8 ++- src/files.rs | 18 ++++-- src/lib.rs | 14 ++++- src/main.rs | 17 ++++-- 14 files changed, 269 insertions(+), 97 deletions(-) create mode 100644 .rustfmt.toml diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..df99c69 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1 @@ +max_width = 80 diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index fb3952d..9a22633 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs @@ -130,7 +130,10 @@ pub enum GameVersionType { beta, } -async fn get(api: &str, path: &str) -> Result>, Box> { +async fn get( + api: &str, + path: &str, +) -> Result>, Box> { let url = format!(r#"{}{}"#, api, path); let client = Client::builder() @@ -182,7 +185,10 @@ pub async fn versions(api: &str, id: String, list: List) -> Vec { } ///Get version with the version ids -pub async fn get_raw_versions(api: &str, versions: Vec) -> Vec { +pub async fn get_raw_versions( + api: &str, + versions: Vec, +) -> Vec { let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); let data = get(api, &url).await.unwrap().unwrap(); diff --git a/src/cache.rs b/src/cache.rs index 1e22091..8df4d2f 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -31,6 +31,7 @@ pub fn get_cached_versions(path: &str) -> HashMap { /// Panics if . pub fn copy_cached_version(version_path: &str, download_path: &str) { let versplit: Vec<&str> = version_path.split('/').collect(); - let download = format!("{}/{}", download_path, versplit[versplit.len() - 1]); + let download = + format!("{}/{}", download_path, versplit[versplit.len() - 1]); copy(version_path, download).unwrap(); } diff --git a/src/commands/download.rs b/src/commands/download.rs index dd00ffb..a7cf744 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -5,7 +5,8 @@ use crate::{ db::userlist_get_all_current_versions_with_mods, error::{ErrorType, MLError, MLE}, files::{ - clean_list_dir, delete_version, disable_version, download_versions, get_downloaded_versions, + clean_list_dir, delete_version, disable_version, download_versions, + get_downloaded_versions, }, modrinth::get_raw_versions, }; @@ -18,7 +19,8 @@ pub async fn download( delete_old: bool, ) -> MLE<()> { let mp = MultiProgress::new(); - let download_p = mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); + let download_p = + mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); download_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) .unwrap() @@ -28,14 +30,19 @@ pub async fn download( for current_list in liststack { download_p.set_message(format!("Download in {}", current_list.id)); - let downloaded_versions = get_downloaded_versions(current_list.clone())?; - let current_version_ids = match userlist_get_all_current_versions_with_mods( - config, - String::from(¤t_list.id), - ) { - Ok(i) => Ok(i), - Err(e) => Err(MLError::new(ErrorType::DBError, e.to_string().as_str())), - }?; + let downloaded_versions = + get_downloaded_versions(current_list.clone())?; + let current_version_ids = + match userlist_get_all_current_versions_with_mods( + config, + String::from(¤t_list.id), + ) { + Ok(i) => Ok(i), + Err(e) => Err(MLError::new( + ErrorType::DBError, + e.to_string().as_str(), + )), + }?; let mut to_download: Vec = vec![]; //(mod_id, version_id) @@ -54,7 +61,10 @@ pub async fn download( .ok_or("SOMETHING_HAS_REALLY_GONE_WRONG") .unwrap(); if ¤t_version != downloaded_version { - to_disable.push((mod_id.clone(), String::from(downloaded_version))); + to_disable.push(( + mod_id.clone(), + String::from(downloaded_version), + )); to_download.push(current_version); } } @@ -98,7 +108,12 @@ pub async fn download( } else { d_p.set_message(format!("Disable version {}", ver.1)); d_p.inc(1); - disable_version(config, 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 2501583..8e44b2b 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -102,7 +102,11 @@ pub fn export(config: &Cfg, list: Option) -> MLE<()> { Ok(()) } -pub async fn import(config: &Cfg, file_str: &str, direct_download: bool) -> MLE<()> { +pub async fn import( + config: &Cfg, + file_str: &str, + direct_download: bool, +) -> MLE<()> { let mut file = File::open(file_str)?; let mut content = String::new(); file.read_to_string(&mut content)?; diff --git a/src/commands/list.rs b/src/commands/list.rs index b0a082d..3665446 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -3,8 +3,8 @@ use indicatif::{ProgressBar, ProgressStyle}; use crate::{ config::Cfg, db::{ - config_change_current_list, config_get_current_list, lists_get, lists_get_all_ids, - lists_insert, lists_remove, lists_version, + config_change_current_list, config_get_current_list, lists_get, + lists_get_all_ids, lists_insert, lists_remove, lists_version, }, error::{ErrorType, MLError, MLE}, update, Modloader, STYLE_OPERATION, diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 577bbd1..4488b70 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -5,12 +5,16 @@ use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use crate::{ config::Cfg, db::{ - lists_get_all_ids, mods_get_id, mods_get_info, mods_insert, mods_remove, - userlist_get_all_ids, userlist_get_current_version, userlist_insert, userlist_remove, + lists_get_all_ids, mods_get_id, mods_get_info, mods_insert, + mods_remove, userlist_get_all_ids, userlist_get_current_version, + userlist_insert, userlist_remove, }, error::{ErrorType, MLError, MLE}, files::{delete_version, download_versions}, - modrinth::{extract_current_version, get_raw_versions, project, projects, versions, Version}, + modrinth::{ + extract_current_version, get_raw_versions, project, projects, versions, + Version, + }, List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION, }; @@ -60,7 +64,9 @@ pub async fn mod_add( for m in mods { add_p.inc(1); match m.id { - IDSelector::ModificationID(pid) => mod_ids.push((pid, m.set_version)), + IDSelector::ModificationID(pid) => { + mod_ids.push((pid, m.set_version)) + } IDSelector::VersionID(vid) => ver_ids.push((vid, m.set_version)), } } @@ -69,7 +75,8 @@ pub async fn mod_add( let mut projectinfo: Vec = Vec::new(); if !mod_ids.is_empty() { - projectinfo.append(&mut get_mod_infos(config, mod_ids, list.clone()).await?); + projectinfo + .append(&mut get_mod_infos(config, mod_ids, list.clone()).await?); }; if !ver_ids.is_empty() { projectinfo.append(&mut get_ver_info(config, ver_ids).await?); @@ -113,7 +120,10 @@ pub async fn mod_add( project.set_version, ) { Err(e) => { - let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); + let expected_err = format!( + "SQL: UNIQUE constraint failed: {}.mod_id", + list.id + ); if e.to_string() == expected_err { Err(MLError::new( ErrorType::ModError, @@ -126,7 +136,12 @@ pub async fn mod_add( Ok(..) => Ok(..), }?; - match mods_insert(config, &project.mod_id, &project.slug, &project.title) { + match mods_insert( + config, + &project.mod_id, + &project.slug, + &project.title, + ) { Err(e) => { if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { Ok(..) @@ -149,7 +164,14 @@ pub async fn mod_add( //Download all the added mods if direct_download { add_p.set_message("Download mods"); - download_versions(list.clone(), config.clone(), downloadstack, &mp, &add_p).await?; + download_versions( + list.clone(), + config.clone(), + downloadstack, + &mp, + &add_p, + ) + .await?; }; add_p.finish_with_message("Added all mods"); @@ -191,7 +213,8 @@ async fn get_mod_infos( let current_version: Option; let file: String; if !available_versions.is_empty() { - let current_id = extract_current_version(available_versions.clone())?; + let current_id = + extract_current_version(available_versions.clone())?; current_version = Some( available_versions @@ -242,7 +265,10 @@ async fn get_mod_infos( 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(); let mut ids = vec![]; @@ -311,7 +337,9 @@ pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> { match delete_version(list, version) { Ok(_) => (), Err(err) => { - if err.to_string() != "User input not accepted: VERSION_NOT_FOUND_IN_FILES" { + if err.to_string() + != "User input not accepted: VERSION_NOT_FOUND_IN_FILES" + { return Err(err); }; } @@ -343,7 +371,10 @@ pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> { mods_remove(config, &mod_id)?; }; - progress.finish_with_message(format!("Removed {} from {}", info.title, list.id)); + progress.finish_with_message(format!( + "Removed {} from {}", + info.title, list.id + )); Ok(()) } diff --git a/src/commands/update.rs b/src/commands/update.rs index 3aae002..c19c02c 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -4,10 +4,13 @@ use crate::{ config::Cfg, db::{ mods_get_info, userlist_change_versions, userlist_get_all_ids, - userlist_get_applicable_versions, userlist_get_current_version, userlist_get_set_version, + userlist_get_applicable_versions, userlist_get_current_version, + userlist_get_set_version, }, error::{ErrorType, MLError, MLE}, - files::{clean_list_dir, delete_version, disable_version, download_versions}, + files::{ + clean_list_dir, delete_version, disable_version, download_versions, + }, modrinth::{extract_current_version, versions, Version}, List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION, }; @@ -21,7 +24,8 @@ pub async fn update( ) -> MLE<()> { let mp = MultiProgress::new(); - let update_p = mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); + let update_p = + mp.add(ProgressBar::new(liststack.len().try_into().unwrap())); update_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) .unwrap() @@ -32,12 +36,16 @@ pub async fn update( update_p.set_message(format!("Update {}", current_list.id)); let list_p = mp.insert_before(&update_p, ProgressBar::new(2)); - list_p.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); + list_p + .set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); list_p.set_message("Update mods"); let mods = userlist_get_all_ids(config, ¤t_list.id)?; - let list_u_p = mp.insert_before(&list_p, ProgressBar::new(mods.len().try_into().unwrap())); + let list_u_p = mp.insert_before( + &list_p, + ProgressBar::new(mods.len().try_into().unwrap()), + ); list_u_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) .unwrap() @@ -58,10 +66,19 @@ pub async fn update( } //Getting current installed version for disable or delete - let disable_version = userlist_get_current_version(config, ¤t_list.id, &id)?; + let disable_version = + userlist_get_current_version(config, ¤t_list.id, &id)?; updatestack.push( - match specific_update(config, clean, current_list.clone(), &id, &list_u_p).await { + match specific_update( + config, + clean, + current_list.clone(), + &id, + &list_u_p, + ) + .await + { Ok(ver) => { current_versions.push((disable_version, id)); ver @@ -79,7 +96,10 @@ pub async fn update( list_u_p.inc(1); } - list_u_p.finish_with_message(format!("Updated mods in {}", current_list.id)); + list_u_p.finish_with_message(format!( + "Updated mods in {}", + current_list.id + )); if clean { list_p.set_message("Cleaning"); @@ -100,7 +120,9 @@ pub async fn update( if !clean { let d_p = mp.insert_before( &list_p, - ProgressBar::new(current_versions.len().try_into().unwrap()), + ProgressBar::new( + current_versions.len().try_into().unwrap(), + ), ); d_p.set_style( ProgressStyle::with_template(STYLE_BAR_POS) @@ -115,7 +137,12 @@ pub async fn update( } else if ver.0 != "NONE" { d_p.set_message(format!("Disable version {}", ver.0)); d_p.inc(1); - disable_version(config, current_list.clone(), ver.0, ver.1)?; + disable_version( + config, + current_list.clone(), + ver.0, + ver.1, + )?; }; } @@ -144,7 +171,8 @@ async fn specific_update( id: &str, progress: &ProgressBar, ) -> MLE { - let applicable_versions = versions(&config.apis.modrinth, String::from(id), list.clone()).await; + let applicable_versions = + versions(&config.apis.modrinth, String::from(id), list.clone()).await; let mut versions: Vec = vec![]; @@ -159,7 +187,11 @@ async fn specific_update( let mut current: Vec = vec![]; if clean || (versions.join("|") - != userlist_get_applicable_versions(config, String::from(&list.id), String::from(id))?) + != userlist_get_applicable_versions( + config, + String::from(&list.id), + String::from(id), + )?) { let current_str = extract_current_version(applicable_versions.clone())?; diff --git a/src/config.rs b/src/config.rs index 3858484..f0eb8f7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,7 +7,9 @@ use std::{ use indicatif::{ProgressBar, ProgressStyle}; use serde::{Deserialize, Serialize}; -use crate::{check_game_versions, db::db_setup, error::MLE, Modloader, VersionLevel}; +use crate::{ + check_game_versions, db::db_setup, error::MLE, Modloader, VersionLevel, +}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Cfg { @@ -44,7 +46,8 @@ impl Cfg { let mut file = match File::open(&configfile) { Ok(file) => file, Err(err) => { - if err.kind() == std::io::ErrorKind::NotFound && path.is_none() { + if err.kind() == std::io::ErrorKind::NotFound && path.is_none() + { create_config(&configfile)?; File::open(&configfile)? } else { diff --git a/src/db.rs b/src/db.rs index 1958fc5..49db2fd 100644 --- a/src/db.rs +++ b/src/db.rs @@ -21,7 +21,9 @@ pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { Ok(()) } -pub fn mods_get_all_ids(config: &Cfg) -> Result, Box> { +pub fn mods_get_all_ids( + config: &Cfg, +) -> Result, Box> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); @@ -64,8 +66,10 @@ pub fn mods_get_id(data: &str, slug: &str) -> MLE { } //get from id if no slug found if mod_id.is_empty() { - let mut stmt = connection.prepare("SELECT id FROM mods WHERE id = ?")?; - let id_iter = stmt.query_map([slug], |row| row.get::(0))?; + let mut stmt = + connection.prepare("SELECT id FROM mods WHERE id = ?")?; + let id_iter = + stmt.query_map([slug], |row| row.get::(0))?; for id in id_iter { mod_id = id?; @@ -73,8 +77,10 @@ pub fn mods_get_id(data: &str, slug: &str) -> MLE { } //get from title if no id found from slug if mod_id.is_empty() { - let mut stmt = connection.prepare("SELECT id FROM mods WHERE title = ?")?; - let id_iter = stmt.query_map([slug], |row| row.get::(0))?; + let mut stmt = + connection.prepare("SELECT id FROM mods WHERE title = ?")?; + let id_iter = + stmt.query_map([slug], |row| row.get::(0))?; for id in id_iter { mod_id = id?; @@ -98,7 +104,8 @@ pub fn mods_get_info(config: &Cfg, id: &str) -> MLE { let connection = Connection::open(data)?; let mut mod_info: Option = None; - let mut stmt = connection.prepare("SELECT title, slug FROM mods WHERE id = ?")?; + let mut stmt = + connection.prepare("SELECT title, slug FROM mods WHERE id = ?")?; let name_iter = stmt.query_map([id], |row| { Ok(vec![ row.get::(0)?, @@ -135,7 +142,10 @@ pub struct DBModlistVersions { pub versions: String, } -pub fn mods_get_versions(config: &Cfg, mods: Vec) -> MLE> { +pub fn mods_get_versions( + config: &Cfg, + mods: Vec, +) -> MLE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -153,8 +163,9 @@ pub fn mods_get_versions(config: &Cfg, mods: Vec) -> MLE = Vec::new(); - let mut stmt = connection - .prepare(format!("SELECT id, versions, title FROM mods {}", wherestr).as_str())?; + let mut stmt = connection.prepare( + format!("SELECT id, versions, title FROM mods {}", wherestr).as_str(), + )?; let id_iter = stmt.query_map([], |row| { Ok(vec![ row.get::(0)?, @@ -218,7 +229,8 @@ pub fn userlist_get_all_ids(config: &Cfg, list_id: &str) -> MLE> { let connection = Connection::open(data).unwrap(); let mut mod_ids: Vec = Vec::new(); - let mut stmt = connection.prepare(format!("SELECT mod_id FROM {}", list_id).as_str())?; + let mut stmt = connection + .prepare(format!("SELECT mod_id FROM {}", list_id).as_str())?; let id_iter = stmt.query_map([], |row| row.get::(0))?; for id in id_iter { @@ -261,7 +273,8 @@ pub fn userlist_get_applicable_versions( ) .as_str(), )?; - let ver_iter = stmt.query_map([mod_id], |row| row.get::(0))?; + let ver_iter = + stmt.query_map([mod_id], |row| row.get::(0))?; for ver in ver_iter { version = ver?; @@ -281,8 +294,9 @@ pub fn userlist_get_all_applicable_versions_with_mods( let connection = Connection::open(data)?; let mut versions: Vec<(String, String)> = Vec::new(); - let mut stmt = connection - .prepare(format!("SELECT mod_id, applicable_versions FROM {}", list_id).as_str())?; + let mut stmt = connection.prepare( + format!("SELECT mod_id, applicable_versions FROM {}", list_id).as_str(), + )?; let id_iter = stmt.query_map([], |row| { Ok(vec![ row.get::(0)?, @@ -302,14 +316,21 @@ pub fn userlist_get_all_applicable_versions_with_mods( Ok(versions) } -pub fn userlist_get_current_version(config: &Cfg, list_id: &str, mod_id: &str) -> MLE { +pub fn userlist_get_current_version( + config: &Cfg, + list_id: &str, + mod_id: &str, +) -> MLE { let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); let mut version: String = String::new(); - let mut stmt = connection - .prepare(format!("SELECT current_version FROM {} WHERE mod_id = ?", list_id).as_str())?; - let ver_iter = stmt.query_map([&mod_id], |row| row.get::(0))?; + let mut stmt = connection.prepare( + format!("SELECT current_version FROM {} WHERE mod_id = ?", list_id) + .as_str(), + )?; + let ver_iter = + stmt.query_map([&mod_id], |row| row.get::(0))?; for ver in ver_iter { version = ver?; @@ -321,13 +342,16 @@ pub fn userlist_get_current_version(config: &Cfg, list_id: &str, mod_id: &str) - } } -pub fn userlist_get_all_current_version_ids(config: &Cfg, list_id: String) -> MLE> { +pub fn userlist_get_all_current_version_ids( + config: &Cfg, + list_id: String, +) -> MLE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; let mut versions: Vec = Vec::new(); - let mut stmt = - connection.prepare(format!("SELECT current_version FROM {}", list_id).as_str())?; + let mut stmt = connection + .prepare(format!("SELECT current_version FROM {}", list_id).as_str())?; let id_iter = stmt.query_map([], |row| row.get::(0))?; for id in id_iter { @@ -349,8 +373,9 @@ pub fn userlist_get_all_current_versions_with_mods( let connection = Connection::open(data)?; let mut versions: Vec<(String, String)> = Vec::new(); - let mut stmt = - connection.prepare(format!("SELECT mod_id, current_version FROM {}", list_id).as_str())?; + let mut stmt = connection.prepare( + format!("SELECT mod_id, current_version FROM {}", list_id).as_str(), + )?; let id_iter = stmt.query_map([], |row| { Ok(vec![ row.get::(0)?, @@ -373,14 +398,21 @@ pub fn userlist_get_all_current_versions_with_mods( Ok(versions) } -pub fn userlist_get_set_version(config: &Cfg, list_id: &str, mod_id: &str) -> MLE { +pub fn userlist_get_set_version( + config: &Cfg, + list_id: &str, + mod_id: &str, +) -> MLE { let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); let mut set_version: bool = false; - let mut stmt = connection - .prepare(format!("SELECT set_version FROM {} WHERE mod_id = ?", list_id).as_str())?; - let ver_iter = stmt.query_map([&mod_id], |row| row.get::(0))?; + let mut stmt = connection.prepare( + format!("SELECT set_version FROM {} WHERE mod_id = ?", list_id) + .as_str(), + )?; + let ver_iter = + stmt.query_map([&mod_id], |row| row.get::(0))?; for ver in ver_iter { set_version = ver?; @@ -413,11 +445,16 @@ pub fn userlist_add_disabled_versions( let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; - let currently_disabled_versions = - userlist_get_disabled_versions(config, String::from(&list_id), String::from(&mod_id))?; + let currently_disabled_versions = userlist_get_disabled_versions( + config, + String::from(&list_id), + String::from(&mod_id), + )?; let disabled_versions = match currently_disabled_versions == "NONE" { true => disabled_version, - false => format!("{}|{}", currently_disabled_versions, disabled_version), + false => { + format!("{}|{}", currently_disabled_versions, disabled_version) + } }; connection.execute( @@ -440,9 +477,12 @@ pub fn userlist_get_disabled_versions( let connection = Connection::open(data).unwrap(); let mut version: String = String::new(); - let mut stmt = connection - .prepare(format!("SELECT disabled_versions FROM {} WHERE mod_id = ?", list_id).as_str())?; - let ver_iter = stmt.query_map([mod_id], |row| row.get::(0))?; + let mut stmt = connection.prepare( + format!("SELECT disabled_versions FROM {} WHERE mod_id = ?", list_id) + .as_str(), + )?; + let ver_iter = + stmt.query_map([mod_id], |row| row.get::(0))?; for ver in ver_iter { version = ver?; @@ -462,8 +502,9 @@ pub fn userlist_get_all_downloads( let connection = Connection::open(data).unwrap(); let mut links: Vec = Vec::new(); - let mut stmt = - connection.prepare(format!("SELECT current_download FROM {}", list_id).as_str())?; + let mut stmt = connection.prepare( + format!("SELECT current_download FROM {}", list_id).as_str(), + )?; let link_iter = stmt.query_map([], |row| row.get::(0))?; for link in link_iter { @@ -521,8 +562,9 @@ pub fn lists_get(config: &Cfg, list_id: &str) -> MLE { modloader: Modloader::Fabric, download_folder: String::new(), }; - let mut stmt = connection - .prepare("SELECT mc_version, modloader, download_folder FROM lists WHERE id = ?")?; + let mut stmt = connection.prepare( + "SELECT mc_version, modloader, download_folder FROM lists WHERE id = ?", + )?; let list_iter = stmt.query_map([&list_id], |row| { Ok(vec![ @@ -595,7 +637,8 @@ pub fn config_get_current_list(config: &Cfg) -> MLE { let connection = Connection::open(data).unwrap(); let mut list_id = String::new(); - let mut stmt = connection.prepare("SELECT value FROM user_config WHERE id = 'current_list'")?; + let mut stmt = connection + .prepare("SELECT value FROM user_config WHERE id = 'current_list'")?; let list_iter = stmt.query_map([], |row| row.get::(0))?; for list in list_iter { @@ -630,7 +673,9 @@ pub fn s_userlist_update_download( Ok(()) } -pub fn s_config_create_version(config: &Cfg) -> Result<(), Box> { +pub fn s_config_create_version( + config: &Cfg, +) -> Result<(), Box> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -655,12 +700,15 @@ pub fn s_config_update_version( Ok(()) } -pub fn s_config_get_version(config: &Cfg) -> Result> { +pub fn s_config_get_version( + config: &Cfg, +) -> Result> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; let mut version: String = String::new(); - let mut stmt = connection.prepare("SELECT value FROM user_config WHERE id = 'db_version'")?; + let mut stmt = connection + .prepare("SELECT value FROM user_config WHERE id = 'db_version'")?; let ver_iter = stmt.query_map([], |row| row.get::(0))?; for ver in ver_iter { diff --git a/src/error.rs b/src/error.rs index f981f14..a2b37a8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -34,8 +34,12 @@ impl std::error::Error for MLError { impl fmt::Display for MLError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.etype { - ErrorType::ArgumentError => write!(f, "User input not accepted: {}", self.message), - ErrorType::ArgumentCountError => write!(f, "Too many/too few arguments"), + ErrorType::ArgumentError => { + write!(f, "User input not accepted: {}", self.message) + } + ErrorType::ArgumentCountError => { + write!(f, "Too many/too few arguments") + } ErrorType::ConfigError => write!(f, "CONFIG"), ErrorType::DBError => write!(f, "Database: {}", self.message), ErrorType::ModError => write!(f, "Mod: {}", self.message), diff --git a/src/files.rs b/src/files.rs index c81857b..3a16c62 100644 --- a/src/files.rs +++ b/src/files.rs @@ -92,7 +92,9 @@ async fn download_version( let mut splitname: Vec<&str> = file.filename.split('.').collect(); let extension = match splitname.pop().ok_or("") { Ok(e) => e, - Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")), + Err(..) => { + return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")) + } }; let filename = format!( "{}.mr.{}.{}.{}", @@ -102,7 +104,8 @@ async fn download_version( extension ); - download_file(&file.url, &list.download_folder, &filename, &progress).await?; + download_file(&file.url, &list.download_folder, &filename, &progress) + .await?; progress.set_message(format!("Copy {} to cache", version.id)); let dl_path_file = format!("{}/{}", list.download_folder, filename); @@ -119,7 +122,12 @@ async fn download_version( Ok(()) } -async fn download_file(url: &str, path: &str, name: &str, progress: &ProgressBar) -> MLE<()> { +async fn download_file( + url: &str, + path: &str, + name: &str, + progress: &ProgressBar, +) -> MLE<()> { let dl_path_file = format!("{}/{}", path, name); let res = Client::new().get(url).send().await?; @@ -185,7 +193,9 @@ pub fn get_file_path(list: &List, versionid: String) -> MLE { if path.is_file() { let pathstr = match path.to_str().ok_or("") { Ok(s) => s, - Err(..) => return Err(MLError::new(ErrorType::Other, "INVALID_PATH")), + Err(..) => { + return Err(MLError::new(ErrorType::Other, "INVALID_PATH")) + } }; let namesplit: Vec<&str> = pathstr.split('.').collect(); let ver_id = namesplit[namesplit.len() - 2]; diff --git a/src/lib.rs b/src/lib.rs index d76f8bf..f77befc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,9 @@ impl Modloader { "forge" => Ok(Modloader::Forge), "fabric" => Ok(Modloader::Fabric), "quilt" => Ok(Modloader::Quilt), - _ => Err(MLError::new(ErrorType::ArgumentError, "UNKNOWN_MODLOADER")), + _ => { + Err(MLError::new(ErrorType::ArgumentError, "UNKNOWN_MODLOADER")) + } } } } @@ -76,7 +78,9 @@ pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { p.set_message("Update minecraft versions"); let creation_time = fs::metadata(path)?.created()?; - if !force && creation_time.elapsed().unwrap() < Duration::from_secs(60 * 60 * 24) { + if !force + && creation_time.elapsed().unwrap() < Duration::from_secs(60 * 60 * 24) + { return Ok(()); } @@ -107,7 +111,11 @@ impl VersionLevel { } } - pub async fn get(self, versions_path: &str, force_update: bool) -> MLE { + pub async fn get( + self, + versions_path: &str, + force_update: bool, + ) -> MLE { let path = format!("{}/versions.json", versions_path); check_game_versions(&path, force_update).await?; let mut versions = load_game_versions(&path)?.into_iter(); diff --git a/src/main.rs b/src/main.rs index d9ad6af..5d60a17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,9 @@ use clap::{Parser, Subcommand}; use modlist::{ config::Cfg, db::{config_get_current_list, lists_get, lists_get_all_ids}, - download, export, get_current_list, import, list_add, list_change, list_list, list_remove, - list_version, mod_add, mod_remove, update, AddMod, IDSelector, List, Modloader, VersionLevel, + download, export, get_current_list, import, list_add, list_change, + list_list, list_remove, list_version, mod_add, mod_remove, update, AddMod, + IDSelector, List, Modloader, VersionLevel, }; #[derive(Parser)] @@ -170,7 +171,11 @@ async fn main() { } => { let listf = match list { Some(list) => lists_get(&config, &list).unwrap(), - None => lists_get(&config, &config_get_current_list(&config).unwrap()).unwrap(), + None => lists_get( + &config, + &config_get_current_list(&config).unwrap(), + ) + .unwrap(), }; let marked_id = match version { @@ -188,7 +193,11 @@ async fn main() { ModCommands::Remove { id, list } => { let listf = match list { Some(list) => lists_get(&config, &list).unwrap(), - None => lists_get(&config, &config_get_current_list(&config).unwrap()).unwrap(), + None => lists_get( + &config, + &config_get_current_list(&config).unwrap(), + ) + .unwrap(), }; mod_remove(&config, &id, &listf) } -- cgit v1.2.3