From 6a91d0a864f9edd9d9fe50ca89ccbce4fc98e043 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 11:12:04 +0200 Subject: do nearly anything to shut clippy up --- src/db.rs | 222 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 125 insertions(+), 97 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 6a3c9af..b150023 100644 --- a/src/db.rs +++ b/src/db.rs @@ -4,11 +4,12 @@ use rusqlite::Connection; use crate::{ config::Cfg, - error::{ErrorType, MLError, MLE}, + error::{EType, MLErr, MLE}, List, Modloader, }; //MODS +/// # Errors pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -21,11 +22,12 @@ pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { Ok(()) } +/// # Errors pub fn mods_get_all_ids( config: &Cfg, ) -> Result, Box> { let data = format!("{}/data.db", config.data); - let connection = Connection::open(data).unwrap(); + let connection = Connection::open(data)?; let mut mods: Vec = Vec::new(); @@ -36,21 +38,22 @@ pub fn mods_get_all_ids( mods.push(id?); } - match mods.is_empty() { - true => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_MODS_ALL"))), - false => Ok(mods), + if mods.is_empty() { + Err(Box::new(Error::new(ErrorKind::NotFound, "NO_MODS_ALL"))) + } else { + Ok(mods) } } -///Get mod id based on the slug or name -///# Arguments +/// Get mod id based on the slug or name +/// # Arguments /// ///* `data` - file directory of the database ///* `slug` - Slug or Name of a mod /// -///# Failure +/// # Errors /// -///Will return `MLError` when no mod id is found +/// Will return `MLError` when no mod id is found pub fn mods_get_id(data: &str, slug: &str) -> MLE { let data = format!("{data}/data.db"); let connection = Connection::open(data)?; @@ -88,7 +91,7 @@ pub fn mods_get_id(data: &str, slug: &str) -> MLE { } if mod_id.is_empty() { - return Err(MLError::new(ErrorType::DBError, "GI_MOD_NOT_FOUND")); + return Err(MLErr::new(EType::DBError, "GI_MOD_NOT_FOUND")); }; Ok(mod_id) @@ -99,6 +102,7 @@ pub struct ModInfo { pub title: String, } +/// # Errors pub fn mods_get_info(config: &Cfg, id: &str) -> MLE { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -121,12 +125,14 @@ pub fn mods_get_info(config: &Cfg, id: &str) -> MLE { }); } - match mod_info.is_none() { - true => Err(MLError::new(ErrorType::DBError, "GN_MOD_NOT_FOUND")), - false => Ok(mod_info.unwrap()), + if mod_info.is_none() { + Err(MLErr::new(EType::DBError, "GN_MOD_NOT_FOUND")) + } else { + Ok(mod_info.ok_or(MLErr::new(EType::Other, "mod_info"))?) } } +/// # Errors pub fn mods_remove(config: &Cfg, id: &str) -> MLE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -142,15 +148,16 @@ pub struct DBModlistVersions { pub versions: String, } +/// # Errors pub fn mods_get_versions( config: &Cfg, - mods: Vec, + mods: &[String], ) -> MLE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; if mods.is_empty() { - return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_INPUT")); + return Err(MLErr::new(EType::ArgumentError, "MODS_NO_INPUT")); } let mut wherestr = String::from("WHERE"); @@ -182,35 +189,32 @@ pub fn mods_get_versions( }); } - match versionmaps.is_empty() { - true => Err(MLError::new(ErrorType::DBError, "MODS_MODS_NOT_FOUND")), - false => Ok(versionmaps), + if versionmaps.is_empty() { + Err(MLErr::new(EType::DBError, "MODS_MODS_NOT_FOUND")) + } else { + Ok(versionmaps) } } //userlist +/// # Errors pub fn userlist_insert( config: &Cfg, list_id: &str, mod_id: &str, current_version: &str, - applicable_versions: Vec, + applicable_versions: &[String], current_link: &str, set_version: bool, ) -> MLE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; - let sv = match set_version { - true => "1", - false => "0", - }; + let sv = if set_version { "1" } else { "0" }; connection.execute( - format!( - "INSERT INTO {list_id} VALUES (?1, ?2, ?3, ?4, 'NONE', ?5)" - ) - .as_str(), + format!("INSERT INTO {list_id} VALUES (?1, ?2, ?3, ?4, 'NONE', ?5)") + .as_str(), [ mod_id, current_version, @@ -223,28 +227,31 @@ pub fn userlist_insert( Ok(()) } +/// # Errors pub fn userlist_get_all_ids(config: &Cfg, list_id: &str) -> MLE> { let data = format!("{}/data.db", config.data); - let connection = Connection::open(data).unwrap(); + let connection = Connection::open(data)?; 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 { mod_ids.push(id?); } - match mod_ids.is_empty() { - true => Err(MLError::new( - ErrorType::DBError, + if mod_ids.is_empty() { + Err(MLErr::new( + EType::DBError, &format!("NO_MODS_USERLIST{list_id}"), - )), - false => Ok(mod_ids), + )) + } else { + Ok(mod_ids) } } +/// # Errors pub fn userlist_remove(config: &Cfg, list_id: &str, mod_id: &str) -> MLE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -256,20 +263,19 @@ pub fn userlist_remove(config: &Cfg, list_id: &str, mod_id: &str) -> MLE<()> { Ok(()) } +/// # Errors pub fn userlist_get_applicable_versions( config: &Cfg, - list_id: String, + list_id: &str, mod_id: String, ) -> MLE { let data = format!("{}/data.db", config.data); - let connection = Connection::open(data).unwrap(); + let connection = Connection::open(data)?; let mut version: String = String::new(); let mut stmt = connection.prepare( - format!( - "SELECT applicable_versions FROM {list_id} WHERE mod_id = ?" - ) - .as_str(), + format!("SELECT applicable_versions FROM {list_id} WHERE mod_id = ?") + .as_str(), )?; let ver_iter = stmt.query_map([mod_id], |row| row.get::(0))?; @@ -278,15 +284,17 @@ pub fn userlist_get_applicable_versions( version = ver?; } - match version.is_empty() { - true => Err(MLError::new(ErrorType::DBError, "GAV_MOD_NOT_FOUND")), - false => Ok(version), + if version.is_empty() { + Err(MLErr::new(EType::DBError, "GAV_MOD_NOT_FOUND")) + } else { + Ok(version) } } +/// # Errors pub fn userlist_get_all_applicable_versions_with_mods( config: &Cfg, - list_id: String, + list_id: &str, ) -> MLE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -308,19 +316,20 @@ pub fn userlist_get_all_applicable_versions_with_mods( } if versions.is_empty() { - return Err(MLError::new(ErrorType::DBError, "NO_MODS_ON_LIST")); + return Err(MLErr::new(EType::DBError, "NO_MODS_ON_LIST")); }; Ok(versions) } +/// # Errors 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 connection = Connection::open(data)?; let mut version: String = String::new(); let mut stmt = connection.prepare( @@ -334,15 +343,17 @@ pub fn userlist_get_current_version( version = ver?; } - match version.is_empty() { - true => Err(MLError::new(ErrorType::DBError, "GCV_MOD_NOT_FOUND")), - false => Ok(version), + if version.is_empty() { + Err(MLErr::new(EType::DBError, "GCV_MOD_NOT_FOUND")) + } else { + Ok(version) } } +/// # Errors pub fn userlist_get_all_current_version_ids( config: &Cfg, - list_id: String, + list_id: &str, ) -> MLE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -357,15 +368,16 @@ pub fn userlist_get_all_current_version_ids( } if versions.is_empty() { - return Err(MLError::new(ErrorType::DBError, "NO_MODS_ON_LIST")); + return Err(MLErr::new(EType::DBError, "NO_MODS_ON_LIST")); }; Ok(versions) } +/// # Errors pub fn userlist_get_all_current_versions_with_mods( config: &Cfg, - list_id: String, + list_id: &str, ) -> Result, Box> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -396,18 +408,18 @@ pub fn userlist_get_all_current_versions_with_mods( Ok(versions) } +/// # Errors 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 connection = Connection::open(data)?; let mut set_version: bool = false; let mut stmt = connection.prepare( - format!("SELECT set_version FROM {list_id} WHERE mod_id = ?") - .as_str(), + format!("SELECT set_version FROM {list_id} WHERE mod_id = ?").as_str(), )?; let ver_iter = stmt.query_map([&mod_id], |row| row.get::(0))?; @@ -419,9 +431,10 @@ pub fn userlist_get_set_version( Ok(set_version) } +/// # Errors pub fn userlist_change_versions( config: &Cfg, - list_id: String, + list_id: &str, current_version: String, versions: String, link: String, @@ -434,25 +447,22 @@ pub fn userlist_change_versions( Ok(()) } +/// # Errors pub fn userlist_add_disabled_versions( config: &Cfg, - list_id: String, + list_id: &str, disabled_version: String, mod_id: String, ) -> MLE<()> { 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 disabled_versions = match currently_disabled_versions == "NONE" { - true => disabled_version, - false => { - format!("{currently_disabled_versions}|{disabled_version}") - } + let currently_disabled_versions = + userlist_get_disabled_versions(config, list_id, String::from(&mod_id))?; + let disabled_versions = if currently_disabled_versions == "NONE" { + disabled_version + } else { + format!("{currently_disabled_versions}|{disabled_version}") }; connection.execute( @@ -465,13 +475,14 @@ pub fn userlist_add_disabled_versions( Ok(()) } +/// # Errors pub fn userlist_get_disabled_versions( config: &Cfg, - list_id: String, + list_id: &str, mod_id: String, ) -> MLE { let data = format!("{}/data.db", config.data); - let connection = Connection::open(data).unwrap(); + let connection = Connection::open(data)?; let mut version: String = String::new(); let mut stmt = connection.prepare( @@ -485,23 +496,24 @@ pub fn userlist_get_disabled_versions( version = ver?; } - match version.is_empty() { - true => Err(MLError::new(ErrorType::DBError, "GDV_MOD_NOT_FOUND")), - false => Ok(version), + if version.is_empty() { + Err(MLErr::new(EType::DBError, "GDV_MOD_NOT_FOUND")) + } else { + Ok(version) } } +/// # Errors pub fn userlist_get_all_downloads( config: &Cfg, - list_id: String, + list_id: &str, ) -> Result, Box> { let data = format!("{}/data.db", config.data); - let connection = Connection::open(data).unwrap(); + let connection = Connection::open(data)?; 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,6 +533,7 @@ pub fn userlist_get_all_downloads( //lists ///Inserts into lists table and creates new table +/// # Errors pub fn lists_insert( config: &Cfg, id: &str, @@ -540,6 +553,7 @@ pub fn lists_insert( Ok(()) } +/// # Errors pub fn lists_remove(config: &Cfg, id: &str) -> MLE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -549,9 +563,10 @@ pub fn lists_remove(config: &Cfg, id: &str) -> MLE<()> { Ok(()) } +/// # Errors pub fn lists_get(config: &Cfg, list_id: &str) -> MLE { let data = format!("{}/data.db", config.data); - let connection = Connection::open(data).unwrap(); + let connection = Connection::open(data)?; let mut list = List { id: String::new(), @@ -582,15 +597,16 @@ pub fn lists_get(config: &Cfg, list_id: &str) -> MLE { } if list.id.is_empty() { - return Err(MLError::new(ErrorType::DBError, "LIST_NOT_FOUND")); + return Err(MLErr::new(EType::DBError, "LIST_NOT_FOUND")); } Ok(list) } +/// # Errors pub fn lists_version(config: &Cfg, list_id: &str, version: &str) -> MLE<()> { let data = format!("{}/data.db", config.data); - let connection = Connection::open(data).unwrap(); + let connection = Connection::open(data)?; connection.execute( "UPDATE lists SET mc_version = ? WHERE id = ?", @@ -599,9 +615,10 @@ pub fn lists_version(config: &Cfg, list_id: &str, version: &str) -> MLE<()> { Ok(()) } +/// # Errors pub fn lists_get_all_ids(config: &Cfg) -> MLE> { let data = format!("{}/data.db", config.data); - let connection = Connection::open(data).unwrap(); + let connection = Connection::open(data)?; let mut list_ids: Vec = Vec::new(); let mut stmt = connection.prepare("SELECT id FROM lists")?; @@ -611,13 +628,15 @@ pub fn lists_get_all_ids(config: &Cfg) -> MLE> { list_ids.push(id?); } - match list_ids.is_empty() { - true => Err(MLError::new(ErrorType::DBError, "NO_LISTS")), - false => Ok(list_ids), + if list_ids.is_empty() { + Err(MLErr::new(EType::DBError, "NO_LISTS")) + } else { + Ok(list_ids) } } //config +/// # Errors pub fn config_change_current_list(config: &Cfg, id: &str) -> MLE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -629,9 +648,10 @@ pub fn config_change_current_list(config: &Cfg, id: &str) -> MLE<()> { Ok(()) } +/// # Errors pub fn config_get_current_list(config: &Cfg) -> MLE { let data = format!("{}/data.db", config.data); - let connection = Connection::open(data).unwrap(); + let connection = Connection::open(data)?; let mut list_id = String::new(); let mut stmt = connection @@ -643,16 +663,17 @@ pub fn config_get_current_list(config: &Cfg) -> MLE { } if list_id.is_empty() { - return Err(MLError::new(ErrorType::DBError, "NO_CURRENT_LIST")); + return Err(MLErr::new(EType::DBError, "NO_CURRENT_LIST")); } Ok(list_id) } //SETUP(UPDATES) +/// # Errors pub fn s_userlist_update_download( config: &Cfg, - list_id: String, + list_id: &str, mod_id: String, link: String, ) -> Result<(), Box> { @@ -660,15 +681,14 @@ pub fn s_userlist_update_download( let connection = Connection::open(data)?; connection.execute( - format!( - "UPDATE {list_id} SET current_download = ?1 WHERE mod_id = ?2" - ) - .as_str(), + format!("UPDATE {list_id} SET current_download = ?1 WHERE mod_id = ?2") + .as_str(), [link, mod_id], )?; Ok(()) } +/// # Errors pub fn s_config_create_version( config: &Cfg, ) -> Result<(), Box> { @@ -682,6 +702,7 @@ pub fn s_config_create_version( Ok(()) } +/// # Errors pub fn s_config_update_version( config: &Cfg, ver: String, @@ -696,6 +717,7 @@ pub fn s_config_update_version( Ok(()) } +/// # Errors pub fn s_config_get_version( config: &Cfg, ) -> Result> { @@ -720,11 +742,12 @@ pub fn s_config_get_version( Ok(version) } +/// # Errors pub fn s_insert_column( config: &Cfg, - table: String, - column: String, - c_type: String, + table: &str, + column: &str, + c_type: &str, default: Option, ) -> Result<(), Box> { let data = format!("{}/data.db", config.data); @@ -733,14 +756,19 @@ pub fn s_insert_column( let mut sql = format!("ALTER TABLE {table} ADD '{column}' {c_type}"); if default.is_some() { - sql = format!("{} DEFAULT {}", sql, default.unwrap()); + sql = format!( + "{} DEFAULT {}", + sql, + default.ok_or(MLErr::new(EType::Other, "errornous default"))? + ); } connection.execute(sql.as_str(), ())?; Ok(()) } -pub fn db_setup(path: &str) -> MLE<()> { +/// # Errors +pub fn setup(path: &str) -> MLE<()> { let connection = Connection::open(path)?; connection.execute_batch( -- cgit v1.2.3