summaryrefslogblamecommitdiff
path: root/src/input.rs
blob: c7e82d9b5eed2cded64bc42323df28878416ff89 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12

                                                                      
 
                               







                                                                             

                                                 
                                                    














                                                                                   



                                                                               





                                                      
 
                                             

                                  

                                                  
          


                                    


                                





                                  


                                                                                   






                                                                                                                                                                     
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<Vec<String>>,
}

impl Input {
    pub fn from(string: String) -> Result<Self, Box<dyn std::error::Error>> {
        let mut split: Vec<&str> = string.split(' ').collect();
        let command: String;
        let mut args: Option<Vec<String>> = 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<String> = 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<dyn std::error::Error>> {
    let mut args: Vec<String> = 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);
}