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 --- src/input.rs | 140 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 103 insertions(+), 37 deletions(-) (limited to 'src/input.rs') 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); } -- cgit v1.2.3