summaryrefslogtreecommitdiff
path: root/src/input.rs
blob: 09d05a1eb60a2df8bd46eb62f2e5591cef4fee25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::env;
use crate::{config::Cfg, list, modification, update, setup, download, io, error::{MLError, ErrorType, MLE}};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Input {
    pub command: Cmd,
    pub subcommand: Option<Subcmd>,
    pub args: Option<Vec<String>>,
    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<Self> {
        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<usize> = 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 {
            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<String> = 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 })
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Cmd {
    Mod,
    List,
    Update,
    Download,
    Setup,
    Io
}

impl Cmd {
    fn from(string: &str) -> MLE<Self> {
        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 Subcmd {
    Add,
    Remove,
    Change,
    Export,
    Import,
}

impl Subcmd {
    fn from(string: &str) -> MLE<Self> {
        let cmd = match string {
            "add" => Self::Add,
            "remove" => Self::Remove,
            "change" => Self::Change,
            "export" => Self::Export,
            "import" => Self::Import,
            _ => return Err(MLError::new(ErrorType::ArgumentError, "SUBCMD_NOT_FOUND"))
        };
        Ok(cmd)
    }
}

pub async fn get_input(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
    let mut args: Vec<String> = env::args().collect();
    args.reverse();
    args.pop();
    args.reverse();
    
    let input = Input::from(&args.join(" "))?;

    match input.command {
        Cmd::Mod => { 
            modification(config, input).await
        },
        Cmd::List => {
            list(config, input)
        },
        Cmd::Update => {
            update(config, input).await
        },
        Cmd::Setup => {
            setup(config).await
        },
        Cmd::Download => {
            download(config, input).await
        },
        Cmd::Io => {
            io(config, input).await
        }
    }
}

#[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 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);
}