use std::{io::{Error, ErrorKind}, env}; use crate::{config::Cfg, list, modification, update, setup, download}; #[derive(Debug, PartialEq, Eq)] pub struct Input { pub command: String, pub args: Option>, } impl Input { pub fn from(string: String) -> Result> { 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()); } args = Some(str_args); Ok(Input { command, args }) }, _ => { panic!("This should never happen") } } } } 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(" "))?; match input.command.as_str() { "mod" => { modification(config, input.args).await }, "list" => { list(config, input.args) }, "update" => { update(config).await }, "setup" => { setup(config).await }, "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")]) }; assert_eq!(Input::from(string).unwrap(), input); }