From 30c1ffcecb541d9b27982df6af26948514cbadaa Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 15 Jan 2023 16:58:45 +0100 Subject: started implementation of new input system --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/list.rs | 16 ++- src/commands/mod.rs | 20 +-- src/input.rs | 363 +++++++++++++++++++++++++++++++-------------------- src/lib.rs | 6 +- src/main.rs | 9 +- 7 files changed, 255 insertions(+), 163 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dadb2e1..cf7495b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -615,7 +615,7 @@ dependencies = [ [[package]] name = "modlist" -version = "0.8.1" +version = "0.9.0" dependencies = [ "chrono", "dirs", diff --git a/Cargo.toml b/Cargo.toml index 76b31d4..9d85556 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "modlist" -version = "0.8.1" +version = "0.9.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/commands/list.rs b/src/commands/list.rs index 3998cce..2fec1c7 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,6 +1,6 @@ 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, lists_version}, Modloader, config::Cfg, input::{Input, Subcmd}, cmd_update, error::{MLE, ErrorType, MLError}, modrinth::MCVersionType}; +use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, ListOptions}, /*cmd_update,*/ error::{MLE, ErrorType, MLError}, /*modrinth::MCVersionType*/}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct List { @@ -9,20 +9,20 @@ pub struct List { pub modloader: Modloader, pub download_folder: String, } - +/* pub async fn list(config: Cfg, input: Input) -> Result<(), Box> { - match input.subcommand.ok_or("")? { - Subcmd::Add => { + match input.list_options.ok_or("")? { + ListOptions::Add => { match add(config, input.args.ok_or("")?) { Ok(..) => Ok(()), Err(e) => Err(Box::new(e)) } }, - Subcmd::Change => { + ListOptions::Change => { change(config, input.args) }, - Subcmd::Remove => { + ListOptions::Remove => { match remove(config, input.args.ok_or("")?) { Ok(..) => Ok(()), Err(e) => Err(Box::new(e)) @@ -39,7 +39,7 @@ pub async fn list(config: Cfg, input: Input) -> Result<(), Box MLE { let id = config_get_current_list(config.clone())?; lists_get(config, id) @@ -83,6 +83,7 @@ fn remove(config: Cfg, args: Vec) -> MLE<()> { } } +/* ///Changing the current lists version and updating it /// #Arguments /// @@ -95,3 +96,4 @@ async fn version(config: Cfg, args: Option>, version_type: Option, - pub args: Option>, - pub direct_download: bool, - pub all_lists: bool, - pub delete_old: bool, - pub clean: bool, - pub disable_download: bool, - pub version: bool, -} - -impl Input { - fn from(string: &str) -> MLE { - let mut split: Vec<&str> = string.split(' ').collect(); - - let mut direct_download = false; - let mut all_lists = false; - let mut delete_old = false; - let mut clean = false; - let mut disable_download = false; - let mut version = false; - - let mut toremove: Vec = vec![]; - for (i, input) in split.clone().into_iter().enumerate() { - if input.starts_with("--") { - match input { - "--direct-download" => direct_download = true, - "--all-lists" => all_lists = true, - "--delete-old" => delete_old = true, - "--clean" => clean = true, - "--disable-download" => disable_download = true, - "--version" => version = true, - _ => continue, - } - toremove.push(i) - } - } - - for rem in toremove.into_iter().rev() { - split.remove(rem); - } - - if version { - match std::env::var("DEV") { - Ok(dev) => { - let devint = dev.parse::().unwrap(); - if devint >= 1 { - println!("Modlist by FxQnLr v{} (DEV)", env!("CARGO_PKG_VERSION")); - } else { - println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION")); - } - }, - Err(..) => println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION")), - } - - std::process::exit(0); - } - - 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, all_lists, delete_old, clean, disable_download, version }) - } + pub command: Option, + pub download: bool, + pub update: bool, + pub mod_options: Option, + pub mod_id: Option, + pub mod_version: Option, + pub list: Option, + pub list_options: Option, + pub list_id: Option, + pub list_mcversion: Option, + pub modloader: Option, + pub directory: Option, + pub export: bool, + pub import: bool, + pub file: Option, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -88,90 +25,236 @@ pub enum Cmd { List, Update, Download, - Setup, - Io + Io, } -impl Cmd { - fn from(string: &str) -> MLE { - let cmd = match string { - "mod" => Self::Mod, - "list" => Self::List, - "update" => Self::Update, - "download" => Self::Download, - "setup" => Self::Setup, - "io" => Self::Io, - _ => return Err(MLError::new(ErrorType::ArgumentError, "Unknown command")) - }; - Ok(cmd) - } +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ModOptions { + Add, + Remove } #[derive(Debug, Clone, PartialEq, Eq)] -pub enum Subcmd { +pub enum ListOptions { Add, Remove, Change, - Version, - Export, - Import, } -impl Subcmd { - fn from(string: &str) -> MLE { - let cmd = match string { - "add" => Self::Add, - "remove" => Self::Remove, - "change" => Self::Change, - "version" => Self::Version, - "export" => Self::Export, - "import" => Self::Import, - _ => return Err(MLError::new(ErrorType::ArgumentError, "SUBCMD_NOT_FOUND")) - }; - Ok(cmd) +impl Input { + fn from(config: Cfg, input: Vec) -> MLE { + let input_string = input.join(" "); + let mut args: Vec<&str> = input_string.split('-').collect(); + args.reverse(); + args.pop(); + args.reverse(); + + let mut command: Option = None; + + let mut download = false; + let mut update = false; + let mut mod_options: Option = None; + let mut mod_id: Option = None; + let mut mod_version: Option = None; + let mut list: Option = None; + let mut list_options: Option = None; + let mut list_id: Option = None; + let mut list_mcversion: Option = None; + let mut modloader: Option = None; + let mut directory: Option = None; + let mut export = false; + let mut import = false; + let mut file: Option = None; + + for arg in args { + let arg_split: Vec<&str> = arg.trim().split(" ").collect(); + match arg_split[0] { + "v" | "version" => { show_version(); }, + "d" | "download" => { + command = Some(Cmd::Download); + }, + "u" | "update" => { + command = Some(Cmd::Update); + } + "ma" => { + command = Some(Cmd::Mod); + mod_options = Some(ModOptions::Add); + mod_id = Some(String::from(arg_split[1])); + }, + "l" => { + list = Some(lists_get(config.clone(), String::from(arg_split[1]))?); + } + "la" => { + command = Some(Cmd::List); + list_options = Some(ListOptions::Add); + list_id = Some(String::from(arg_split[1])); + }, + "lr" => { + command = Some(Cmd::List); + list_options = Some(ListOptions::Remove); + list_id = Some(String::from(arg_split[1])); + }, + "lc" => { + command = Some(Cmd::List); + list_options = Some(ListOptions::Change); + list_id = Some(String::from(arg_split[1])); + }, + "lv" => { + list_mcversion = Some(String::from(arg_split[1])); + } + _ => return Err(MLError::new(ErrorType::ArgumentError, "UnknownArgument")), + } + } + + Ok(Self { + command, + download, + update, + mod_options, + mod_id, + mod_version, + list, + list_options, + list_id, + list_mcversion, + modloader, + directory, + export, + import, + file + }) } } -pub async fn get_input(config: Cfg) -> Result<(), Box> { - let mut args: Vec = env::args().collect(); - args.reverse(); - args.pop(); - args.reverse(); - - let input = Input::from(&args.join(" "))?; - - match input.command { - Cmd::Mod => { - modification(config, input).await +fn show_version() { + match std::env::var("DEV") { + Ok(dev) => { + let devint = dev.parse::().unwrap(); + if devint >= 1 { + println!("Modlist by FxQnLr v{} (DEV)", env!("CARGO_PKG_VERSION")); + } else { + println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION")); + } }, - Cmd::List => { - list(config, input).await + Err(..) => println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION")), + } + std::process::exit(0); +} + +pub async fn get_input(config: Cfg, args: Vec) -> MLE { + let input = Input::from(config.clone(), args)?; + + match input.clone().command.unwrap() { + Cmd::Mod => check_mod(input, config), + Cmd::List => check_list(input), + Cmd::Update => check_update(input), + Cmd::Download => check_download(input), + Cmd::Io => check_io(input), + } +} + +fn check_mod(mut input: Input, config: Cfg) -> MLE { + print!("Checkmod"); + if input.mod_options.is_none() { + return Err(MLError::new(ErrorType::ArgumentError, "NO_MOD_ARGUMENT")); + }; + match input.clone().mod_options.unwrap() { + ModOptions::Add => { + print!("Checkadd"); + if input.mod_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_MODID")); }; + if input.list_id.is_none() { println!("NOLIST"); input.list = Some(get_current_list(config.clone())?); }; + Ok(input) }, - Cmd::Update => { - match update(config, input).await { - Ok(..) => Ok(()), - Err(..) => Err(Box::new(MLError::new(ErrorType::Other, "UPDATE_ERR"))) - } + ModOptions::Remove => { + if input.mod_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_MODID")); }; + Ok(input) }, - Cmd::Setup => { - setup(config).await + } +} + +fn check_list(mut input: Input) -> MLE { + if input.list_options.is_none() { + return Err(MLError::new(ErrorType::ArgumentError, "NO_LIST_ARGUMENT")); + }; + match input.clone().list_options.unwrap() { + ListOptions::Add => { + if input.list_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "LISTS_NO_ID")); }; + if input.list_mcversion.is_none() { /*TODO Get latest version */ input.list_mcversion = Some(String::from("1.19.3")) }; + if input.modloader.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "LISTS_NO_MODLOADER")); }; + if input.directory.is_none() { input.directory = Some(format!("./downloads/{}", input.clone().list_id.expect("earlier if failed"))) }; + Ok(input) }, - Cmd::Download => { - download(config, input).await + ListOptions::Remove => { + if input.list_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "LISTS_NO_ID")); }; + Ok(input) }, - Cmd::Io => { - io(config, input).await + ListOptions::Change => { + //TODO check if no change + if input.list_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "LISTS_NO_ID")); }; + Ok(input) } } } +fn check_update(input: Input) -> MLE { + Ok(input) +} + +fn check_download(input: Input) -> MLE { + Ok(input) +} + +fn check_io(input: Input) -> MLE { + Ok(input) +} + #[test] fn input_from() { - let string = "list 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")]), direct_download: false, all_lists: false, clean: false, delete_old: false, disable_download: false, version: false }; - assert_eq!(Input::from(string).unwrap(), input); + let config = Cfg::init("modlist.toml").unwrap(); + assert_eq!( + Input::from(config.clone(), vec![String::from("-la test -lv 1.19.3")]).unwrap(), + Input { + command: Some(Cmd::List), + download: false, + update: false, + mod_options: None, + mod_id: None, + mod_version: None, + list: None, + list_options: Some(ListOptions::Add), + list_id: Some(String::from("test")), + list_mcversion: Some(String::from("1.19.3")), + modloader: None, + directory: None, + export: false, + import: false, + file: None + } + ); - let string = "update --direct-download --delete-old"; - let input = Input{ command: Cmd::Update, subcommand: None, args: None, direct_download: true, all_lists: false, clean: false, delete_old: true, disable_download: false, version: false }; - assert_eq!(Input::from(string).unwrap(), input); +} + +#[tokio::test] +async fn get_input_test() { + let config = Cfg::init("modlist.toml").unwrap(); + assert_eq!( + get_input(config.clone(), vec![String::from("-ma test")]).await.unwrap(), + Input { + command: Some(Cmd::Mod), + download: false, + update: false, + mod_options: Some(ModOptions::Add), + mod_id: Some(String::from("test")), + mod_version: None, + list: Some(lists_get(config.clone(), String::from("one")).unwrap()), + list_options: None, + list_id: None, + list_mcversion: None, + modloader: None, + directory: None, + export: false, + import: false, + file: None + } + ) } diff --git a/src/lib.rs b/src/lib.rs index eb845d8..ef97a53 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,14 @@ -pub mod apis; +//pub mod apis; pub mod config; pub mod commands; pub mod input; pub mod db; pub mod error; -pub mod files; +//pub mod files; use std::path::Path; -pub use apis::*; +//pub use apis::*; pub use commands::*; use error::{MLE, ErrorType, MLError}; diff --git a/src/main.rs b/src/main.rs index 59d41c5..39e46e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,17 @@ +use std::env; + use modlist::{config::Cfg, input::get_input}; #[tokio::main] async fn main() { let config = Cfg::init("modlist.toml").unwrap(); - match get_input(config).await { + let mut args: Vec = env::args().collect(); + args.reverse(); + args.pop(); + args.reverse(); + + match get_input(config, args).await { Ok(..) => (), Err(e) => { println!("{}", e); -- cgit v1.2.3