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

                                
                                                                                                                                                                                 
 
                                      



                             
                                
 
 
                                                                                  
 


                                              
          

                                                 
          

                                                 


                                                                                  
         


     
                                                                                  

                                                      

 

                                                                                  

                                                                                             

                                                    


                                                                             






                                                                                        
                                                   



                                                                                                                      
                                                    







                                                                                        
                                                          



                                                                                        
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, Subcmd}};

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

pub fn list(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {

    match input.subcommand.ok_or("")? {
        Subcmd::Add => {
            add(config, input.args.ok_or("")?)
        },
        Subcmd::Change => {
            change(config, input.args.ok_or("")?)
        },
        Subcmd::Remove => {
            remove(config, input.args.ok_or("")?)
        },
        _ => {
            Err(Box::new(Error::new(ErrorKind::InvalidInput, "WRONG_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 | 3 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))),
        4 => {
            let id = String::from(&args[0]);
            let mc_version = String::from(&args[1]);
            let mod_loader = Modloader::from(&args[2])?;
            let download_folder = String::from(&args[3]);
            lists_insert(config, id, mc_version, mod_loader, download_folder)
        },
        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"))); };
            config_change_current_list(config, list)
        },
        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 => lists_remove(config, String::from(&args[0])),
        2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
        _ => panic!("list arguments should never be zero or lower"),
    }
}