From fdd7525e5a0d298ebb8a9aa81cc19ec79e8cd113 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 17 Nov 2022 21:20:09 +0100 Subject: added --clean for update && list downloadfolder --- data.db | Bin 24576 -> 24576 bytes planmodlist.xopp | Bin 198243 -> 197609 bytes src/apis/modrinth.rs | 9 +-- src/commands/list.rs | 58 ++++++------------ src/commands/modification.rs | 31 +++------- src/commands/setup.rs | 11 +++- src/commands/update.rs | 35 +++++++---- src/db.rs | 21 ++++--- src/error.rs | 34 +++++++++++ src/input.rs | 140 +++++++++++++++++++++++++++++++------------ src/lib.rs | 14 ++--- tests/db.rs | 5 +- 12 files changed, 217 insertions(+), 141 deletions(-) create mode 100644 src/error.rs diff --git a/data.db b/data.db index 5308bbc..a54cd80 100644 Binary files a/data.db and b/data.db differ diff --git a/planmodlist.xopp b/planmodlist.xopp index 277f1ad..7dbf105 100644 Binary files a/planmodlist.xopp and b/planmodlist.xopp differ diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index abb8eec..ec8d203 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs @@ -124,7 +124,6 @@ async fn get(api: String, path: String) -> Result, Box Project { let url = format!("project/{}", name); let data = get(api, url); @@ -143,7 +142,6 @@ pub async fn projects(api: String, ids: Vec) -> Vec { } pub async fn versions(api: String, id: String, list: List) -> Vec { - let loaderstr = match list.modloader { Modloader::Forge => String::from("forge"), Modloader::Fabric => String::from("fabric"), @@ -157,7 +155,7 @@ pub async fn versions(api: String, id: String, list: List) -> Vec { } pub async fn get_raw_versions(api: String, versions: Vec) -> Vec { - println!("Getting versions"); + println!("Getting versions {}", &versions.join(", ")); let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); @@ -169,18 +167,15 @@ pub async fn get_raw_versions(api: String, versions: Vec) -> Vec) -> Result> { match versions.len() { 0 => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_VERSIONS_AVAILABLE"))), - //TODO compare publish dates 1.. => { let mut times: Vec<(String, DateTime)> = vec![]; for ver in versions { let stamp = DateTime::parse_from_rfc3339(&ver.date_published)?; times.push((ver.id, stamp)) } - dbg!(×); times.sort_by_key(|t| t.1); times.reverse(); - dbg!(×); - println!("CW: {}", times[0].0); + println!("Current Version: {}", times[0].0); Ok(times[0].0.to_string()) }, _ => panic!("available_versions should never be negative"), diff --git a/src/commands/list.rs b/src/commands/list.rs index 6c80e4e..76965df 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,38 +1,27 @@ use std::io::{Error, ErrorKind}; -use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get}, Modloader, config::Cfg, input::Input}; +use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get}, Modloader, config::Cfg, input::{Input, Subcmd}}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct List { pub id: String, pub mc_version: String, pub modloader: Modloader, + pub download_folder: String, } -pub fn list(config: Cfg, args: Option>) -> Result<(), Box> { +pub fn list(config: Cfg, input: Input) -> Result<(), Box> { - if args.is_none() { - let lists = lists_get_all_ids(config.clone())?; - let current_list = config_get_current_list(config)?; - println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list); - return Ok(()); - } - - let arguments = Input::from(args.unwrap().join(" "))?; - - if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; - - match arguments.command.as_str() { - "add" => { - add(config, arguments.args.unwrap()) + match input.subcommand.ok_or("")? { + Subcmd::Add => { + add(config, input.args.ok_or("")?) }, - "change" => { - change(config, arguments.args.unwrap()) + Subcmd::Change => { + change(config, input.args.ok_or("")?) }, - "remove" => { - remove(config, arguments.args.unwrap()) - }, - _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND"))) + Subcmd::Remove => { + remove(config, input.args.ok_or("")?) + } } } @@ -43,16 +32,13 @@ pub fn get_current_list(config: Cfg) -> Result> fn add(config: Cfg, args: Vec) -> Result<(), Box> { match args.len() { - 1 | 2 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))), - 3 => { + 1 | 2 | 3 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))), + 4 => { let id = String::from(&args[0]); let mc_version = String::from(&args[1]); - let mod_loader = match args[2].as_str() { - "forge" => Modloader::Forge, - "fabric" => Modloader::Fabric, - _ => return Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_MODLOADER"))) - }; - lists_insert(config, id, mc_version, mod_loader) + let mod_loader = Modloader::from(&args[2])?; + let download_folder = String::from(&args[3]); + lists_insert(config, id, mc_version, mod_loader, download_folder) }, 5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), _ => panic!("list arguments should never be zero or lower"), @@ -65,10 +51,7 @@ fn change(config: Cfg, args: Vec) -> Result<(), Box { let list = String::from(&args[0]); if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); }; - match config_change_current_list(config, list) { - Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "72"))) }, - Ok(()) => Ok(()), - } + config_change_current_list(config, list) }, 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), _ => panic!("list arguments should never be zero or lower"), @@ -77,12 +60,7 @@ fn change(config: Cfg, args: Vec) -> Result<(), Box) -> Result<(), Box> { match args.len() { - 1 => { - match lists_remove(config, String::from(&args[0])) { - Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "85"))) }, - Ok(()) => Ok(()), - } - }, + 1 => lists_remove(config, String::from(&args[0])), 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), _ => panic!("list arguments should never be zero or lower"), } diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 595b677..519a0cb 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,23 +1,17 @@ use std::io::{Error, ErrorKind}; -use crate::{modrinth::{project, versions, extract_current_version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::Input, get_current_list}; +use crate::{modrinth::{project, versions, extract_current_version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::{Input, Subcmd}, get_current_list}; -pub async fn modification(config: Cfg, args: Option>) -> Result<(), Box> { +pub async fn modification(config: Cfg, input: Input) -> Result<(), Box> { - if args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) } - - let arguments = Input::from(args.unwrap().join(" "))?; - - if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; - - match arguments.command.as_str() { - "add" => { - add(config, arguments.args.unwrap()).await + match input.subcommand.ok_or("")? { + Subcmd::Add => { + add(config, input.args.ok_or("")?).await }, - "remove" => { - remove(config, arguments.args.unwrap()) + Subcmd::Remove => { + remove(config, input.args.ok_or("")?) }, - _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND"))) + _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "SUBCOMMAND_NOT_AVAILABLE"))) } } @@ -29,8 +23,6 @@ async fn add(config: Cfg, args: Vec) -> Result<(), Box) -> Result<(), Box { - dbg!(&mods); if mods.contains(&project.id) { return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } else { @@ -60,7 +51,6 @@ async fn add(config: Cfg, args: Vec) -> Result<(), Box { - dbg!(&mods); if mods.contains(&project.id) { return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) } else { @@ -81,8 +71,5 @@ fn remove(config: Cfg, args: Vec) -> Result<(), Box { Err(Box::new(Error::new(ErrorKind::Other, "TBD"))) }, - Ok(()) => Ok(()), - } + userlist_remove(config, current_list.id, mod_id) } diff --git a/src/commands/setup.rs b/src/commands/setup.rs index 8c0fcfd..be06040 100644 --- a/src/commands/setup.rs +++ b/src/commands/setup.rs @@ -1,9 +1,8 @@ use std::{fs::File, path::Path, io::{Error, ErrorKind}}; -use crate::{config::Cfg, db::{db_setup, s_config_get_version, s_config_create_version, s_insert_column, lists_get_all_ids, lists_get, userlist_get_all_current_version_ids, s_userlist_update_download}, modrinth::get_raw_versions}; +use crate::{config::Cfg, db::{db_setup, s_config_get_version, s_config_create_version, s_insert_column, lists_get_all_ids, lists_get, userlist_get_all_current_version_ids, s_userlist_update_download, s_config_update_version}, modrinth::get_raw_versions}; pub async fn setup(config: Cfg) -> Result<(), Box> { - let db_file = format!("{}/data.db", String::from(&config.data)); if !Path::new(&db_file).exists() { @@ -13,6 +12,7 @@ pub async fn setup(config: Cfg) -> Result<(), Box> { match s_config_get_version(config.clone()) { Ok(ver) => { match ver.as_str() { + "0.2" => to_03(config)?, _ => return Err(Box::new(Error::new(ErrorKind::Other, "UNKNOWN_VERSION"))) } }, @@ -33,7 +33,7 @@ async fn to_02(config: Cfg) -> Result<(), Box> { for list in lists { println!("Updating {}", list); - s_insert_column(config.clone(), String::from(&list), String::from("current_download"), String::new())?; + s_insert_column(config.clone(), String::from(&list), String::from("current_download"), String::from("TEXT"))?; let full_list = lists_get(config.clone(), String::from(&list))?; @@ -51,3 +51,8 @@ async fn to_02(config: Cfg) -> Result<(), Box> { Ok(()) } + +fn to_03(config: Cfg) -> Result<(), Box> { + s_insert_column(config.clone(), String::from("lists"), String::from("download_folder"), String::from("TEXT"))?; + s_config_update_version(config, String::from("0.3")) +} diff --git a/src/commands/update.rs b/src/commands/update.rs index e383eae..85630f5 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -4,9 +4,9 @@ use reqwest::Client; use futures_util::StreamExt; -use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions}, List}; +use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions}, List, input::Input}; -pub async fn update(config: Cfg) -> Result<(), Box> { +pub async fn update(config: Cfg, input: Input) -> Result<(), Box> { let current_list = get_current_list(config.clone())?; @@ -20,15 +20,16 @@ pub async fn update(config: Cfg) -> Result<(), Box> { let mut updatestack: Vec = vec![]; for (index, project) in projects.into_iter().enumerate() { + //Get versions for project and check if they match up let current_version = &versions[index]; - let p_id = String::from(&project.id); let v_id = ¤t_version.mod_id; - if &p_id != v_id { return Err(Box::new(Error::new(ErrorKind::Other, "SORTING_ERROR"))) }; - - if project.versions.join("|") != current_version.versions { - updatestack.push(match specific_update(config.clone(), current_list.clone(), project).await { + + + //Adding to stack if not the same versions in the list OR if clean == true + if input.clone().clean || (project.versions.join("|") != current_version.versions) { + updatestack.push(match specific_update(config.clone(), input.clone(), current_list.clone(), project).await { Ok(ver) => ver, //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") Err(_) => { continue; }, @@ -36,13 +37,23 @@ pub async fn update(config: Cfg) -> Result<(), Box> { }; }; //println!("{:?}", updatestack); + + + if input.clean { + let dl_path = ¤t_list.download_folder; + println!("Cleaning {}", dl_path); + for entry in std::fs::read_dir(dl_path)? { + let entry = entry?; + std::fs::remove_file(entry.path())?; + } + } - download_updates(config, updatestack).await?; + download_updates(config, current_list, updatestack).await?; Ok(()) } -async fn specific_update(config: Cfg, list: List, project: Project) -> Result> { +async fn specific_update(config: Cfg, input: Input, list: List, project: Project) -> Result> { print!("Checking update for '{}' in {}", project.title, list.id); let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; @@ -54,7 +65,7 @@ async fn specific_update(config: Cfg, list: List, project: Project) -> Result = vec![]; - if versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))? { + if input.clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { //get new versions print!(" | getting new version"); let current_str = extract_current_version(applicable_versions.clone())?; @@ -71,9 +82,9 @@ async fn specific_update(config: Cfg, list: List, project: Project) -> Result) -> Result> { +async fn download_updates(config: Cfg, current_list: List, versions: Vec) -> Result> { - let dl_path = String::from(&config.downloads); + let dl_path = String::from(¤t_list.download_folder); for ver in versions { let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); diff --git a/src/db.rs b/src/db.rs index 5d82271..6b1e3ab 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, get_modloader}; +use crate::{Modloader, config::Cfg, List}; //mods pub fn mods_insert(config: Cfg, id: String, name: String, versions: Vec) -> Result<(), Box> { @@ -226,14 +226,14 @@ 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) -> Result<(), Box> { println!("Creating list {}", id); let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; - connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3)", [id.clone(), mc_version, mod_loader.stringify()])?; - connection.execute(format!("CREATE TABLE {}( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT)", id).as_str(), [])?; + connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", [id.clone(), mc_version, mod_loader.stringify(), download_folder])?; + connection.execute(format!("CREATE TABLE {}( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT )", id).as_str(), [])?; Ok(()) } @@ -251,16 +251,16 @@ pub fn lists_get(config: Cfg, list_id: String) -> Result(0)?, row.get::(1)?]) + Ok(vec![row.get::(0)?, row.get::(1)?, row.get::(2)?]) })?; for l in list_iter { let li = l?; - list = List { id: String::from(&list_id), mc_version: String::from(&li[0]), modloader: get_modloader(String::from(&li[1]))? }; + list = List { id: String::from(&list_id), mc_version: String::from(&li[0]), modloader: Modloader::from(&li[1])?, download_folder: String::from(&li[2]) }; }; if list.id.is_empty() { return Err(Box::new(Error::new(ErrorKind::Other, "LIST_NOT_FOUND"))); } @@ -378,11 +378,10 @@ pub fn db_setup(config: Cfg) -> Result<(), Box> { connection.execute_batch( "CREATE TABLE 'user_config' ( 'id' TEXT, 'value' TEXT ); CREATE TABLE 'mods' ( 'id' TEXT, 'name' TEXT, 'versions' TEXT ); - CREATE TABLE 'lists' ( 'id' TEXT, 'mc_version' TEXT, 'modloader' TEXT ); - INSERT INTO 'user_config' VALUES ( 'db_version', '0.2' ); + CREATE TABLE 'lists' ( 'id' TEXT, 'mc_version' TEXT, 'modloader' TEXT, 'download_folder' TEXT ); + INSERT INTO 'user_config' VALUES ( 'db_version', '0.3' ); INSERT INTO 'user_config' VALUES ( 'current_list', '...' )", )?; Ok(()) } - diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..c82688c --- /dev/null +++ b/src/error.rs @@ -0,0 +1,34 @@ +use core::fmt; + +pub type MLE = Result; + +#[derive(Debug)] +pub struct MLError { + etype: ErrorType, + message: String, +} + +#[derive(Debug)] +pub enum ErrorType { + ArgumentError +} + +impl std::error::Error for MLError { + fn description(&self) -> &str { + &self.message + } +} + +impl fmt::Display for MLError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.etype { + ErrorType::ArgumentError => write!(f, "ARGS") + } + } +} + +impl MLError { + pub fn new(etype: ErrorType, message: &str) -> Self { + Self { etype, message: String::from(message) } + } +} diff --git a/src/input.rs b/src/input.rs index c7e82d9..109fa0c 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,69 +1,135 @@ -use std::{io::{Error, ErrorKind}, env}; -use crate::{config::Cfg, list, modification, update, setup, download}; +use std::env; +use crate::{config::Cfg, list, modification, update, setup, download, error::{MLError, ErrorType, MLE}}; -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Input { - pub command: String, + pub command: Cmd, + pub subcommand: Option, pub args: Option>, + pub direct_download: bool, + pub force_download: bool, + pub all_lists: bool, + pub delete_old: bool, + pub clean: bool, } impl Input { - pub fn from(string: String) -> Result> { + pub fn from(string: &str) -> MLE { let mut split: Vec<&str> = string.split(' ').collect(); - let command: String; - let mut args: Option> = None; - if split[0].is_empty() { split.remove(0); }; - match split.len() { - 0 => { Err(Box::new(Error::new(ErrorKind::InvalidInput, "NO_ARGS"))) } - 1 => Ok( Input { command: split[0].to_string(), args }), - 2.. => { - command = split[0].to_string(); - split.remove(0); - let mut str_args: Vec = vec![]; - for e in split { - str_args.push(e.to_string()); + + let mut direct_download = false; + let mut force_download = false; + let mut all_lists = false; + let mut delete_old = false; + let mut clean = false; + + for (i, input) in split.clone().into_iter().enumerate() { + if input.starts_with("--") { + match input { + "--direct-download" => direct_download = true, + "--force-download" => force_download = true, + "--all_lists" => all_lists = true, + "--delete_old" => delete_old = true, + "--clean" => clean = true, + _ => continue, } - args = Some(str_args); - Ok(Input { command, args }) - }, - _ => { panic!("This should never happen") } + split.remove(i); + } } + + let command = Cmd::from(split.remove(0))?; + let subcommand = match split.is_empty() { + false => Some(Subcmd::from(split.remove(0))?), + true => None + }; + + let args = match split.is_empty() { + true => None, + false => { + let mut strsplit: Vec = Vec::new(); + for s in split { + strsplit.push(String::from(s)) + } + Some(strsplit) + } + }; + + Ok(Self { command, subcommand, args, direct_download, force_download, all_lists, delete_old, clean }) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Cmd { + Mod, + List, + Update, + Download, + Setup +} + +impl Cmd { + pub fn from(string: &str) -> MLE { + let cmd = match string { + "mod" => Self::Mod, + "list" => Self::List, + "update" => Self::Update, + "download" => Self::Download, + "setup" => Self::Setup, + _ => return Err(MLError::new(ErrorType::ArgumentError, "Unknown command")) + }; + Ok(cmd) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Subcmd { + Add, + Remove, + Change +} + +impl Subcmd { + fn from(string: &str) -> MLE { + let cmd = match string { + "add" => Self::Add, + "remove" => Self::Remove, + "change" => Self::Change, + _ => return Err(MLError::new(ErrorType::ArgumentError, "SUBCMD_NOT_FOUND")) + }; + Ok(cmd) } } pub async fn get_input(config: Cfg) -> Result<(), Box> { let mut args: Vec = env::args().collect(); - dbg!(&args); args.reverse(); args.pop(); args.reverse(); - dbg!(&args); - let input = Input::from(args.join(" "))?; + let input = Input::from(&args.join(" "))?; - match input.command.as_str() { - "mod" => { - modification(config, input.args).await + match input.command { + Cmd::Mod => { + modification(config, input).await }, - "list" => { - list(config, input.args) + Cmd::List => { + list(config, input) }, - "update" => { - update(config).await + Cmd::Update => { + update(config, input).await }, - "setup" => { + Cmd::Setup => { setup(config).await }, - "download" => { + Cmd::Download => { download(config).await - }, - _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_COMMAND"))), + } } } #[test] fn input_from() { - let string = String::from("list add test 1.19.2 fabric"); - let input = Input { command: String::from("list"), args: Some(vec![String::from("add"), String::from("test"), String::from("1.19.2"), String::from("fabric")]) }; + let string = "lis add test 1.19.2 fabric"; + let input = Input{ command: Cmd::List, subcommand: Some(Subcmd::Add), args: Some(vec![String::from("test"), String::from("1.19.2"), String::from("fabric")]), force_download: false, direct_download: false, all_lists: false }; assert_eq!(Input::from(string).unwrap(), input); } diff --git a/src/lib.rs b/src/lib.rs index 1e7ebbf..51b4487 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub mod config; pub mod commands; pub mod input; pub mod db; +pub mod error; use std::io::{Error, ErrorKind}; @@ -23,12 +24,11 @@ impl Modloader { } } -} - -pub fn get_modloader(string: String) -> Result> { - match string.as_str() { - "forge" => Ok(Modloader::Forge), - "fabric" => Ok(Modloader::Fabric), - _ => Err(Box::new(Error::new(ErrorKind::InvalidData, "UNKNOWN_MODLOADER"))) + fn from(string: &str) -> Result> { + match string { + "forge" => Ok(Modloader::Forge), + "fabric" => Ok(Modloader::Fabric), + _ => Err(Box::new(Error::new(ErrorKind::InvalidData, "UNKNOWN_MODLOADER"))) + } } } diff --git a/tests/db.rs b/tests/db.rs index 16061d0..7127fc6 100644 --- a/tests/db.rs +++ b/tests/db.rs @@ -1,4 +1,4 @@ -use std::{fs::{File, create_dir_all}, path::Path, sync::Once}; +use std::{fs::{File, create_dir_all, remove_dir_all}, path::Path, sync::Once}; use modlist::{config::{Cfg, Apis}, db::{mods_insert, db_setup, mods_get_all_ids, mods_get_id, mods_remove, userlist_insert, lists_insert, userlist_get_all_ids, userlist_remove, mods_get_versions, userlist_get_applicable_versions, lists_remove, lists_get, lists_get_all_ids, userlist_get_all_current_version_ids, userlist_change_versions, s_userlist_update_download, userlist_get_all_downloads, config_change_current_list, config_get_current_list, s_config_update_version, s_config_create_version, s_config_get_version}, Modloader, List}; @@ -11,6 +11,7 @@ fn setup() -> Cfg { INIT.call_once(|| { let db_path = Path::new(db_pathstr); + if db_path.exists() { remove_dir_all(db_pathstr).unwrap(); }; create_dir_all(db_path).unwrap(); let db_filestr = format!("{}/data.db", db_pathstr); File::create(db_filestr).unwrap(); @@ -46,7 +47,7 @@ fn test_mods_get_all_ids() { fn test_mods_get_id() { let config = setup(); - mods_insert(config.clone(), String::from("GI"), String::from("GETID_TEST"), vec![String::from("GI_VER1"), String::from("GI_VER2")]).unwrap(); + mods_insert(config, String::from("GI"), String::from("GETID_TEST"), vec![String::from("GI_VER1"), String::from("GI_VER2")]).unwrap(); } #[test] -- cgit v1.2.3