summaryrefslogblamecommitdiff
path: root/src/commands/list.rs
blob: 6c80e4e675f5f8d27f848dc78f3638a757eeb516 (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                                
                                                                                                                                                                       
 
                                      




                             



                                                                                               

                                                            





















                                                                                                                    
                                                                                  

                                                      

 










                                                                                                   
                                                            






                                                                                        
                                                   



                                                                                                                      

                                                                                 










                                                                                        

                                                                                 






                                                                                        
use std::io::{Error, ErrorKind};

use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get}, Modloader, config::Cfg, input::Input};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct List {
    pub id: String,
    pub mc_version: String,
    pub modloader: Modloader,
}

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

    if args.is_none() {
        let lists = lists_get_all_ids(config.clone())?;
        let current_list = config_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")))
    }
}

pub fn get_current_list(config: Cfg) -> Result<List, Box<dyn std::error::Error>> {
    let id = config_get_current_list(config.clone())?;
    lists_get(config, id)
}

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")))
            };
            lists_insert(config, id, mc_version, mod_loader)
        },
        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 = lists_get_all_ids(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 config_change_current_list(config, list) {
                Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "72"))) },
                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 lists_remove(config, String::from(&args[0])) {
                Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "85"))) },
                Ok(()) => Ok(()),
            }
        },
        2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
        _ => panic!("list arguments should never be zero or lower"),
    }
}