diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/apis/modrinth.rs | 40 | ||||
-rw-r--r-- | src/commands/modification.rs | 38 | ||||
-rw-r--r-- | src/commands/update.rs | 11 |
3 files changed, 60 insertions, 29 deletions
diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index 7b322cb..fb1e666 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs | |||
@@ -111,18 +111,25 @@ pub struct Hash { | |||
111 | pub sha1: String, | 111 | pub sha1: String, |
112 | } | 112 | } |
113 | 113 | ||
114 | async fn get(api: String, path: String) -> Result<Vec<u8>, Box<dyn std::error::Error>> { | 114 | async fn get(api: String, path: String) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> { |
115 | let url = format!(r#"{}{}"#, api, path); | 115 | let url = format!(r#"{}{}"#, api, path); |
116 | 116 | ||
117 | let client = Client::builder() | 117 | let client = Client::builder() |
118 | .user_agent(format!("fxqnlr/modlistcli/{} ([email protected])", env!("CARGO_PKG_VERSION"))) | 118 | .user_agent(format!("fxqnlr/modlistcli/{} ([email protected])", env!("CARGO_PKG_VERSION"))) |
119 | .build()?; | 119 | .build()?; |
120 | let data = client.get(url) | 120 | let res = client.get(url) |
121 | .send() | 121 | .send() |
122 | .await? | 122 | .await?; |
123 | .bytes() | 123 | |
124 | .await? | 124 | let mut data: Option<Vec<u8>> = None; |
125 | .to_vec(); | 125 | |
126 | if res.status() == 200 { | ||
127 | data = Some(res | ||
128 | .bytes() | ||
129 | .await? | ||
130 | .to_vec() | ||
131 | ); | ||
132 | } | ||
126 | 133 | ||
127 | Ok(data) | 134 | Ok(data) |
128 | } | 135 | } |
@@ -130,9 +137,9 @@ async fn get(api: String, path: String) -> Result<Vec<u8>, Box<dyn std::error::E | |||
130 | pub async fn project(api: String, name: &str) -> Project { | 137 | pub async fn project(api: String, name: &str) -> Project { |
131 | println!("!!!PROJECT"); | 138 | println!("!!!PROJECT"); |
132 | let url = format!("project/{}", name); | 139 | let url = format!("project/{}", name); |
133 | let data = get(api, url); | 140 | let data = get(api, url).await.unwrap().unwrap(); |
134 | 141 | ||
135 | serde_json::from_slice(&data.await.unwrap()).unwrap() | 142 | serde_json::from_slice(&data).unwrap() |
136 | } | 143 | } |
137 | 144 | ||
138 | pub async fn projects(api: String, ids: Vec<String>) -> Vec<Project> { | 145 | pub async fn projects(api: String, ids: Vec<String>) -> Vec<Project> { |
@@ -142,9 +149,9 @@ pub async fn projects(api: String, ids: Vec<String>) -> Vec<Project> { | |||
142 | let url = format!(r#"projects?ids=["{}"]"#, all); | 149 | let url = format!(r#"projects?ids=["{}"]"#, all); |
143 | //println!("{}", url); | 150 | //println!("{}", url); |
144 | 151 | ||
145 | let data = get(api, url); | 152 | let data = get(api, url).await.unwrap().unwrap(); |
146 | 153 | ||
147 | serde_json::from_slice(&data.await.unwrap()).unwrap() | 154 | serde_json::from_slice(&data).unwrap() |
148 | } | 155 | } |
149 | 156 | ||
150 | pub async fn versions(api: String, id: String, list: List) -> Vec<Version> { | 157 | pub async fn versions(api: String, id: String, list: List) -> Vec<Version> { |
@@ -156,9 +163,14 @@ pub async fn versions(api: String, id: String, list: List) -> Vec<Version> { | |||
156 | 163 | ||
157 | let url = format!(r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, id, loaderstr, list.mc_version); | 164 | let url = format!(r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, id, loaderstr, list.mc_version); |
158 | 165 | ||
159 | let data = get(api, url); | 166 | let data = get(api, url).await.unwrap(); |
160 | 167 | ||
161 | serde_json::from_slice(&data.await.unwrap()).unwrap() | 168 | dbg!(&data); |
169 | |||
170 | match data { | ||
171 | Some(data) => serde_json::from_slice(&data).unwrap(), | ||
172 | None => Vec::new(), | ||
173 | } | ||
162 | } | 174 | } |
163 | 175 | ||
164 | pub async fn get_raw_versions(api: String, versions: Vec<String>) -> Vec<Version> { | 176 | pub async fn get_raw_versions(api: String, versions: Vec<String>) -> Vec<Version> { |
@@ -167,9 +179,9 @@ pub async fn get_raw_versions(api: String, versions: Vec<String>) -> Vec<Version | |||
167 | 179 | ||
168 | let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); | 180 | let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); |
169 | 181 | ||
170 | let data = get(api, url).await; | 182 | let data = get(api, url).await.unwrap().unwrap(); |
171 | 183 | ||
172 | serde_json::from_slice(&data.unwrap()).unwrap() | 184 | serde_json::from_slice(&data).unwrap() |
173 | } | 185 | } |
174 | 186 | ||
175 | pub fn extract_current_version(versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { | 187 | pub fn extract_current_version(versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { |
diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 8e39d11..ac23970 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use std::io::{Error, ErrorKind}; | 1 | use std::io::{Error, ErrorKind}; |
2 | 2 | ||
3 | use crate::{modrinth::{project, versions, extract_current_version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::{Input, Subcmd}, get_current_list, download_versions}; | 3 | use crate::{modrinth::{project, versions, extract_current_version, Version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::{Input, Subcmd}, get_current_list, download_versions}; |
4 | 4 | ||
5 | pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 5 | pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { |
6 | 6 | ||
@@ -26,27 +26,41 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error> | |||
26 | let project = project(String::from(&config.apis.modrinth), &args[0]).await; | 26 | let project = project(String::from(&config.apis.modrinth), &args[0]).await; |
27 | 27 | ||
28 | let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await; | 28 | let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await; |
29 | |||
30 | let current_id = extract_current_version(available_versions.clone())?; | ||
31 | |||
32 | let current_version = available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap(); | ||
33 | 29 | ||
34 | let file = current_version.clone().files.into_iter().find(|f| f.primary).unwrap().url; | ||
35 | |||
36 | let mut available_versions_vec: Vec<String> = Vec::new(); | 30 | let mut available_versions_vec: Vec<String> = Vec::new(); |
37 | for ver in available_versions { | 31 | let current_version: Option<Version>; |
38 | available_versions_vec.push(ver.id); | 32 | let current_version_id: String; |
33 | let file: String; | ||
34 | if !available_versions.is_empty() { | ||
35 | let current_id = extract_current_version(available_versions.clone())?; | ||
36 | |||
37 | current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); | ||
38 | |||
39 | current_version_id = current_version.clone().unwrap().id; | ||
40 | |||
41 | file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url; | ||
42 | |||
43 | for ver in available_versions { | ||
44 | available_versions_vec.push(ver.id); | ||
45 | }; | ||
46 | } else { | ||
47 | println!("There's currently no mod version for your specified target"); | ||
48 | current_version = None; | ||
49 | current_version_id = String::from("NONE"); | ||
50 | file = String::from("NONE"); | ||
51 | available_versions_vec.push(String::from("NONE")); | ||
39 | } | 52 | } |
53 | |||
40 | //add to current list and mod table | 54 | //add to current list and mod table |
41 | match userlist_get_all_ids(config.clone(), current_list.clone().id) { | 55 | match userlist_get_all_ids(config.clone(), current_list.clone().id) { |
42 | Ok(mods) => { | 56 | Ok(mods) => { |
43 | if mods.contains(&project.id) { | 57 | if mods.contains(&project.id) { |
44 | return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } | 58 | return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } |
45 | else { | 59 | else { |
46 | userlist_insert(config.clone(), String::from(¤t_list.id), String::from(&project.id), String::from(¤t_version.id), available_versions_vec, file)?; | 60 | userlist_insert(config.clone(), String::from(¤t_list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; |
47 | } | 61 | } |
48 | }, | 62 | }, |
49 | Err(..) => userlist_insert(config.clone(), String::from(¤t_list.id), String::from(&project.id), String::from(¤t_version.id), available_versions_vec, file)?, | 63 | Err(..) => userlist_insert(config.clone(), String::from(¤t_list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?, |
50 | }; | 64 | }; |
51 | 65 | ||
52 | match mods_get_all_ids(config.clone()) { | 66 | match mods_get_all_ids(config.clone()) { |
@@ -62,7 +76,7 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error> | |||
62 | }, | 76 | }, |
63 | }; | 77 | }; |
64 | 78 | ||
65 | if !input.disable_download { download_versions(current_list, vec![current_version]).await?; } | 79 | if !input.disable_download && current_version.is_some() { download_versions(current_list, vec![current_version.unwrap()]).await?; } |
66 | 80 | ||
67 | Ok(()) | 81 | Ok(()) |
68 | } | 82 | } |
diff --git a/src/commands/update.rs b/src/commands/update.rs index c8f0880..be15cfa 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs | |||
@@ -86,10 +86,15 @@ async fn specific_update(config: Cfg, input: Input, list: List, project: Project | |||
86 | let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; | 86 | let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; |
87 | 87 | ||
88 | let mut versions: Vec<String> = vec![]; | 88 | let mut versions: Vec<String> = vec![]; |
89 | 89 | ||
90 | for ver in &applicable_versions { | 90 | if !applicable_versions.is_empty() { |
91 | versions.push(String::from(&ver.id)); | 91 | for ver in &applicable_versions { |
92 | versions.push(String::from(&ver.id)); | ||
93 | } | ||
94 | } else { | ||
95 | versions.push(String::from("NONE")); | ||
92 | } | 96 | } |
97 | |||
93 | 98 | ||
94 | let mut current: Vec<Version> = vec![]; | 99 | let mut current: Vec<Version> = vec![]; |
95 | if input.clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { | 100 | if input.clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { |