summaryrefslogtreecommitdiff
path: root/src/commands/list.rs
blob: 6c260ce598a8a8571c27c6a1b62c071167ed8d27 (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
use std::io::{Error, ErrorKind};

use crate::{db::{insert_list, remove_list, change_list, get_lists, get_current_list}, Modloader, config::Cfg, input::Input};

pub fn list(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {

    if args.is_none() {
        let lists = get_lists(config.clone())?;
        let current_list = get_current_list(config)?;
        println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list);
        return Ok(());
    }

    let arguments = Input::from(args.unwrap().join(" "))?;

    if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };

    match arguments.command.as_str() {
        "add" => {
            add(config, arguments.args.unwrap())
        },
        "change" => {
            change(config, arguments.args.unwrap())
        },
        "remove" => {
            remove(config, arguments.args.unwrap())
        },
        _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND")))
    }
}

fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
    match args.len() {
        1 | 2 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))),
        3 => {
            let id = String::from(&args[0]);
            let mc_version = String::from(&args[1]);
            let mod_loader = match args[2].as_str() {
                "forge" => Modloader::Forge,
                "fabric" => Modloader::Fabric,
                _ => return Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_MODLOADER")))
            };
            match insert_list(config, id, mc_version, mod_loader) {
                Err(err) => { Err(Box::new(err)) },
                Ok(()) => Ok(()),
            }
        },
        5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
        _ => panic!("list arguments should never be zero or lower"),
    }
}

fn change(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
    let lists = get_lists(config.clone())?;
    match args.len() {
        1 => {
            let list = String::from(&args[0]);
            if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); };
            match change_list(config, list) {
                Err(err) => { Err(Box::new(err)) },
                Ok(()) => Ok(()),
            }
        },
        2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
        _ => panic!("list arguments should never be zero or lower"),
    }
}

fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
    match args.len() {
        1 => {
            match remove_list(config, String::from(&args[0])) {
                Err(err) => { Err(Box::new(err)) },
                Ok(()) => Ok(()),
            }
        },
        2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
        _ => panic!("list arguments should never be zero or lower"),
    }
}