diff options
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r-- | src/commands/modification.rs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs new file mode 100644 index 0000000..43e2180 --- /dev/null +++ b/src/commands/modification.rs | |||
@@ -0,0 +1,88 @@ | |||
1 | use std::io::{Error, ErrorKind}; | ||
2 | |||
3 | use crate::{modrinth::{project, versions, Version}, config::Cfg, db::{insert_mod, remove_mod_from_list, get_mod_id, insert_mod_in_list, get_mods, get_mods_from_list}, input::Input, get_current_list}; | ||
4 | |||
5 | pub async fn modification(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> { | ||
6 | |||
7 | if args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) } | ||
8 | |||
9 | let arguments = Input::from(args.unwrap().join(" "))?; | ||
10 | |||
11 | if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; | ||
12 | |||
13 | match arguments.command.as_str() { | ||
14 | "add" => { | ||
15 | add(config, arguments.args.unwrap()).await | ||
16 | }, | ||
17 | "remove" => { | ||
18 | remove(config, arguments.args.unwrap()) | ||
19 | }, | ||
20 | _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND"))) | ||
21 | } | ||
22 | } | ||
23 | |||
24 | async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { | ||
25 | |||
26 | if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; | ||
27 | |||
28 | let current_list = get_current_list(config.clone())?; | ||
29 | |||
30 | let project = project(String::from(&config.apis.modrinth), &args[0]).await; | ||
31 | |||
32 | dbg!(&project); | ||
33 | |||
34 | if project.versions.is_empty() { panic!("This should never happen"); }; | ||
35 | |||
36 | let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await; | ||
37 | |||
38 | let current_version = extract_current_version(available_versions.clone())?; | ||
39 | |||
40 | //add to current list and mod table | ||
41 | match get_mods_from_list(config.clone(), current_list.clone()) { | ||
42 | Ok(mods) => { | ||
43 | dbg!(&mods); | ||
44 | if mods.contains(&project.id) { | ||
45 | return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } | ||
46 | else { | ||
47 | insert_mod_in_list(config.clone(), current_list.clone(), String::from(&project.id), current_version, available_versions)?; | ||
48 | } | ||
49 | }, | ||
50 | Err(..) => insert_mod_in_list(config.clone(), current_list, String::from(&project.id), current_version, available_versions)?, | ||
51 | }; | ||
52 | |||
53 | match get_mods(config.clone()) { | ||
54 | Ok(mods) => { | ||
55 | dbg!(&mods); | ||
56 | if mods.contains(&project.id) { | ||
57 | return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) | ||
58 | } else { | ||
59 | insert_mod(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; | ||
60 | } | ||
61 | }, | ||
62 | Err(..) => insert_mod(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?, | ||
63 | }; | ||
64 | |||
65 | Ok(()) | ||
66 | } | ||
67 | |||
68 | fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { | ||
69 | if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; | ||
70 | |||
71 | let current_list = get_current_list(config.clone())?; | ||
72 | let mod_id = get_mod_id(config.clone(), String::from(&args[0]))?; | ||
73 | |||
74 | //TODO implement remove from modlist if not in any other lists && config clean is true | ||
75 | match remove_mod_from_list(config, current_list, mod_id) { | ||
76 | Err(err) => { Err(Box::new(err)) }, | ||
77 | Ok(()) => Ok(()), | ||
78 | } | ||
79 | } | ||
80 | |||
81 | fn extract_current_version(versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { | ||
82 | match versions.len() { | ||
83 | 0 => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_VERSIONS_AVAILABLE"))), | ||
84 | //TODO compare publish dates | ||
85 | 1.. => Ok(versions[0].id.to_string()), | ||
86 | _ => panic!("available_versions should never be negative"), | ||
87 | } | ||
88 | } | ||