From 89193143f90e1b8cbb91445d14942fa39509acbb Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 9 Jan 2023 23:12:52 +0100 Subject: implemented more Error (dumb) --- src/db.rs | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index d823018..c5a972f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -2,7 +2,7 @@ use std::io::{Error, ErrorKind}; use rusqlite::Connection; -use crate::{Modloader, config::Cfg, List, devdir}; +use crate::{Modloader, config::Cfg, List, devdir, error::{MLE, MLError, ErrorType}}; //mods pub fn mods_insert(config: Cfg, id: String, name: String, versions: Vec) -> Result<(), Box> { @@ -81,7 +81,7 @@ pub fn mods_get_name(config: Cfg, id: String) -> Result Result<(), Box> { +pub fn mods_change_versions(config: Cfg, versions: String, mod_id: String) -> MLE<()> { println!("Updating versions for {} with \n {}", mod_id, versions); @@ -110,11 +110,11 @@ pub struct DBModlistVersions { pub versions: String, } -pub fn mods_get_versions(config: Cfg, mods: Vec) -> Result, Box> { +pub fn mods_get_versions(config: Cfg, mods: Vec) -> MLE> { let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; - if mods.is_empty() { return Err(Box::new(Error::new(ErrorKind::Other, "MODS_NO_INPUT"))); } + if mods.is_empty() { return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_INPUT")); } let mut wherestr = String::from("WHERE"); for (i, id) in mods.iter().enumerate() { @@ -137,7 +137,7 @@ pub fn mods_get_versions(config: Cfg, mods: Vec) -> Result Err(Box::new(Error::new(ErrorKind::NotFound, "MODS_MODS_NOT_FOUND"))), + true => Err(MLError::new(ErrorType::DBError, "MODS_MODS_NOT_FOUND")), false => Ok(versionmaps), } } @@ -155,7 +155,7 @@ pub fn userlist_insert(config: Cfg, list_id: String, mod_id: String, current_ver Ok(()) } -pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> Result, Box> { +pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> MLE> { let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); @@ -171,7 +171,7 @@ pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> Result, }; match mod_ids.is_empty() { - true => Err(Box::new(std::io::Error::new(ErrorKind::NotFound, "NO_MODS"))), + true => Err(MLError::new(ErrorType::DBError, "NO_MODS")), false => Ok(mod_ids), } } @@ -226,7 +226,7 @@ pub fn userlist_get_all_applicable_versions_with_mods(config: Cfg, list_id: Stri Ok(versions) } -pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String) -> Result> { +pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String) -> MLE { let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); @@ -241,7 +241,7 @@ pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String }; match version.is_empty() { - true => Err(Box::new(Error::new(ErrorKind::NotFound, "MOD_NOT_FOUND"))), + true => Err(MLError::new(ErrorType::DBError, "MOD_NOT_FOUND")), false => Ok(version), } } @@ -293,7 +293,7 @@ pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: S Ok(()) } -pub fn userlist_add_disabled_versions(config: Cfg, list_id: String, disabled_version: String, mod_id: String) -> Result<(), Box> { +pub fn userlist_add_disabled_versions(config: Cfg, list_id: String, disabled_version: String, mod_id: String) -> MLE<()> { let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; @@ -307,7 +307,7 @@ pub fn userlist_add_disabled_versions(config: Cfg, list_id: String, disabled_ver Ok(()) } -pub fn userlist_get_disabled_versions(config:Cfg, list_id: String, mod_id: String) -> Result> { +pub fn userlist_get_disabled_versions(config:Cfg, list_id: String, mod_id: String) -> MLE { let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); @@ -322,7 +322,7 @@ pub fn userlist_get_disabled_versions(config:Cfg, list_id: String, mod_id: Strin }; match version.is_empty() { - true => Err(Box::new(Error::new(ErrorKind::NotFound, "MOD_NOT_FOUND"))), + true => Err(MLError::new(ErrorType::DBError, "MOD_NOT_FOUND")), false => Ok(version), } } @@ -349,13 +349,13 @@ pub fn userlist_get_all_downloads(config: Cfg, list_id: String) -> Result Result<(), Box> { +pub fn lists_insert(config: Cfg, id: String, mc_version: String, mod_loader: Modloader, download_folder: String) -> MLE<()> { println!("Creating list {}", id); let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; - connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", [id.clone(), mc_version, mod_loader.stringify(), download_folder])?; + connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", [id.clone(), mc_version, mod_loader.to_string(), download_folder])?; connection.execute(format!("CREATE TABLE {}( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT, 'disabled_versions' TEXT DEFAULT 'NONE' )", id).as_str(), [])?; Ok(()) @@ -370,7 +370,7 @@ pub fn lists_remove(config: Cfg, id: String) -> Result<(), Box Result> { +pub fn lists_get(config: Cfg, list_id: String) -> MLE { let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); @@ -386,12 +386,12 @@ pub fn lists_get(config: Cfg, list_id: String) -> Result Result<(), Box> { +pub fn lists_version(config: Cfg, list_id: String, version: String) -> MLE<()> { let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); @@ -399,7 +399,7 @@ pub fn lists_version(config: Cfg, list_id: String, version: String) -> Result<() Ok(()) } -pub fn lists_get_all_ids(config: Cfg) -> Result, Box> { +pub fn lists_get_all_ids(config: Cfg) -> MLE> { let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); @@ -414,7 +414,7 @@ pub fn lists_get_all_ids(config: Cfg) -> Result, Box Err(Box::new(std::io::Error::new(ErrorKind::NotFound, "NO_LISTS"))), + true => Err(MLError::new(ErrorType::DBError, "NO_LISTS")), false => Ok(list_ids), } } @@ -428,7 +428,7 @@ pub fn config_change_current_list(config: Cfg, id: String) -> Result<(), Box Result> { +pub fn config_get_current_list(config: Cfg) -> MLE { let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); @@ -442,7 +442,7 @@ pub fn config_get_current_list(config: Cfg) -> Result