summaryrefslogtreecommitdiff
path: root/src/commands/list.rs
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2022-11-01 23:00:45 +0100
committerfxqnlr <[email protected]>2022-11-01 23:00:45 +0100
commitb125dfd03084fff47ab8e90d002c6699b762d998 (patch)
tree151677fb5e88105c06e1072c2fa309f55afaba36 /src/commands/list.rs
parentd38f09a247b8bf1ed328c342f84b74581905317d (diff)
downloadmodlist-b125dfd03084fff47ab8e90d002c6699b762d998.tar
modlist-b125dfd03084fff47ab8e90d002c6699b762d998.tar.gz
modlist-b125dfd03084fff47ab8e90d002c6699b762d998.zip
added list stuff + beginnings of mods
Diffstat (limited to 'src/commands/list.rs')
-rw-r--r--src/commands/list.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/commands/list.rs b/src/commands/list.rs
new file mode 100644
index 0000000..6c260ce
--- /dev/null
+++ b/src/commands/list.rs
@@ -0,0 +1,80 @@
1use std::io::{Error, ErrorKind};
2
3use crate::{db::{insert_list, remove_list, change_list, get_lists, get_current_list}, Modloader, config::Cfg, input::Input};
4
5pub fn list(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
6
7 if args.is_none() {
8 let lists = get_lists(config.clone())?;
9 let current_list = get_current_list(config)?;
10 println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list);
11 return Ok(());
12 }
13
14 let arguments = Input::from(args.unwrap().join(" "))?;
15
16 if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
17
18 match arguments.command.as_str() {
19 "add" => {
20 add(config, arguments.args.unwrap())
21 },
22 "change" => {
23 change(config, arguments.args.unwrap())
24 },
25 "remove" => {
26 remove(config, arguments.args.unwrap())
27 },
28 _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND")))
29 }
30}
31
32fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
33 match args.len() {
34 1 | 2 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))),
35 3 => {
36 let id = String::from(&args[0]);
37 let mc_version = String::from(&args[1]);
38 let mod_loader = match args[2].as_str() {
39 "forge" => Modloader::Forge,
40 "fabric" => Modloader::Fabric,
41 _ => return Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_MODLOADER")))
42 };
43 match insert_list(config, id, mc_version, mod_loader) {
44 Err(err) => { Err(Box::new(err)) },
45 Ok(()) => Ok(()),
46 }
47 },
48 5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
49 _ => panic!("list arguments should never be zero or lower"),
50 }
51}
52
53fn change(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
54 let lists = get_lists(config.clone())?;
55 match args.len() {
56 1 => {
57 let list = String::from(&args[0]);
58 if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); };
59 match change_list(config, list) {
60 Err(err) => { Err(Box::new(err)) },
61 Ok(()) => Ok(()),
62 }
63 },
64 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
65 _ => panic!("list arguments should never be zero or lower"),
66 }
67}
68
69fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
70 match args.len() {
71 1 => {
72 match remove_list(config, String::from(&args[0])) {
73 Err(err) => { Err(Box::new(err)) },
74 Ok(()) => Ok(()),
75 }
76 },
77 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
78 _ => panic!("list arguments should never be zero or lower"),
79 }
80}