diff options
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 151 |
1 files changed, 78 insertions, 73 deletions
@@ -1,16 +1,39 @@ | |||
1 | use std::io::{Error, ErrorKind}; | ||
2 | |||
3 | use rusqlite::Connection; | 1 | use rusqlite::Connection; |
4 | 2 | ||
5 | use crate::{ | 3 | use crate::{ |
6 | config::Cfg, | 4 | config::Cfg, |
7 | data::{list::List, modloader::Modloader}, | 5 | data::{list::List, modloader::Modloader}, |
8 | error::{EType, MLErr, MLE}, | ||
9 | }; | 6 | }; |
10 | 7 | ||
8 | type DbE<T> = Result<T, DBError>; | ||
9 | |||
10 | #[derive(Debug, thiserror::Error)] | ||
11 | pub enum DBError { | ||
12 | #[error("sqlite: {source}")] | ||
13 | Sqlite { | ||
14 | #[from] | ||
15 | source: rusqlite::Error, | ||
16 | }, | ||
17 | |||
18 | #[error("conversion: {source}")] | ||
19 | Conversion { | ||
20 | #[from] | ||
21 | source: crate::errors::ConversionError, | ||
22 | }, | ||
23 | |||
24 | #[error("not enough arguments")] | ||
25 | NotEnoughArguments, | ||
26 | |||
27 | #[error("couldn't find requested mod/version")] | ||
28 | NotFound, | ||
29 | |||
30 | #[error("no mods/versions are available in the requested scope")] | ||
31 | NotAvailable, | ||
32 | } | ||
33 | |||
11 | //MODS | 34 | //MODS |
12 | /// # Errors | 35 | /// # Errors |
13 | pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { | 36 | pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> DbE<()> { |
14 | let data = format!("{}/data.db", config.data); | 37 | let data = format!("{}/data.db", config.data); |
15 | let connection = Connection::open(data)?; | 38 | let connection = Connection::open(data)?; |
16 | 39 | ||
@@ -23,9 +46,7 @@ pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { | |||
23 | } | 46 | } |
24 | 47 | ||
25 | /// # Errors | 48 | /// # Errors |
26 | pub fn mods_get_all_ids( | 49 | pub fn mods_get_all_ids(config: &Cfg) -> DbE<Vec<String>> { |
27 | config: &Cfg, | ||
28 | ) -> Result<Vec<String>, Box<dyn std::error::Error>> { | ||
29 | let data = format!("{}/data.db", config.data); | 50 | let data = format!("{}/data.db", config.data); |
30 | let connection = Connection::open(data)?; | 51 | let connection = Connection::open(data)?; |
31 | 52 | ||
@@ -39,7 +60,7 @@ pub fn mods_get_all_ids( | |||
39 | } | 60 | } |
40 | 61 | ||
41 | if mods.is_empty() { | 62 | if mods.is_empty() { |
42 | Err(Box::new(Error::new(ErrorKind::NotFound, "NO_MODS_ALL"))) | 63 | Err(DBError::NotFound) |
43 | } else { | 64 | } else { |
44 | Ok(mods) | 65 | Ok(mods) |
45 | } | 66 | } |
@@ -53,8 +74,8 @@ pub fn mods_get_all_ids( | |||
53 | /// | 74 | /// |
54 | /// # Errors | 75 | /// # Errors |
55 | /// | 76 | /// |
56 | /// Will return `MLError` when no mod id is found | 77 | /// Will return `DBError` when no mod id is found |
57 | pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> { | 78 | pub fn mods_get_id(data: &str, slug: &str) -> DbE<String> { |
58 | let data = format!("{data}/data.db"); | 79 | let data = format!("{data}/data.db"); |
59 | let connection = Connection::open(data)?; | 80 | let connection = Connection::open(data)?; |
60 | 81 | ||
@@ -91,7 +112,7 @@ pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> { | |||
91 | } | 112 | } |
92 | 113 | ||
93 | if mod_id.is_empty() { | 114 | if mod_id.is_empty() { |
94 | return Err(MLErr::new(EType::DBError, "GI_MOD_NOT_FOUND")); | 115 | Err(DBError::NotFound)?; |
95 | }; | 116 | }; |
96 | 117 | ||
97 | Ok(mod_id) | 118 | Ok(mod_id) |
@@ -103,7 +124,7 @@ pub struct ModInfo { | |||
103 | } | 124 | } |
104 | 125 | ||
105 | /// # Errors | 126 | /// # Errors |
106 | pub fn mods_get_info(config: &Cfg, id: &str) -> MLE<ModInfo> { | 127 | pub fn mods_get_info(config: &Cfg, id: &str) -> DbE<ModInfo> { |
107 | let data = format!("{}/data.db", config.data); | 128 | let data = format!("{}/data.db", config.data); |
108 | let connection = Connection::open(data)?; | 129 | let connection = Connection::open(data)?; |
109 | 130 | ||
@@ -125,15 +146,15 @@ pub fn mods_get_info(config: &Cfg, id: &str) -> MLE<ModInfo> { | |||
125 | }); | 146 | }); |
126 | } | 147 | } |
127 | 148 | ||
128 | if mod_info.is_none() { | 149 | if let Some(mi) = mod_info { |
129 | Err(MLErr::new(EType::DBError, "GN_MOD_NOT_FOUND")) | 150 | Ok(mi) |
130 | } else { | 151 | } else { |
131 | Ok(mod_info.ok_or(MLErr::new(EType::Other, "mod_info"))?) | 152 | Err(DBError::NotFound)? |
132 | } | 153 | } |
133 | } | 154 | } |
134 | 155 | ||
135 | /// # Errors | 156 | /// # Errors |
136 | pub fn mods_remove(config: &Cfg, id: &str) -> MLE<()> { | 157 | pub fn mods_remove(config: &Cfg, id: &str) -> DbE<()> { |
137 | let data = format!("{}/data.db", config.data); | 158 | let data = format!("{}/data.db", config.data); |
138 | let connection = Connection::open(data)?; | 159 | let connection = Connection::open(data)?; |
139 | 160 | ||
@@ -152,12 +173,12 @@ pub struct DBModlistVersions { | |||
152 | pub fn mods_get_versions( | 173 | pub fn mods_get_versions( |
153 | config: &Cfg, | 174 | config: &Cfg, |
154 | mods: &[String], | 175 | mods: &[String], |
155 | ) -> MLE<Vec<DBModlistVersions>> { | 176 | ) -> DbE<Vec<DBModlistVersions>> { |
156 | let data = format!("{}/data.db", config.data); | 177 | let data = format!("{}/data.db", config.data); |
157 | let connection = Connection::open(data)?; | 178 | let connection = Connection::open(data)?; |
158 | 179 | ||
159 | if mods.is_empty() { | 180 | if mods.is_empty() { |
160 | return Err(MLErr::new(EType::ArgumentError, "MODS_NO_INPUT")); | 181 | Err(DBError::NotEnoughArguments)?; |
161 | } | 182 | } |
162 | 183 | ||
163 | let mut wherestr = String::from("WHERE"); | 184 | let mut wherestr = String::from("WHERE"); |
@@ -190,7 +211,7 @@ pub fn mods_get_versions( | |||
190 | } | 211 | } |
191 | 212 | ||
192 | if versionmaps.is_empty() { | 213 | if versionmaps.is_empty() { |
193 | Err(MLErr::new(EType::DBError, "MODS_MODS_NOT_FOUND")) | 214 | Err(DBError::NotFound) |
194 | } else { | 215 | } else { |
195 | Ok(versionmaps) | 216 | Ok(versionmaps) |
196 | } | 217 | } |
@@ -206,7 +227,7 @@ pub fn userlist_insert( | |||
206 | applicable_versions: &[String], | 227 | applicable_versions: &[String], |
207 | current_link: &str, | 228 | current_link: &str, |
208 | set_version: bool, | 229 | set_version: bool, |
209 | ) -> MLE<()> { | 230 | ) -> DbE<()> { |
210 | let data = format!("{}/data.db", config.data); | 231 | let data = format!("{}/data.db", config.data); |
211 | let connection = Connection::open(data)?; | 232 | let connection = Connection::open(data)?; |
212 | 233 | ||
@@ -228,7 +249,7 @@ pub fn userlist_insert( | |||
228 | } | 249 | } |
229 | 250 | ||
230 | /// # Errors | 251 | /// # Errors |
231 | pub fn userlist_get_all_ids(config: &Cfg, list_id: &str) -> MLE<Vec<String>> { | 252 | pub fn userlist_get_all_ids(config: &Cfg, list_id: &str) -> DbE<Vec<String>> { |
232 | let data = format!("{}/data.db", config.data); | 253 | let data = format!("{}/data.db", config.data); |
233 | let connection = Connection::open(data)?; | 254 | let connection = Connection::open(data)?; |
234 | 255 | ||
@@ -242,17 +263,14 @@ pub fn userlist_get_all_ids(config: &Cfg, list_id: &str) -> MLE<Vec<String>> { | |||
242 | } | 263 | } |
243 | 264 | ||
244 | if mod_ids.is_empty() { | 265 | if mod_ids.is_empty() { |
245 | Err(MLErr::new( | 266 | Err(DBError::NotAvailable) |
246 | EType::DBError, | ||
247 | &format!("NO_MODS_USERLIST{list_id}"), | ||
248 | )) | ||
249 | } else { | 267 | } else { |
250 | Ok(mod_ids) | 268 | Ok(mod_ids) |
251 | } | 269 | } |
252 | } | 270 | } |
253 | 271 | ||
254 | /// # Errors | 272 | /// # Errors |
255 | pub fn userlist_remove(config: &Cfg, list_id: &str, mod_id: &str) -> MLE<()> { | 273 | pub fn userlist_remove(config: &Cfg, list_id: &str, mod_id: &str) -> DbE<()> { |
256 | let data = format!("{}/data.db", config.data); | 274 | let data = format!("{}/data.db", config.data); |
257 | let connection = Connection::open(data)?; | 275 | let connection = Connection::open(data)?; |
258 | 276 | ||
@@ -268,7 +286,7 @@ pub fn userlist_get_applicable_versions( | |||
268 | config: &Cfg, | 286 | config: &Cfg, |
269 | list_id: &str, | 287 | list_id: &str, |
270 | mod_id: String, | 288 | mod_id: String, |
271 | ) -> MLE<String> { | 289 | ) -> DbE<String> { |
272 | let data = format!("{}/data.db", config.data); | 290 | let data = format!("{}/data.db", config.data); |
273 | let connection = Connection::open(data)?; | 291 | let connection = Connection::open(data)?; |
274 | 292 | ||
@@ -285,7 +303,7 @@ pub fn userlist_get_applicable_versions( | |||
285 | } | 303 | } |
286 | 304 | ||
287 | if version.is_empty() { | 305 | if version.is_empty() { |
288 | Err(MLErr::new(EType::DBError, "GAV_MOD_NOT_FOUND")) | 306 | Err(DBError::NotFound) |
289 | } else { | 307 | } else { |
290 | Ok(version) | 308 | Ok(version) |
291 | } | 309 | } |
@@ -295,7 +313,7 @@ pub fn userlist_get_applicable_versions( | |||
295 | pub fn userlist_get_all_applicable_versions_with_mods( | 313 | pub fn userlist_get_all_applicable_versions_with_mods( |
296 | config: &Cfg, | 314 | config: &Cfg, |
297 | list_id: &str, | 315 | list_id: &str, |
298 | ) -> MLE<Vec<(String, String)>> { | 316 | ) -> DbE<Vec<(String, String)>> { |
299 | let data = format!("{}/data.db", config.data); | 317 | let data = format!("{}/data.db", config.data); |
300 | let connection = Connection::open(data)?; | 318 | let connection = Connection::open(data)?; |
301 | 319 | ||
@@ -316,7 +334,7 @@ pub fn userlist_get_all_applicable_versions_with_mods( | |||
316 | } | 334 | } |
317 | 335 | ||
318 | if versions.is_empty() { | 336 | if versions.is_empty() { |
319 | return Err(MLErr::new(EType::DBError, "NO_MODS_ON_LIST")); | 337 | return Err(DBError::NotAvailable); |
320 | }; | 338 | }; |
321 | 339 | ||
322 | Ok(versions) | 340 | Ok(versions) |
@@ -327,7 +345,7 @@ pub fn userlist_get_current_version( | |||
327 | config: &Cfg, | 345 | config: &Cfg, |
328 | list_id: &str, | 346 | list_id: &str, |
329 | mod_id: &str, | 347 | mod_id: &str, |
330 | ) -> MLE<String> { | 348 | ) -> DbE<String> { |
331 | let data = format!("{}/data.db", config.data); | 349 | let data = format!("{}/data.db", config.data); |
332 | let connection = Connection::open(data)?; | 350 | let connection = Connection::open(data)?; |
333 | 351 | ||
@@ -344,7 +362,7 @@ pub fn userlist_get_current_version( | |||
344 | } | 362 | } |
345 | 363 | ||
346 | if version.is_empty() { | 364 | if version.is_empty() { |
347 | Err(MLErr::new(EType::DBError, "GCV_MOD_NOT_FOUND")) | 365 | Err(DBError::NotFound) |
348 | } else { | 366 | } else { |
349 | Ok(version) | 367 | Ok(version) |
350 | } | 368 | } |
@@ -354,7 +372,7 @@ pub fn userlist_get_current_version( | |||
354 | pub fn userlist_get_all_current_version_ids( | 372 | pub fn userlist_get_all_current_version_ids( |
355 | config: &Cfg, | 373 | config: &Cfg, |
356 | list_id: &str, | 374 | list_id: &str, |
357 | ) -> MLE<Vec<String>> { | 375 | ) -> DbE<Vec<String>> { |
358 | let data = format!("{}/data.db", config.data); | 376 | let data = format!("{}/data.db", config.data); |
359 | let connection = Connection::open(data)?; | 377 | let connection = Connection::open(data)?; |
360 | 378 | ||
@@ -368,7 +386,7 @@ pub fn userlist_get_all_current_version_ids( | |||
368 | } | 386 | } |
369 | 387 | ||
370 | if versions.is_empty() { | 388 | if versions.is_empty() { |
371 | return Err(MLErr::new(EType::DBError, "NO_MODS_ON_LIST")); | 389 | return Err(DBError::NotAvailable); |
372 | }; | 390 | }; |
373 | 391 | ||
374 | Ok(versions) | 392 | Ok(versions) |
@@ -378,7 +396,7 @@ pub fn userlist_get_all_current_version_ids( | |||
378 | pub fn userlist_get_all_current_versions_with_mods( | 396 | pub fn userlist_get_all_current_versions_with_mods( |
379 | config: &Cfg, | 397 | config: &Cfg, |
380 | list_id: &str, | 398 | list_id: &str, |
381 | ) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> { | 399 | ) -> DbE<Vec<(String, String)>> { |
382 | let data = format!("{}/data.db", config.data); | 400 | let data = format!("{}/data.db", config.data); |
383 | let connection = Connection::open(data)?; | 401 | let connection = Connection::open(data)?; |
384 | 402 | ||
@@ -399,10 +417,7 @@ pub fn userlist_get_all_current_versions_with_mods( | |||
399 | } | 417 | } |
400 | 418 | ||
401 | if versions.is_empty() { | 419 | if versions.is_empty() { |
402 | return Err(Box::new(std::io::Error::new( | 420 | return Err(DBError::NotAvailable); |
403 | ErrorKind::Other, | ||
404 | "NO_MODS_ON_LIST", | ||
405 | ))); | ||
406 | }; | 421 | }; |
407 | 422 | ||
408 | Ok(versions) | 423 | Ok(versions) |
@@ -413,7 +428,7 @@ pub fn userlist_get_set_version( | |||
413 | config: &Cfg, | 428 | config: &Cfg, |
414 | list_id: &str, | 429 | list_id: &str, |
415 | mod_id: &str, | 430 | mod_id: &str, |
416 | ) -> MLE<bool> { | 431 | ) -> DbE<bool> { |
417 | let data = format!("{}/data.db", config.data); | 432 | let data = format!("{}/data.db", config.data); |
418 | let connection = Connection::open(data)?; | 433 | let connection = Connection::open(data)?; |
419 | 434 | ||
@@ -439,7 +454,7 @@ pub fn userlist_change_versions( | |||
439 | versions: String, | 454 | versions: String, |
440 | link: String, | 455 | link: String, |
441 | mod_id: String, | 456 | mod_id: String, |
442 | ) -> MLE<()> { | 457 | ) -> DbE<()> { |
443 | let data = format!("{}/data.db", config.data); | 458 | let data = format!("{}/data.db", config.data); |
444 | let connection = Connection::open(data)?; | 459 | let connection = Connection::open(data)?; |
445 | 460 | ||
@@ -453,7 +468,7 @@ pub fn userlist_add_disabled_versions( | |||
453 | list_id: &str, | 468 | list_id: &str, |
454 | disabled_version: String, | 469 | disabled_version: String, |
455 | mod_id: String, | 470 | mod_id: String, |
456 | ) -> MLE<()> { | 471 | ) -> DbE<()> { |
457 | let data = format!("{}/data.db", config.data); | 472 | let data = format!("{}/data.db", config.data); |
458 | let connection = Connection::open(data)?; | 473 | let connection = Connection::open(data)?; |
459 | 474 | ||
@@ -480,7 +495,7 @@ pub fn userlist_get_disabled_versions( | |||
480 | config: &Cfg, | 495 | config: &Cfg, |
481 | list_id: &str, | 496 | list_id: &str, |
482 | mod_id: String, | 497 | mod_id: String, |
483 | ) -> MLE<String> { | 498 | ) -> DbE<String> { |
484 | let data = format!("{}/data.db", config.data); | 499 | let data = format!("{}/data.db", config.data); |
485 | let connection = Connection::open(data)?; | 500 | let connection = Connection::open(data)?; |
486 | 501 | ||
@@ -497,7 +512,7 @@ pub fn userlist_get_disabled_versions( | |||
497 | } | 512 | } |
498 | 513 | ||
499 | if version.is_empty() { | 514 | if version.is_empty() { |
500 | Err(MLErr::new(EType::DBError, "GDV_MOD_NOT_FOUND")) | 515 | Err(DBError::NotFound) |
501 | } else { | 516 | } else { |
502 | Ok(version) | 517 | Ok(version) |
503 | } | 518 | } |
@@ -507,7 +522,7 @@ pub fn userlist_get_disabled_versions( | |||
507 | pub fn userlist_get_all_downloads( | 522 | pub fn userlist_get_all_downloads( |
508 | config: &Cfg, | 523 | config: &Cfg, |
509 | list_id: &str, | 524 | list_id: &str, |
510 | ) -> Result<Vec<String>, Box<dyn std::error::Error>> { | 525 | ) -> DbE<Vec<String>> { |
511 | let data = format!("{}/data.db", config.data); | 526 | let data = format!("{}/data.db", config.data); |
512 | let connection = Connection::open(data)?; | 527 | let connection = Connection::open(data)?; |
513 | 528 | ||
@@ -522,10 +537,7 @@ pub fn userlist_get_all_downloads( | |||
522 | } | 537 | } |
523 | 538 | ||
524 | if links.is_empty() { | 539 | if links.is_empty() { |
525 | return Err(Box::new(std::io::Error::new( | 540 | return Err(DBError::NotAvailable); |
526 | ErrorKind::Other, | ||
527 | "NO_MODS_ON_LIST", | ||
528 | ))); | ||
529 | }; | 541 | }; |
530 | 542 | ||
531 | Ok(links) | 543 | Ok(links) |
@@ -540,7 +552,7 @@ pub fn lists_insert( | |||
540 | mc_version: &str, | 552 | mc_version: &str, |
541 | mod_loader: &Modloader, | 553 | mod_loader: &Modloader, |
542 | download_folder: &str, | 554 | download_folder: &str, |
543 | ) -> MLE<()> { | 555 | ) -> DbE<()> { |
544 | let data = format!("{}/data.db", config.data); | 556 | let data = format!("{}/data.db", config.data); |
545 | let connection = Connection::open(data)?; | 557 | let connection = Connection::open(data)?; |
546 | 558 | ||
@@ -554,7 +566,7 @@ pub fn lists_insert( | |||
554 | } | 566 | } |
555 | 567 | ||
556 | /// # Errors | 568 | /// # Errors |
557 | pub fn lists_remove(config: &Cfg, id: &str) -> MLE<()> { | 569 | pub fn lists_remove(config: &Cfg, id: &str) -> DbE<()> { |
558 | let data = format!("{}/data.db", config.data); | 570 | let data = format!("{}/data.db", config.data); |
559 | let connection = Connection::open(data)?; | 571 | let connection = Connection::open(data)?; |
560 | 572 | ||
@@ -564,7 +576,7 @@ pub fn lists_remove(config: &Cfg, id: &str) -> MLE<()> { | |||
564 | } | 576 | } |
565 | 577 | ||
566 | /// # Errors | 578 | /// # Errors |
567 | pub fn lists_get(config: &Cfg, list_id: &str) -> MLE<List> { | 579 | pub fn lists_get(config: &Cfg, list_id: &str) -> DbE<List> { |
568 | let data = format!("{}/data.db", config.data); | 580 | let data = format!("{}/data.db", config.data); |
569 | let connection = Connection::open(data)?; | 581 | let connection = Connection::open(data)?; |
570 | 582 | ||
@@ -591,20 +603,20 @@ pub fn lists_get(config: &Cfg, list_id: &str) -> MLE<List> { | |||
591 | list = List { | 603 | list = List { |
592 | id: list_id.to_string(), | 604 | id: list_id.to_string(), |
593 | mc_version: String::from(&li[0]), | 605 | mc_version: String::from(&li[0]), |
594 | modloader: Modloader::from(&li[1])?, | 606 | modloader: Modloader::try_from(li[1].as_str())?, |
595 | download_folder: String::from(&li[2]), | 607 | download_folder: String::from(&li[2]), |
596 | }; | 608 | }; |
597 | } | 609 | } |
598 | 610 | ||
599 | if list.id.is_empty() { | 611 | if list.id.is_empty() { |
600 | return Err(MLErr::new(EType::DBError, "LIST_NOT_FOUND")); | 612 | Err(DBError::NotFound)?; |
601 | } | 613 | } |
602 | 614 | ||
603 | Ok(list) | 615 | Ok(list) |
604 | } | 616 | } |
605 | 617 | ||
606 | /// # Errors | 618 | /// # Errors |
607 | pub fn lists_version(config: &Cfg, list_id: &str, version: &str) -> MLE<()> { | 619 | pub fn lists_version(config: &Cfg, list_id: &str, version: &str) -> DbE<()> { |
608 | let data = format!("{}/data.db", config.data); | 620 | let data = format!("{}/data.db", config.data); |
609 | let connection = Connection::open(data)?; | 621 | let connection = Connection::open(data)?; |
610 | 622 | ||
@@ -616,7 +628,7 @@ pub fn lists_version(config: &Cfg, list_id: &str, version: &str) -> MLE<()> { | |||
616 | } | 628 | } |
617 | 629 | ||
618 | /// # Errors | 630 | /// # Errors |
619 | pub fn lists_get_all_ids(config: &Cfg) -> MLE<Vec<String>> { | 631 | pub fn lists_get_all_ids(config: &Cfg) -> DbE<Vec<String>> { |
620 | let data = format!("{}/data.db", config.data); | 632 | let data = format!("{}/data.db", config.data); |
621 | let connection = Connection::open(data)?; | 633 | let connection = Connection::open(data)?; |
622 | 634 | ||
@@ -629,7 +641,7 @@ pub fn lists_get_all_ids(config: &Cfg) -> MLE<Vec<String>> { | |||
629 | } | 641 | } |
630 | 642 | ||
631 | if list_ids.is_empty() { | 643 | if list_ids.is_empty() { |
632 | Err(MLErr::new(EType::DBError, "NO_LISTS")) | 644 | Err(DBError::NotAvailable) |
633 | } else { | 645 | } else { |
634 | Ok(list_ids) | 646 | Ok(list_ids) |
635 | } | 647 | } |
@@ -637,7 +649,7 @@ pub fn lists_get_all_ids(config: &Cfg) -> MLE<Vec<String>> { | |||
637 | 649 | ||
638 | //config | 650 | //config |
639 | /// # Errors | 651 | /// # Errors |
640 | pub fn config_change_current_list(config: &Cfg, id: &str) -> MLE<()> { | 652 | pub fn config_change_current_list(config: &Cfg, id: &str) -> DbE<()> { |
641 | let data = format!("{}/data.db", config.data); | 653 | let data = format!("{}/data.db", config.data); |
642 | let connection = Connection::open(data)?; | 654 | let connection = Connection::open(data)?; |
643 | 655 | ||
@@ -649,7 +661,7 @@ pub fn config_change_current_list(config: &Cfg, id: &str) -> MLE<()> { | |||
649 | } | 661 | } |
650 | 662 | ||
651 | /// # Errors | 663 | /// # Errors |
652 | pub fn config_get_current_list(config: &Cfg) -> MLE<String> { | 664 | pub fn config_get_current_list(config: &Cfg) -> DbE<String> { |
653 | let data = format!("{}/data.db", config.data); | 665 | let data = format!("{}/data.db", config.data); |
654 | let connection = Connection::open(data)?; | 666 | let connection = Connection::open(data)?; |
655 | 667 | ||
@@ -663,7 +675,7 @@ pub fn config_get_current_list(config: &Cfg) -> MLE<String> { | |||
663 | } | 675 | } |
664 | 676 | ||
665 | if list_id.is_empty() { | 677 | if list_id.is_empty() { |
666 | return Err(MLErr::new(EType::DBError, "NO_CURRENT_LIST")); | 678 | return Err(DBError::NotAvailable); |
667 | } | 679 | } |
668 | 680 | ||
669 | Ok(list_id) | 681 | Ok(list_id) |
@@ -720,7 +732,7 @@ pub fn s_config_update_version( | |||
720 | /// # Errors | 732 | /// # Errors |
721 | pub fn s_config_get_version( | 733 | pub fn s_config_get_version( |
722 | config: &Cfg, | 734 | config: &Cfg, |
723 | ) -> Result<String, Box<dyn std::error::Error>> { | 735 | ) -> DbE<String> { |
724 | let data = format!("{}/data.db", config.data); | 736 | let data = format!("{}/data.db", config.data); |
725 | let connection = Connection::open(data)?; | 737 | let connection = Connection::open(data)?; |
726 | 738 | ||
@@ -734,10 +746,7 @@ pub fn s_config_get_version( | |||
734 | } | 746 | } |
735 | 747 | ||
736 | if version.is_empty() { | 748 | if version.is_empty() { |
737 | return Err(Box::new(std::io::Error::new( | 749 | return Err(DBError::NotAvailable); |
738 | ErrorKind::Other, | ||
739 | "NO_DBVERSION", | ||
740 | ))); | ||
741 | }; | 750 | }; |
742 | Ok(version) | 751 | Ok(version) |
743 | } | 752 | } |
@@ -749,18 +758,14 @@ pub fn s_insert_column( | |||
749 | column: &str, | 758 | column: &str, |
750 | c_type: &str, | 759 | c_type: &str, |
751 | default: Option<String>, | 760 | default: Option<String>, |
752 | ) -> Result<(), Box<dyn std::error::Error>> { | 761 | ) -> DbE<()> { |
753 | let data = format!("{}/data.db", config.data); | 762 | let data = format!("{}/data.db", config.data); |
754 | let connection = Connection::open(data)?; | 763 | let connection = Connection::open(data)?; |
755 | 764 | ||
756 | let mut sql = format!("ALTER TABLE {table} ADD '{column}' {c_type}"); | 765 | let mut sql = format!("ALTER TABLE {table} ADD '{column}' {c_type}"); |
757 | 766 | ||
758 | if default.is_some() { | 767 | if let Some(def) = default { |
759 | sql = format!( | 768 | sql = format!("{sql} DEFAULT {def}"); |
760 | "{} DEFAULT {}", | ||
761 | sql, | ||
762 | default.ok_or(MLErr::new(EType::Other, "errornous default"))? | ||
763 | ); | ||
764 | } | 769 | } |
765 | 770 | ||
766 | connection.execute(sql.as_str(), ())?; | 771 | connection.execute(sql.as_str(), ())?; |
@@ -768,7 +773,7 @@ pub fn s_insert_column( | |||
768 | } | 773 | } |
769 | 774 | ||
770 | /// # Errors | 775 | /// # Errors |
771 | pub fn setup(path: &str) -> MLE<()> { | 776 | pub fn setup(path: &str) -> DbE<()> { |
772 | let connection = Connection::open(path)?; | 777 | let connection = Connection::open(path)?; |
773 | 778 | ||
774 | connection.execute_batch( | 779 | connection.execute_batch( |