diff options
Diffstat (limited to 'src/commands/list.rs')
-rw-r--r-- | src/commands/list.rs | 58 |
1 files changed, 18 insertions, 40 deletions
diff --git a/src/commands/list.rs b/src/commands/list.rs index 6c80e4e..76965df 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs | |||
@@ -1,38 +1,27 @@ | |||
1 | use std::io::{Error, ErrorKind}; | 1 | use std::io::{Error, ErrorKind}; |
2 | 2 | ||
3 | 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}; | 3 | 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}}; |
4 | 4 | ||
5 | #[derive(Debug, Clone, PartialEq, Eq)] | 5 | #[derive(Debug, Clone, PartialEq, Eq)] |
6 | pub struct List { | 6 | pub struct List { |
7 | pub id: String, | 7 | pub id: String, |
8 | pub mc_version: String, | 8 | pub mc_version: String, |
9 | pub modloader: Modloader, | 9 | pub modloader: Modloader, |
10 | pub download_folder: String, | ||
10 | } | 11 | } |
11 | 12 | ||
12 | pub fn list(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> { | 13 | pub fn list(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { |
13 | 14 | ||
14 | if args.is_none() { | 15 | match input.subcommand.ok_or("")? { |
15 | let lists = lists_get_all_ids(config.clone())?; | 16 | Subcmd::Add => { |
16 | let current_list = config_get_current_list(config)?; | 17 | add(config, input.args.ok_or("")?) |
17 | println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list); | ||
18 | return Ok(()); | ||
19 | } | ||
20 | |||
21 | let arguments = Input::from(args.unwrap().join(" "))?; | ||
22 | |||
23 | if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; | ||
24 | |||
25 | match arguments.command.as_str() { | ||
26 | "add" => { | ||
27 | add(config, arguments.args.unwrap()) | ||
28 | }, | 18 | }, |
29 | "change" => { | 19 | Subcmd::Change => { |
30 | change(config, arguments.args.unwrap()) | 20 | change(config, input.args.ok_or("")?) |
31 | }, | 21 | }, |
32 | "remove" => { | 22 | Subcmd::Remove => { |
33 | remove(config, arguments.args.unwrap()) | 23 | remove(config, input.args.ok_or("")?) |
34 | }, | 24 | } |
35 | _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND"))) | ||
36 | } | 25 | } |
37 | } | 26 | } |
38 | 27 | ||
@@ -43,16 +32,13 @@ pub fn get_current_list(config: Cfg) -> Result<List, Box<dyn std::error::Error>> | |||
43 | 32 | ||
44 | fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { | 33 | fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { |
45 | match args.len() { | 34 | match args.len() { |
46 | 1 | 2 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))), | 35 | 1 | 2 | 3 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))), |
47 | 3 => { | 36 | 4 => { |
48 | let id = String::from(&args[0]); | 37 | let id = String::from(&args[0]); |
49 | let mc_version = String::from(&args[1]); | 38 | let mc_version = String::from(&args[1]); |
50 | let mod_loader = match args[2].as_str() { | 39 | let mod_loader = Modloader::from(&args[2])?; |
51 | "forge" => Modloader::Forge, | 40 | let download_folder = String::from(&args[3]); |
52 | "fabric" => Modloader::Fabric, | 41 | lists_insert(config, id, mc_version, mod_loader, download_folder) |
53 | _ => return Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_MODLOADER"))) | ||
54 | }; | ||
55 | lists_insert(config, id, mc_version, mod_loader) | ||
56 | }, | 42 | }, |
57 | 5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), | 43 | 5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), |
58 | _ => panic!("list arguments should never be zero or lower"), | 44 | _ => panic!("list arguments should never be zero or lower"), |
@@ -65,10 +51,7 @@ fn change(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Erro | |||
65 | 1 => { | 51 | 1 => { |
66 | let list = String::from(&args[0]); | 52 | let list = String::from(&args[0]); |
67 | if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); }; | 53 | if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); }; |
68 | match config_change_current_list(config, list) { | 54 | config_change_current_list(config, list) |
69 | Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "72"))) }, | ||
70 | Ok(()) => Ok(()), | ||
71 | } | ||
72 | }, | 55 | }, |
73 | 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), | 56 | 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), |
74 | _ => panic!("list arguments should never be zero or lower"), | 57 | _ => panic!("list arguments should never be zero or lower"), |
@@ -77,12 +60,7 @@ fn change(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Erro | |||
77 | 60 | ||
78 | fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { | 61 | fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { |
79 | match args.len() { | 62 | match args.len() { |
80 | 1 => { | 63 | 1 => lists_remove(config, String::from(&args[0])), |
81 | match lists_remove(config, String::from(&args[0])) { | ||
82 | Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "85"))) }, | ||
83 | Ok(()) => Ok(()), | ||
84 | } | ||
85 | }, | ||
86 | 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), | 64 | 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), |
87 | _ => panic!("list arguments should never be zero or lower"), | 65 | _ => panic!("list arguments should never be zero or lower"), |
88 | } | 66 | } |