diff options
author | fxqnlr <[email protected]> | 2023-04-20 15:13:58 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2023-04-20 15:13:58 +0200 |
commit | 8050cfcd70a16273cc2814fe29c8ee08320d85d3 (patch) | |
tree | 13924a7f1a9a0c7515beb47a14fe62d5c387d09a /src/apis | |
parent | fde646a876cc43857a278ef09250263a21d547ee (diff) | |
download | modlist-8050cfcd70a16273cc2814fe29c8ee08320d85d3.tar modlist-8050cfcd70a16273cc2814fe29c8ee08320d85d3.tar.gz modlist-8050cfcd70a16273cc2814fe29c8ee08320d85d3.zip |
cargo fmt
Diffstat (limited to 'src/apis')
-rw-r--r-- | src/apis/modrinth.rs | 56 |
1 files changed, 32 insertions, 24 deletions
diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index bb5ee19..9afe7f3 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs | |||
@@ -2,7 +2,10 @@ use chrono::{DateTime, FixedOffset}; | |||
2 | use reqwest::Client; | 2 | use reqwest::Client; |
3 | use serde::Deserialize; | 3 | use serde::Deserialize; |
4 | 4 | ||
5 | use crate::{Modloader, List, error::{MLE, MLError, ErrorType}}; | 5 | use crate::{ |
6 | error::{ErrorType, MLError, MLE}, | ||
7 | List, Modloader, | ||
8 | }; | ||
6 | 9 | ||
7 | #[derive(Debug, Deserialize, Clone)] | 10 | #[derive(Debug, Deserialize, Clone)] |
8 | pub struct Project { | 11 | pub struct Project { |
@@ -47,7 +50,7 @@ pub struct ModeratorMessage { | |||
47 | pub enum Side { | 50 | pub enum Side { |
48 | required, | 51 | required, |
49 | optional, | 52 | optional, |
50 | unsupported | 53 | unsupported, |
51 | } | 54 | } |
52 | 55 | ||
53 | #[allow(non_camel_case_types)] | 56 | #[allow(non_camel_case_types)] |
@@ -55,7 +58,7 @@ pub enum Side { | |||
55 | pub enum Type { | 58 | pub enum Type { |
56 | r#mod, | 59 | r#mod, |
57 | modpack, | 60 | modpack, |
58 | recourcepack | 61 | recourcepack, |
59 | } | 62 | } |
60 | 63 | ||
61 | #[allow(non_camel_case_types)] | 64 | #[allow(non_camel_case_types)] |
@@ -63,9 +66,11 @@ pub enum Type { | |||
63 | pub enum Status { | 66 | pub enum Status { |
64 | approved, | 67 | approved, |
65 | rejected, | 68 | rejected, |
66 | draft, unlisted, archived, | 69 | draft, |
70 | unlisted, | ||
71 | archived, | ||
67 | processing, | 72 | processing, |
68 | unknown | 73 | unknown, |
69 | } | 74 | } |
70 | 75 | ||
71 | #[derive(Debug, Clone, Deserialize)] | 76 | #[derive(Debug, Clone, Deserialize)] |
@@ -90,7 +95,7 @@ pub struct Version { | |||
90 | pub enum VersionType { | 95 | pub enum VersionType { |
91 | release, | 96 | release, |
92 | beta, | 97 | beta, |
93 | alpha | 98 | alpha, |
94 | } | 99 | } |
95 | 100 | ||
96 | #[derive(Debug, Clone, Deserialize)] | 101 | #[derive(Debug, Clone, Deserialize)] |
@@ -110,22 +115,19 @@ pub struct Hash { | |||
110 | 115 | ||
111 | async fn get(api: &str, path: String) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> { | 116 | async fn get(api: &str, path: String) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> { |
112 | let url = format!(r#"{}{}"#, api, path); | 117 | let url = format!(r#"{}{}"#, api, path); |
113 | 118 | ||
114 | let client = Client::builder() | 119 | let client = Client::builder() |
115 | .user_agent(format!("fxqnlr/modlistcli/{} ([email protected])", env!("CARGO_PKG_VERSION"))) | 120 | .user_agent(format!( |
121 | "fxqnlr/modlistcli/{} ([email protected])", | ||
122 | env!("CARGO_PKG_VERSION") | ||
123 | )) | ||
116 | .build()?; | 124 | .build()?; |
117 | let res = client.get(url) | 125 | let res = client.get(url).send().await?; |
118 | .send() | 126 | |
119 | .await?; | ||
120 | |||
121 | let mut data: Option<Vec<u8>> = None; | 127 | let mut data: Option<Vec<u8>> = None; |
122 | 128 | ||
123 | if res.status() == 200 { | 129 | if res.status() == 200 { |
124 | data = Some(res | 130 | data = Some(res.bytes().await?.to_vec()); |
125 | .bytes() | ||
126 | .await? | ||
127 | .to_vec() | ||
128 | ); | ||
129 | } | 131 | } |
130 | 132 | ||
131 | Ok(data) | 133 | Ok(data) |
@@ -143,7 +145,7 @@ pub async fn projects(api: &str, ids: Vec<String>) -> Vec<Project> { | |||
143 | let url = format!(r#"projects?ids=["{}"]"#, all); | 145 | let url = format!(r#"projects?ids=["{}"]"#, all); |
144 | 146 | ||
145 | let data = get(api, url).await.unwrap().unwrap(); | 147 | let data = get(api, url).await.unwrap().unwrap(); |
146 | 148 | ||
147 | serde_json::from_slice(&data).unwrap() | 149 | serde_json::from_slice(&data).unwrap() |
148 | } | 150 | } |
149 | 151 | ||
@@ -154,7 +156,10 @@ pub async fn versions(api: &str, id: String, list: List) -> Vec<Version> { | |||
154 | Modloader::Fabric => String::from("fabric"), | 156 | Modloader::Fabric => String::from("fabric"), |
155 | }; | 157 | }; |
156 | 158 | ||
157 | let url = format!(r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, id, loaderstr, list.mc_version); | 159 | let url = format!( |
160 | r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, | ||
161 | id, loaderstr, list.mc_version | ||
162 | ); | ||
158 | 163 | ||
159 | let data = get(api, url).await.unwrap(); | 164 | let data = get(api, url).await.unwrap(); |
160 | 165 | ||
@@ -185,7 +190,7 @@ pub fn extract_current_version(versions: Vec<Version>) -> MLE<String> { | |||
185 | times.sort_by_key(|t| t.1); | 190 | times.sort_by_key(|t| t.1); |
186 | times.reverse(); | 191 | times.reverse(); |
187 | Ok(times[0].0.to_string()) | 192 | Ok(times[0].0.to_string()) |
188 | }, | 193 | } |
189 | _ => panic!("available_versions should never be negative"), | 194 | _ => panic!("available_versions should never be negative"), |
190 | } | 195 | } |
191 | } | 196 | } |
@@ -205,16 +210,19 @@ pub struct MCVersion { | |||
205 | } | 210 | } |
206 | 211 | ||
207 | pub async fn get_minecraft_version(api: &str, version: MCVersionType) -> String { | 212 | pub async fn get_minecraft_version(api: &str, version: MCVersionType) -> String { |
208 | let data = get(api, String::from("tag/game_version")).await.unwrap().unwrap(); | 213 | let data = get(api, String::from("tag/game_version")) |
214 | .await | ||
215 | .unwrap() | ||
216 | .unwrap(); | ||
209 | let mc_versions: Vec<MCVersion> = serde_json::from_slice(&data).unwrap(); | 217 | let mc_versions: Vec<MCVersion> = serde_json::from_slice(&data).unwrap(); |
210 | let ver = match version { | 218 | let ver = match version { |
211 | MCVersionType::Release => { | 219 | MCVersionType::Release => { |
212 | let mut i = 0; | 220 | let mut i = 0; |
213 | while !mc_versions[i].major { | 221 | while !mc_versions[i].major { |
214 | i += 1; | 222 | i += 1; |
215 | }; | 223 | } |
216 | &mc_versions[i] | 224 | &mc_versions[i] |
217 | }, | 225 | } |
218 | MCVersionType::Latest => &mc_versions[0], | 226 | MCVersionType::Latest => &mc_versions[0], |
219 | MCVersionType::Specific => { | 227 | MCVersionType::Specific => { |
220 | println!("Not inplemented"); | 228 | println!("Not inplemented"); |