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