diff options
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 222 |
1 files changed, 125 insertions, 97 deletions
@@ -4,11 +4,12 @@ use rusqlite::Connection; | |||
4 | 4 | ||
5 | use crate::{ | 5 | use crate::{ |
6 | config::Cfg, | 6 | config::Cfg, |
7 | error::{ErrorType, MLError, MLE}, | 7 | error::{EType, MLErr, MLE}, |
8 | List, Modloader, | 8 | List, Modloader, |
9 | }; | 9 | }; |
10 | 10 | ||
11 | //MODS | 11 | //MODS |
12 | /// # Errors | ||
12 | pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { | 13 | pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { |
13 | let data = format!("{}/data.db", config.data); | 14 | let data = format!("{}/data.db", config.data); |
14 | let connection = Connection::open(data)?; | 15 | let connection = Connection::open(data)?; |
@@ -21,11 +22,12 @@ pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { | |||
21 | Ok(()) | 22 | Ok(()) |
22 | } | 23 | } |
23 | 24 | ||
25 | /// # Errors | ||
24 | pub fn mods_get_all_ids( | 26 | pub fn mods_get_all_ids( |
25 | config: &Cfg, | 27 | config: &Cfg, |
26 | ) -> Result<Vec<String>, Box<dyn std::error::Error>> { | 28 | ) -> Result<Vec<String>, Box<dyn std::error::Error>> { |
27 | let data = format!("{}/data.db", config.data); | 29 | let data = format!("{}/data.db", config.data); |
28 | let connection = Connection::open(data).unwrap(); | 30 | let connection = Connection::open(data)?; |
29 | 31 | ||
30 | let mut mods: Vec<String> = Vec::new(); | 32 | let mut mods: Vec<String> = Vec::new(); |
31 | 33 | ||
@@ -36,21 +38,22 @@ pub fn mods_get_all_ids( | |||
36 | mods.push(id?); | 38 | mods.push(id?); |
37 | } | 39 | } |
38 | 40 | ||
39 | match mods.is_empty() { | 41 | if mods.is_empty() { |
40 | true => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_MODS_ALL"))), | 42 | Err(Box::new(Error::new(ErrorKind::NotFound, "NO_MODS_ALL"))) |
41 | false => Ok(mods), | 43 | } else { |
44 | Ok(mods) | ||
42 | } | 45 | } |
43 | } | 46 | } |
44 | 47 | ||
45 | ///Get mod id based on the slug or name | 48 | /// Get mod id based on the slug or name |
46 | ///# Arguments | 49 | /// # Arguments |
47 | /// | 50 | /// |
48 | ///* `data` - file directory of the database | 51 | ///* `data` - file directory of the database |
49 | ///* `slug` - Slug or Name of a mod | 52 | ///* `slug` - Slug or Name of a mod |
50 | /// | 53 | /// |
51 | ///# Failure | 54 | /// # Errors |
52 | /// | 55 | /// |
53 | ///Will return `MLError` when no mod id is found | 56 | /// Will return `MLError` when no mod id is found |
54 | pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> { | 57 | pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> { |
55 | let data = format!("{data}/data.db"); | 58 | let data = format!("{data}/data.db"); |
56 | let connection = Connection::open(data)?; | 59 | let connection = Connection::open(data)?; |
@@ -88,7 +91,7 @@ pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> { | |||
88 | } | 91 | } |
89 | 92 | ||
90 | if mod_id.is_empty() { | 93 | if mod_id.is_empty() { |
91 | return Err(MLError::new(ErrorType::DBError, "GI_MOD_NOT_FOUND")); | 94 | return Err(MLErr::new(EType::DBError, "GI_MOD_NOT_FOUND")); |
92 | }; | 95 | }; |
93 | 96 | ||
94 | Ok(mod_id) | 97 | Ok(mod_id) |
@@ -99,6 +102,7 @@ pub struct ModInfo { | |||
99 | pub title: String, | 102 | pub title: String, |
100 | } | 103 | } |
101 | 104 | ||
105 | /// # Errors | ||
102 | pub fn mods_get_info(config: &Cfg, id: &str) -> MLE<ModInfo> { | 106 | pub fn mods_get_info(config: &Cfg, id: &str) -> MLE<ModInfo> { |
103 | let data = format!("{}/data.db", config.data); | 107 | let data = format!("{}/data.db", config.data); |
104 | let connection = Connection::open(data)?; | 108 | let connection = Connection::open(data)?; |
@@ -121,12 +125,14 @@ pub fn mods_get_info(config: &Cfg, id: &str) -> MLE<ModInfo> { | |||
121 | }); | 125 | }); |
122 | } | 126 | } |
123 | 127 | ||
124 | match mod_info.is_none() { | 128 | if mod_info.is_none() { |
125 | true => Err(MLError::new(ErrorType::DBError, "GN_MOD_NOT_FOUND")), | 129 | Err(MLErr::new(EType::DBError, "GN_MOD_NOT_FOUND")) |
126 | false => Ok(mod_info.unwrap()), | 130 | } else { |
131 | Ok(mod_info.ok_or(MLErr::new(EType::Other, "mod_info"))?) | ||
127 | } | 132 | } |
128 | } | 133 | } |
129 | 134 | ||
135 | /// # Errors | ||
130 | pub fn mods_remove(config: &Cfg, id: &str) -> MLE<()> { | 136 | pub fn mods_remove(config: &Cfg, id: &str) -> MLE<()> { |
131 | let data = format!("{}/data.db", config.data); | 137 | let data = format!("{}/data.db", config.data); |
132 | let connection = Connection::open(data)?; | 138 | let connection = Connection::open(data)?; |
@@ -142,15 +148,16 @@ pub struct DBModlistVersions { | |||
142 | pub versions: String, | 148 | pub versions: String, |
143 | } | 149 | } |
144 | 150 | ||
151 | /// # Errors | ||
145 | pub fn mods_get_versions( | 152 | pub fn mods_get_versions( |
146 | config: &Cfg, | 153 | config: &Cfg, |
147 | mods: Vec<String>, | 154 | mods: &[String], |
148 | ) -> MLE<Vec<DBModlistVersions>> { | 155 | ) -> MLE<Vec<DBModlistVersions>> { |
149 | let data = format!("{}/data.db", config.data); | 156 | let data = format!("{}/data.db", config.data); |
150 | let connection = Connection::open(data)?; | 157 | let connection = Connection::open(data)?; |
151 | 158 | ||
152 | if mods.is_empty() { | 159 | if mods.is_empty() { |
153 | return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_INPUT")); | 160 | return Err(MLErr::new(EType::ArgumentError, "MODS_NO_INPUT")); |
154 | } | 161 | } |
155 | 162 | ||
156 | let mut wherestr = String::from("WHERE"); | 163 | let mut wherestr = String::from("WHERE"); |
@@ -182,35 +189,32 @@ pub fn mods_get_versions( | |||
182 | }); | 189 | }); |
183 | } | 190 | } |
184 | 191 | ||
185 | match versionmaps.is_empty() { | 192 | if versionmaps.is_empty() { |
186 | true => Err(MLError::new(ErrorType::DBError, "MODS_MODS_NOT_FOUND")), | 193 | Err(MLErr::new(EType::DBError, "MODS_MODS_NOT_FOUND")) |
187 | false => Ok(versionmaps), | 194 | } else { |
195 | Ok(versionmaps) | ||
188 | } | 196 | } |
189 | } | 197 | } |
190 | 198 | ||
191 | //userlist | 199 | //userlist |
200 | /// # Errors | ||
192 | pub fn userlist_insert( | 201 | pub fn userlist_insert( |
193 | config: &Cfg, | 202 | config: &Cfg, |
194 | list_id: &str, | 203 | list_id: &str, |
195 | mod_id: &str, | 204 | mod_id: &str, |
196 | current_version: &str, | 205 | current_version: &str, |
197 | applicable_versions: Vec<String>, | 206 | applicable_versions: &[String], |
198 | current_link: &str, | 207 | current_link: &str, |
199 | set_version: bool, | 208 | set_version: bool, |
200 | ) -> MLE<()> { | 209 | ) -> MLE<()> { |
201 | let data = format!("{}/data.db", config.data); | 210 | let data = format!("{}/data.db", config.data); |
202 | let connection = Connection::open(data)?; | 211 | let connection = Connection::open(data)?; |
203 | 212 | ||
204 | let sv = match set_version { | 213 | let sv = if set_version { "1" } else { "0" }; |
205 | true => "1", | ||
206 | false => "0", | ||
207 | }; | ||
208 | 214 | ||
209 | connection.execute( | 215 | connection.execute( |
210 | format!( | 216 | format!("INSERT INTO {list_id} VALUES (?1, ?2, ?3, ?4, 'NONE', ?5)") |
211 | "INSERT INTO {list_id} VALUES (?1, ?2, ?3, ?4, 'NONE', ?5)" | 217 | .as_str(), |
212 | ) | ||
213 | .as_str(), | ||
214 | [ | 218 | [ |
215 | mod_id, | 219 | mod_id, |
216 | current_version, | 220 | current_version, |
@@ -223,28 +227,31 @@ pub fn userlist_insert( | |||
223 | Ok(()) | 227 | Ok(()) |
224 | } | 228 | } |
225 | 229 | ||
230 | /// # Errors | ||
226 | pub fn userlist_get_all_ids(config: &Cfg, list_id: &str) -> MLE<Vec<String>> { | 231 | pub fn userlist_get_all_ids(config: &Cfg, list_id: &str) -> MLE<Vec<String>> { |
227 | let data = format!("{}/data.db", config.data); | 232 | let data = format!("{}/data.db", config.data); |
228 | let connection = Connection::open(data).unwrap(); | 233 | let connection = Connection::open(data)?; |
229 | 234 | ||
230 | let mut mod_ids: Vec<String> = Vec::new(); | 235 | let mut mod_ids: Vec<String> = Vec::new(); |
231 | let mut stmt = connection | 236 | let mut stmt = |
232 | .prepare(format!("SELECT mod_id FROM {list_id}").as_str())?; | 237 | connection.prepare(format!("SELECT mod_id FROM {list_id}").as_str())?; |
233 | let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; | 238 | let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; |
234 | 239 | ||
235 | for id in id_iter { | 240 | for id in id_iter { |
236 | mod_ids.push(id?); | 241 | mod_ids.push(id?); |
237 | } | 242 | } |
238 | 243 | ||
239 | match mod_ids.is_empty() { | 244 | if mod_ids.is_empty() { |
240 | true => Err(MLError::new( | 245 | Err(MLErr::new( |
241 | ErrorType::DBError, | 246 | EType::DBError, |
242 | &format!("NO_MODS_USERLIST{list_id}"), | 247 | &format!("NO_MODS_USERLIST{list_id}"), |
243 | )), | 248 | )) |
244 | false => Ok(mod_ids), | 249 | } else { |
250 | Ok(mod_ids) | ||
245 | } | 251 | } |
246 | } | 252 | } |
247 | 253 | ||
254 | /// # Errors | ||
248 | pub fn userlist_remove(config: &Cfg, list_id: &str, mod_id: &str) -> MLE<()> { | 255 | pub fn userlist_remove(config: &Cfg, list_id: &str, mod_id: &str) -> MLE<()> { |
249 | let data = format!("{}/data.db", config.data); | 256 | let data = format!("{}/data.db", config.data); |
250 | let connection = Connection::open(data)?; | 257 | let connection = Connection::open(data)?; |
@@ -256,20 +263,19 @@ pub fn userlist_remove(config: &Cfg, list_id: &str, mod_id: &str) -> MLE<()> { | |||
256 | Ok(()) | 263 | Ok(()) |
257 | } | 264 | } |
258 | 265 | ||
266 | /// # Errors | ||
259 | pub fn userlist_get_applicable_versions( | 267 | pub fn userlist_get_applicable_versions( |
260 | config: &Cfg, | 268 | config: &Cfg, |
261 | list_id: String, | 269 | list_id: &str, |
262 | mod_id: String, | 270 | mod_id: String, |
263 | ) -> MLE<String> { | 271 | ) -> MLE<String> { |
264 | let data = format!("{}/data.db", config.data); | 272 | let data = format!("{}/data.db", config.data); |
265 | let connection = Connection::open(data).unwrap(); | 273 | let connection = Connection::open(data)?; |
266 | 274 | ||
267 | let mut version: String = String::new(); | 275 | let mut version: String = String::new(); |
268 | let mut stmt = connection.prepare( | 276 | let mut stmt = connection.prepare( |
269 | format!( | 277 | format!("SELECT applicable_versions FROM {list_id} WHERE mod_id = ?") |
270 | "SELECT applicable_versions FROM {list_id} WHERE mod_id = ?" | 278 | .as_str(), |
271 | ) | ||
272 | .as_str(), | ||
273 | )?; | 279 | )?; |
274 | let ver_iter = | 280 | let ver_iter = |
275 | stmt.query_map([mod_id], |row| row.get::<usize, String>(0))?; | 281 | stmt.query_map([mod_id], |row| row.get::<usize, String>(0))?; |
@@ -278,15 +284,17 @@ pub fn userlist_get_applicable_versions( | |||
278 | version = ver?; | 284 | version = ver?; |
279 | } | 285 | } |
280 | 286 | ||
281 | match version.is_empty() { | 287 | if version.is_empty() { |
282 | true => Err(MLError::new(ErrorType::DBError, "GAV_MOD_NOT_FOUND")), | 288 | Err(MLErr::new(EType::DBError, "GAV_MOD_NOT_FOUND")) |
283 | false => Ok(version), | 289 | } else { |
290 | Ok(version) | ||
284 | } | 291 | } |
285 | } | 292 | } |
286 | 293 | ||
294 | /// # Errors | ||
287 | pub fn userlist_get_all_applicable_versions_with_mods( | 295 | pub fn userlist_get_all_applicable_versions_with_mods( |
288 | config: &Cfg, | 296 | config: &Cfg, |
289 | list_id: String, | 297 | list_id: &str, |
290 | ) -> MLE<Vec<(String, String)>> { | 298 | ) -> MLE<Vec<(String, String)>> { |
291 | let data = format!("{}/data.db", config.data); | 299 | let data = format!("{}/data.db", config.data); |
292 | let connection = Connection::open(data)?; | 300 | let connection = Connection::open(data)?; |
@@ -308,19 +316,20 @@ pub fn userlist_get_all_applicable_versions_with_mods( | |||
308 | } | 316 | } |
309 | 317 | ||
310 | if versions.is_empty() { | 318 | if versions.is_empty() { |
311 | return Err(MLError::new(ErrorType::DBError, "NO_MODS_ON_LIST")); | 319 | return Err(MLErr::new(EType::DBError, "NO_MODS_ON_LIST")); |
312 | }; | 320 | }; |
313 | 321 | ||
314 | Ok(versions) | 322 | Ok(versions) |
315 | } | 323 | } |
316 | 324 | ||
325 | /// # Errors | ||
317 | pub fn userlist_get_current_version( | 326 | pub fn userlist_get_current_version( |
318 | config: &Cfg, | 327 | config: &Cfg, |
319 | list_id: &str, | 328 | list_id: &str, |
320 | mod_id: &str, | 329 | mod_id: &str, |
321 | ) -> MLE<String> { | 330 | ) -> MLE<String> { |
322 | let data = format!("{}/data.db", config.data); | 331 | let data = format!("{}/data.db", config.data); |
323 | let connection = Connection::open(data).unwrap(); | 332 | let connection = Connection::open(data)?; |
324 | 333 | ||
325 | let mut version: String = String::new(); | 334 | let mut version: String = String::new(); |
326 | let mut stmt = connection.prepare( | 335 | let mut stmt = connection.prepare( |
@@ -334,15 +343,17 @@ pub fn userlist_get_current_version( | |||
334 | version = ver?; | 343 | version = ver?; |
335 | } | 344 | } |
336 | 345 | ||
337 | match version.is_empty() { | 346 | if version.is_empty() { |
338 | true => Err(MLError::new(ErrorType::DBError, "GCV_MOD_NOT_FOUND")), | 347 | Err(MLErr::new(EType::DBError, "GCV_MOD_NOT_FOUND")) |
339 | false => Ok(version), | 348 | } else { |
349 | Ok(version) | ||
340 | } | 350 | } |
341 | } | 351 | } |
342 | 352 | ||
353 | /// # Errors | ||
343 | pub fn userlist_get_all_current_version_ids( | 354 | pub fn userlist_get_all_current_version_ids( |
344 | config: &Cfg, | 355 | config: &Cfg, |
345 | list_id: String, | 356 | list_id: &str, |
346 | ) -> MLE<Vec<String>> { | 357 | ) -> MLE<Vec<String>> { |
347 | let data = format!("{}/data.db", config.data); | 358 | let data = format!("{}/data.db", config.data); |
348 | let connection = Connection::open(data)?; | 359 | let connection = Connection::open(data)?; |
@@ -357,15 +368,16 @@ pub fn userlist_get_all_current_version_ids( | |||
357 | } | 368 | } |
358 | 369 | ||
359 | if versions.is_empty() { | 370 | if versions.is_empty() { |
360 | return Err(MLError::new(ErrorType::DBError, "NO_MODS_ON_LIST")); | 371 | return Err(MLErr::new(EType::DBError, "NO_MODS_ON_LIST")); |
361 | }; | 372 | }; |
362 | 373 | ||
363 | Ok(versions) | 374 | Ok(versions) |
364 | } | 375 | } |
365 | 376 | ||
377 | /// # Errors | ||
366 | pub fn userlist_get_all_current_versions_with_mods( | 378 | pub fn userlist_get_all_current_versions_with_mods( |
367 | config: &Cfg, | 379 | config: &Cfg, |
368 | list_id: String, | 380 | list_id: &str, |
369 | ) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> { | 381 | ) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> { |
370 | let data = format!("{}/data.db", config.data); | 382 | let data = format!("{}/data.db", config.data); |
371 | let connection = Connection::open(data)?; | 383 | let connection = Connection::open(data)?; |
@@ -396,18 +408,18 @@ pub fn userlist_get_all_current_versions_with_mods( | |||
396 | Ok(versions) | 408 | Ok(versions) |
397 | } | 409 | } |
398 | 410 | ||
411 | /// # Errors | ||
399 | pub fn userlist_get_set_version( | 412 | pub fn userlist_get_set_version( |
400 | config: &Cfg, | 413 | config: &Cfg, |
401 | list_id: &str, | 414 | list_id: &str, |
402 | mod_id: &str, | 415 | mod_id: &str, |
403 | ) -> MLE<bool> { | 416 | ) -> MLE<bool> { |
404 | let data = format!("{}/data.db", config.data); | 417 | let data = format!("{}/data.db", config.data); |
405 | let connection = Connection::open(data).unwrap(); | 418 | let connection = Connection::open(data)?; |
406 | 419 | ||
407 | let mut set_version: bool = false; | 420 | let mut set_version: bool = false; |
408 | let mut stmt = connection.prepare( | 421 | let mut stmt = connection.prepare( |
409 | format!("SELECT set_version FROM {list_id} WHERE mod_id = ?") | 422 | format!("SELECT set_version FROM {list_id} WHERE mod_id = ?").as_str(), |
410 | .as_str(), | ||
411 | )?; | 423 | )?; |
412 | let ver_iter = | 424 | let ver_iter = |
413 | stmt.query_map([&mod_id], |row| row.get::<usize, bool>(0))?; | 425 | stmt.query_map([&mod_id], |row| row.get::<usize, bool>(0))?; |
@@ -419,9 +431,10 @@ pub fn userlist_get_set_version( | |||
419 | Ok(set_version) | 431 | Ok(set_version) |
420 | } | 432 | } |
421 | 433 | ||
434 | /// # Errors | ||
422 | pub fn userlist_change_versions( | 435 | pub fn userlist_change_versions( |
423 | config: &Cfg, | 436 | config: &Cfg, |
424 | list_id: String, | 437 | list_id: &str, |
425 | current_version: String, | 438 | current_version: String, |
426 | versions: String, | 439 | versions: String, |
427 | link: String, | 440 | link: String, |
@@ -434,25 +447,22 @@ pub fn userlist_change_versions( | |||
434 | Ok(()) | 447 | Ok(()) |
435 | } | 448 | } |
436 | 449 | ||
450 | /// # Errors | ||
437 | pub fn userlist_add_disabled_versions( | 451 | pub fn userlist_add_disabled_versions( |
438 | config: &Cfg, | 452 | config: &Cfg, |
439 | list_id: String, | 453 | list_id: &str, |
440 | disabled_version: String, | 454 | disabled_version: String, |
441 | mod_id: String, | 455 | mod_id: String, |
442 | ) -> MLE<()> { | 456 | ) -> MLE<()> { |
443 | let data = format!("{}/data.db", config.data); | 457 | let data = format!("{}/data.db", config.data); |
444 | let connection = Connection::open(data)?; | 458 | let connection = Connection::open(data)?; |
445 | 459 | ||
446 | let currently_disabled_versions = userlist_get_disabled_versions( | 460 | let currently_disabled_versions = |
447 | config, | 461 | userlist_get_disabled_versions(config, list_id, String::from(&mod_id))?; |
448 | String::from(&list_id), | 462 | let disabled_versions = if currently_disabled_versions == "NONE" { |
449 | String::from(&mod_id), | 463 | disabled_version |
450 | )?; | 464 | } else { |
451 | let disabled_versions = match currently_disabled_versions == "NONE" { | 465 | format!("{currently_disabled_versions}|{disabled_version}") |
452 | true => disabled_version, | ||
453 | false => { | ||
454 | format!("{currently_disabled_versions}|{disabled_version}") | ||
455 | } | ||
456 | }; | 466 | }; |
457 | 467 | ||
458 | connection.execute( | 468 | connection.execute( |
@@ -465,13 +475,14 @@ pub fn userlist_add_disabled_versions( | |||
465 | Ok(()) | 475 | Ok(()) |
466 | } | 476 | } |
467 | 477 | ||
478 | /// # Errors | ||
468 | pub fn userlist_get_disabled_versions( | 479 | pub fn userlist_get_disabled_versions( |
469 | config: &Cfg, | 480 | config: &Cfg, |
470 | list_id: String, | 481 | list_id: &str, |
471 | mod_id: String, | 482 | mod_id: String, |
472 | ) -> MLE<String> { | 483 | ) -> MLE<String> { |
473 | let data = format!("{}/data.db", config.data); | 484 | let data = format!("{}/data.db", config.data); |
474 | let connection = Connection::open(data).unwrap(); | 485 | let connection = Connection::open(data)?; |
475 | 486 | ||
476 | let mut version: String = String::new(); | 487 | let mut version: String = String::new(); |
477 | let mut stmt = connection.prepare( | 488 | let mut stmt = connection.prepare( |
@@ -485,23 +496,24 @@ pub fn userlist_get_disabled_versions( | |||
485 | version = ver?; | 496 | version = ver?; |
486 | } | 497 | } |
487 | 498 | ||
488 | match version.is_empty() { | 499 | if version.is_empty() { |
489 | true => Err(MLError::new(ErrorType::DBError, "GDV_MOD_NOT_FOUND")), | 500 | Err(MLErr::new(EType::DBError, "GDV_MOD_NOT_FOUND")) |
490 | false => Ok(version), | 501 | } else { |
502 | Ok(version) | ||
491 | } | 503 | } |
492 | } | 504 | } |
493 | 505 | ||
506 | /// # Errors | ||
494 | pub fn userlist_get_all_downloads( | 507 | pub fn userlist_get_all_downloads( |
495 | config: &Cfg, | 508 | config: &Cfg, |
496 | list_id: String, | 509 | list_id: &str, |
497 | ) -> Result<Vec<String>, Box<dyn std::error::Error>> { | 510 | ) -> Result<Vec<String>, Box<dyn std::error::Error>> { |
498 | let data = format!("{}/data.db", config.data); | 511 | let data = format!("{}/data.db", config.data); |
499 | let connection = Connection::open(data).unwrap(); | 512 | let connection = Connection::open(data)?; |
500 | 513 | ||
501 | let mut links: Vec<String> = Vec::new(); | 514 | let mut links: Vec<String> = Vec::new(); |
502 | let mut stmt = connection.prepare( | 515 | let mut stmt = connection |
503 | format!("SELECT current_download FROM {list_id}").as_str(), | 516 | .prepare(format!("SELECT current_download FROM {list_id}").as_str())?; |
504 | )?; | ||
505 | let link_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; | 517 | let link_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; |
506 | 518 | ||
507 | for link in link_iter { | 519 | for link in link_iter { |
@@ -521,6 +533,7 @@ pub fn userlist_get_all_downloads( | |||
521 | 533 | ||
522 | //lists | 534 | //lists |
523 | ///Inserts into lists table and creates new table | 535 | ///Inserts into lists table and creates new table |
536 | /// # Errors | ||
524 | pub fn lists_insert( | 537 | pub fn lists_insert( |
525 | config: &Cfg, | 538 | config: &Cfg, |
526 | id: &str, | 539 | id: &str, |
@@ -540,6 +553,7 @@ pub fn lists_insert( | |||
540 | Ok(()) | 553 | Ok(()) |
541 | } | 554 | } |
542 | 555 | ||
556 | /// # Errors | ||
543 | pub fn lists_remove(config: &Cfg, id: &str) -> MLE<()> { | 557 | pub fn lists_remove(config: &Cfg, id: &str) -> MLE<()> { |
544 | let data = format!("{}/data.db", config.data); | 558 | let data = format!("{}/data.db", config.data); |
545 | let connection = Connection::open(data)?; | 559 | let connection = Connection::open(data)?; |
@@ -549,9 +563,10 @@ pub fn lists_remove(config: &Cfg, id: &str) -> MLE<()> { | |||
549 | Ok(()) | 563 | Ok(()) |
550 | } | 564 | } |
551 | 565 | ||
566 | /// # Errors | ||
552 | pub fn lists_get(config: &Cfg, list_id: &str) -> MLE<List> { | 567 | pub fn lists_get(config: &Cfg, list_id: &str) -> MLE<List> { |
553 | let data = format!("{}/data.db", config.data); | 568 | let data = format!("{}/data.db", config.data); |
554 | let connection = Connection::open(data).unwrap(); | 569 | let connection = Connection::open(data)?; |
555 | 570 | ||
556 | let mut list = List { | 571 | let mut list = List { |
557 | id: String::new(), | 572 | id: String::new(), |
@@ -582,15 +597,16 @@ pub fn lists_get(config: &Cfg, list_id: &str) -> MLE<List> { | |||
582 | } | 597 | } |
583 | 598 | ||
584 | if list.id.is_empty() { | 599 | if list.id.is_empty() { |
585 | return Err(MLError::new(ErrorType::DBError, "LIST_NOT_FOUND")); | 600 | return Err(MLErr::new(EType::DBError, "LIST_NOT_FOUND")); |
586 | } | 601 | } |
587 | 602 | ||
588 | Ok(list) | 603 | Ok(list) |
589 | } | 604 | } |
590 | 605 | ||
606 | /// # Errors | ||
591 | pub fn lists_version(config: &Cfg, list_id: &str, version: &str) -> MLE<()> { | 607 | pub fn lists_version(config: &Cfg, list_id: &str, version: &str) -> MLE<()> { |
592 | let data = format!("{}/data.db", config.data); | 608 | let data = format!("{}/data.db", config.data); |
593 | let connection = Connection::open(data).unwrap(); | 609 | let connection = Connection::open(data)?; |
594 | 610 | ||
595 | connection.execute( | 611 | connection.execute( |
596 | "UPDATE lists SET mc_version = ? WHERE id = ?", | 612 | "UPDATE lists SET mc_version = ? WHERE id = ?", |
@@ -599,9 +615,10 @@ pub fn lists_version(config: &Cfg, list_id: &str, version: &str) -> MLE<()> { | |||
599 | Ok(()) | 615 | Ok(()) |
600 | } | 616 | } |
601 | 617 | ||
618 | /// # Errors | ||
602 | pub fn lists_get_all_ids(config: &Cfg) -> MLE<Vec<String>> { | 619 | pub fn lists_get_all_ids(config: &Cfg) -> MLE<Vec<String>> { |
603 | let data = format!("{}/data.db", config.data); | 620 | let data = format!("{}/data.db", config.data); |
604 | let connection = Connection::open(data).unwrap(); | 621 | let connection = Connection::open(data)?; |
605 | 622 | ||
606 | let mut list_ids: Vec<String> = Vec::new(); | 623 | let mut list_ids: Vec<String> = Vec::new(); |
607 | let mut stmt = connection.prepare("SELECT id FROM lists")?; | 624 | let mut stmt = connection.prepare("SELECT id FROM lists")?; |
@@ -611,13 +628,15 @@ pub fn lists_get_all_ids(config: &Cfg) -> MLE<Vec<String>> { | |||
611 | list_ids.push(id?); | 628 | list_ids.push(id?); |
612 | } | 629 | } |
613 | 630 | ||
614 | match list_ids.is_empty() { | 631 | if list_ids.is_empty() { |
615 | true => Err(MLError::new(ErrorType::DBError, "NO_LISTS")), | 632 | Err(MLErr::new(EType::DBError, "NO_LISTS")) |
616 | false => Ok(list_ids), | 633 | } else { |
634 | Ok(list_ids) | ||
617 | } | 635 | } |
618 | } | 636 | } |
619 | 637 | ||
620 | //config | 638 | //config |
639 | /// # Errors | ||
621 | pub fn config_change_current_list(config: &Cfg, id: &str) -> MLE<()> { | 640 | pub fn config_change_current_list(config: &Cfg, id: &str) -> MLE<()> { |
622 | let data = format!("{}/data.db", config.data); | 641 | let data = format!("{}/data.db", config.data); |
623 | let connection = Connection::open(data)?; | 642 | let connection = Connection::open(data)?; |
@@ -629,9 +648,10 @@ pub fn config_change_current_list(config: &Cfg, id: &str) -> MLE<()> { | |||
629 | Ok(()) | 648 | Ok(()) |
630 | } | 649 | } |
631 | 650 | ||
651 | /// # Errors | ||
632 | pub fn config_get_current_list(config: &Cfg) -> MLE<String> { | 652 | pub fn config_get_current_list(config: &Cfg) -> MLE<String> { |
633 | let data = format!("{}/data.db", config.data); | 653 | let data = format!("{}/data.db", config.data); |
634 | let connection = Connection::open(data).unwrap(); | 654 | let connection = Connection::open(data)?; |
635 | 655 | ||
636 | let mut list_id = String::new(); | 656 | let mut list_id = String::new(); |
637 | let mut stmt = connection | 657 | let mut stmt = connection |
@@ -643,16 +663,17 @@ pub fn config_get_current_list(config: &Cfg) -> MLE<String> { | |||
643 | } | 663 | } |
644 | 664 | ||
645 | if list_id.is_empty() { | 665 | if list_id.is_empty() { |
646 | return Err(MLError::new(ErrorType::DBError, "NO_CURRENT_LIST")); | 666 | return Err(MLErr::new(EType::DBError, "NO_CURRENT_LIST")); |
647 | } | 667 | } |
648 | 668 | ||
649 | Ok(list_id) | 669 | Ok(list_id) |
650 | } | 670 | } |
651 | 671 | ||
652 | //SETUP(UPDATES) | 672 | //SETUP(UPDATES) |
673 | /// # Errors | ||
653 | pub fn s_userlist_update_download( | 674 | pub fn s_userlist_update_download( |
654 | config: &Cfg, | 675 | config: &Cfg, |
655 | list_id: String, | 676 | list_id: &str, |
656 | mod_id: String, | 677 | mod_id: String, |
657 | link: String, | 678 | link: String, |
658 | ) -> Result<(), Box<dyn std::error::Error>> { | 679 | ) -> Result<(), Box<dyn std::error::Error>> { |
@@ -660,15 +681,14 @@ pub fn s_userlist_update_download( | |||
660 | let connection = Connection::open(data)?; | 681 | let connection = Connection::open(data)?; |
661 | 682 | ||
662 | connection.execute( | 683 | connection.execute( |
663 | format!( | 684 | format!("UPDATE {list_id} SET current_download = ?1 WHERE mod_id = ?2") |
664 | "UPDATE {list_id} SET current_download = ?1 WHERE mod_id = ?2" | 685 | .as_str(), |
665 | ) | ||
666 | .as_str(), | ||
667 | [link, mod_id], | 686 | [link, mod_id], |
668 | )?; | 687 | )?; |
669 | Ok(()) | 688 | Ok(()) |
670 | } | 689 | } |
671 | 690 | ||
691 | /// # Errors | ||
672 | pub fn s_config_create_version( | 692 | pub fn s_config_create_version( |
673 | config: &Cfg, | 693 | config: &Cfg, |
674 | ) -> Result<(), Box<dyn std::error::Error>> { | 694 | ) -> Result<(), Box<dyn std::error::Error>> { |
@@ -682,6 +702,7 @@ pub fn s_config_create_version( | |||
682 | Ok(()) | 702 | Ok(()) |
683 | } | 703 | } |
684 | 704 | ||
705 | /// # Errors | ||
685 | pub fn s_config_update_version( | 706 | pub fn s_config_update_version( |
686 | config: &Cfg, | 707 | config: &Cfg, |
687 | ver: String, | 708 | ver: String, |
@@ -696,6 +717,7 @@ pub fn s_config_update_version( | |||
696 | Ok(()) | 717 | Ok(()) |
697 | } | 718 | } |
698 | 719 | ||
720 | /// # Errors | ||
699 | pub fn s_config_get_version( | 721 | pub fn s_config_get_version( |
700 | config: &Cfg, | 722 | config: &Cfg, |
701 | ) -> Result<String, Box<dyn std::error::Error>> { | 723 | ) -> Result<String, Box<dyn std::error::Error>> { |
@@ -720,11 +742,12 @@ pub fn s_config_get_version( | |||
720 | Ok(version) | 742 | Ok(version) |
721 | } | 743 | } |
722 | 744 | ||
745 | /// # Errors | ||
723 | pub fn s_insert_column( | 746 | pub fn s_insert_column( |
724 | config: &Cfg, | 747 | config: &Cfg, |
725 | table: String, | 748 | table: &str, |
726 | column: String, | 749 | column: &str, |
727 | c_type: String, | 750 | c_type: &str, |
728 | default: Option<String>, | 751 | default: Option<String>, |
729 | ) -> Result<(), Box<dyn std::error::Error>> { | 752 | ) -> Result<(), Box<dyn std::error::Error>> { |
730 | let data = format!("{}/data.db", config.data); | 753 | let data = format!("{}/data.db", config.data); |
@@ -733,14 +756,19 @@ pub fn s_insert_column( | |||
733 | let mut sql = format!("ALTER TABLE {table} ADD '{column}' {c_type}"); | 756 | let mut sql = format!("ALTER TABLE {table} ADD '{column}' {c_type}"); |
734 | 757 | ||
735 | if default.is_some() { | 758 | if default.is_some() { |
736 | sql = format!("{} DEFAULT {}", sql, default.unwrap()); | 759 | sql = format!( |
760 | "{} DEFAULT {}", | ||
761 | sql, | ||
762 | default.ok_or(MLErr::new(EType::Other, "errornous default"))? | ||
763 | ); | ||
737 | } | 764 | } |
738 | 765 | ||
739 | connection.execute(sql.as_str(), ())?; | 766 | connection.execute(sql.as_str(), ())?; |
740 | Ok(()) | 767 | Ok(()) |
741 | } | 768 | } |
742 | 769 | ||
743 | pub fn db_setup(path: &str) -> MLE<()> { | 770 | /// # Errors |
771 | pub fn setup(path: &str) -> MLE<()> { | ||
744 | let connection = Connection::open(path)?; | 772 | let connection = Connection::open(path)?; |
745 | 773 | ||
746 | connection.execute_batch( | 774 | connection.execute_batch( |