diff options
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r-- | src/commands/modification.rs | 73 |
1 files changed, 37 insertions, 36 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs index c82d6b5..12a635f 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs | |||
@@ -1,4 +1,9 @@ | |||
1 | use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, ModOptions}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}}; | 1 | use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, ModOptions}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}}; |
2 | |||
3 | //TODO DO IT | ||
4 | pub struct ModVer { | ||
5 | |||
6 | } | ||
2 | 7 | ||
3 | pub async fn modification(config: Cfg, input: Input) -> MLE<()> { | 8 | pub async fn modification(config: Cfg, input: Input) -> MLE<()> { |
4 | match input.clone().mod_options.ok_or("").unwrap() { | 9 | match input.clone().mod_options.ok_or("").unwrap() { |
@@ -13,22 +18,26 @@ pub async fn modification(config: Cfg, input: Input) -> MLE<()> { | |||
13 | 18 | ||
14 | async fn add(config: Cfg, input: Input) -> MLE<()> { | 19 | async fn add(config: Cfg, input: Input) -> MLE<()> { |
15 | 20 | ||
16 | mod_add(config, vec![String::from(input.mod_id.unwrap())], input.list.unwrap(), input.direct_download).await?; | 21 | mods_add(config, vec![input.mod_id.unwrap()], input.list.unwrap(), input.direct_download, input.set_version).await?; |
17 | 22 | ||
18 | Ok(()) | 23 | Ok(()) |
19 | } | 24 | } |
20 | 25 | //TODO impl specific version | |
21 | pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, direct_download: bool) -> MLE<()> { | 26 | pub async fn mods_add(config: Cfg, mod_id: Vec<String>, list: List, direct_download: bool, set_version: bool) -> MLE<()> { |
22 | 27 | ||
23 | //Fix printing (its horrible) | 28 | println!("Add mods to {}", list.id); |
24 | println!("Adding mod(s) {:?}", mod_id); | 29 | println!(" └Add mods:"); |
25 | let projects = if mod_id.len() == 1 { | 30 | let projects = if mod_id.len() == 1 { |
26 | vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await] | 31 | vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await] |
27 | } else { | 32 | } else { |
28 | projects(String::from(&config.apis.modrinth), mod_id).await | 33 | projects(String::from(&config.apis.modrinth), mod_id).await |
29 | }; | 34 | }; |
35 | |||
36 | let mut downloadstack: Vec<Version> = Vec::new(); | ||
30 | 37 | ||
31 | for project in projects { | 38 | for project in projects { |
39 | println!("\t└{}", project.title); | ||
40 | println!("\t └Get versions"); | ||
32 | let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; | 41 | let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; |
33 | 42 | ||
34 | let mut available_versions_vec: Vec<String> = Vec::new(); | 43 | let mut available_versions_vec: Vec<String> = Vec::new(); |
@@ -37,64 +46,56 @@ pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, direct_downlo | |||
37 | let file: String; | 46 | let file: String; |
38 | if !available_versions.is_empty() { | 47 | if !available_versions.is_empty() { |
39 | let current_id = extract_current_version(available_versions.clone())?; | 48 | let current_id = extract_current_version(available_versions.clone())?; |
49 | println!("\t └Current version: {}", current_id); | ||
40 | 50 | ||
41 | current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); | 51 | current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); |
42 | 52 | ||
43 | current_version_id = current_version.clone().unwrap().id; | 53 | current_version_id = current_version.clone().unwrap().id; |
44 | 54 | ||
45 | file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url; | 55 | file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url; |
46 | |||
47 | for ver in available_versions { | 56 | for ver in available_versions { |
48 | available_versions_vec.push(ver.id); | 57 | available_versions_vec.push(ver.id); |
49 | }; | 58 | }; |
50 | } else { | 59 | } else { |
51 | println!("There's currently no mod version for your specified target"); | 60 | println!("\t └There's currently no mod version for your specified target"); |
52 | current_version = None; | 61 | current_version = None; |
53 | current_version_id = String::from("NONE"); | 62 | current_version_id = String::from("NONE"); |
54 | file = String::from("NONE"); | 63 | file = String::from("NONE"); |
55 | available_versions_vec.push(String::from("NONE")); | 64 | available_versions_vec.push(String::from("NONE")); |
56 | } | 65 | } |
57 | 66 | ||
58 | //add to current list and mod table | 67 | match userlist_insert(config.clone(), &list.id, &project.id, ¤t_version_id, available_versions_vec, &file, set_version) { |
59 | match userlist_get_all_ids(config.clone(), list.clone().id) { | 68 | Err(e) => { |
60 | Ok(mods) => { | 69 | let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); |
61 | if mods.contains(&project.id) { | 70 | if e.to_string() == expected_err { Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) } else { Err(e) } |
62 | return Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_LIST")); } | ||
63 | else { | ||
64 | userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; | ||
65 | } | ||
66 | }, | 71 | }, |
67 | Err(..) => userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?, | 72 | Ok(..) => { Ok(..) }, |
68 | }; | 73 | }?; |
69 | 74 | ||
70 | match mods_get_all_ids(config.clone()) { | 75 | match mods_insert(config.clone(), &project.id, &project.slug, &project.title) { |
71 | Ok(mods) => { | 76 | Err(e) => { |
72 | if mods.contains(&project.id) { | 77 | if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { Ok(..) } else { Err(e) } |
73 | //return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) | ||
74 | } else { | ||
75 | mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; | ||
76 | } | ||
77 | }, | ||
78 | Err(..) => { | ||
79 | mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; | ||
80 | }, | 78 | }, |
81 | }; | 79 | Ok(..) => Ok(..), |
80 | }?; | ||
82 | 81 | ||
83 | if direct_download && current_version.is_some() { download_versions(list.clone(), config.clone(), vec![current_version.unwrap()]).await?; }; | 82 | downloadstack.push(current_version.unwrap()); |
84 | 83 | }; | |
85 | } | 84 | |
85 | //Download all the added mods | ||
86 | if direct_download { | ||
87 | download_versions(list.clone(), config.clone(), downloadstack).await?; | ||
88 | }; | ||
86 | 89 | ||
87 | Ok(()) | 90 | Ok(()) |
88 | } | 91 | } |
89 | 92 | ||
90 | fn remove(config: Cfg, input: Input) -> MLE<()> { | 93 | fn remove(config: Cfg, input: Input) -> MLE<()> { |
91 | 94 | ||
92 | //TODO inplement deletion by slug or title | 95 | let mod_id = mods_get_id(&config.data, input.mod_id.as_ref().unwrap())?; |
93 | let mod_id = mods_get_id(config.clone(), input.clone().mod_id.unwrap())?; | ||
94 | 96 | ||
95 | let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; | 97 | let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; |
96 | 98 | ||
97 | //TODO implement remove from modlist if not in any other lists && config clean is true | ||
98 | userlist_remove(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; | 99 | userlist_remove(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; |
99 | delete_version(input.list.unwrap(), version)?; | 100 | delete_version(input.list.unwrap(), version)?; |
100 | 101 | ||