diff options
author | fxqnlr <[email protected]> | 2024-09-04 17:32:19 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-09-04 17:32:19 +0200 |
commit | ecc4743fdec43eb578e9c35bb008c68909f1517e (patch) | |
tree | 73916114bc2bff8c72f759f5aae11a95d4dede22 /src/commands/list.rs | |
parent | 11e64fc7560de3cd0def718edf68c31e3dc8be72 (diff) | |
download | modlist-refactor.tar modlist-refactor.tar.gz modlist-refactor.zip |
better error handlingrefactor
Diffstat (limited to 'src/commands/list.rs')
-rw-r--r-- | src/commands/list.rs | 38 |
1 files changed, 13 insertions, 25 deletions
diff --git a/src/commands/list.rs b/src/commands/list.rs index 23a9f0f..db8a831 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs | |||
@@ -1,10 +1,14 @@ | |||
1 | use indicatif::{ProgressBar, ProgressStyle}; | 1 | use indicatif::{ProgressBar, ProgressStyle}; |
2 | 2 | ||
3 | use crate::{ | 3 | use crate::{ |
4 | config::Cfg, data::modloader::Modloader, db::{ | 4 | config::Cfg, |
5 | config_change_current_list, lists_get, | 5 | data::modloader::Modloader, |
6 | lists_get_all_ids, lists_insert, lists_remove, lists_version, | 6 | db::{ |
7 | }, error::{EType, MLErr, MLE}, update, STYLE_OPERATION | 7 | config_change_current_list, lists_get, lists_get_all_ids, lists_insert, |
8 | lists_remove, lists_version, | ||
9 | }, | ||
10 | errors::{Error, MLE}, | ||
11 | update, STYLE_OPERATION, | ||
8 | }; | 12 | }; |
9 | 13 | ||
10 | /// # Errors | 14 | /// # Errors |
@@ -16,11 +20,7 @@ pub fn add( | |||
16 | directory: &str, | 20 | directory: &str, |
17 | ) -> MLE<()> { | 21 | ) -> MLE<()> { |
18 | let p = ProgressBar::new_spinner(); | 22 | let p = ProgressBar::new_spinner(); |
19 | p.set_style( | 23 | p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); |
20 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | ||
21 | MLErr::new(EType::LibIndicatif, "template error") | ||
22 | })?, | ||
23 | ); | ||
24 | p.set_message(format!("Create {id}")); | 24 | p.set_message(format!("Create {id}")); |
25 | lists_insert(config, id, mc_version, modloader, directory)?; | 25 | lists_insert(config, id, mc_version, modloader, directory)?; |
26 | p.finish_with_message(format!("Created {id}")); | 26 | p.finish_with_message(format!("Created {id}")); |
@@ -30,15 +30,11 @@ pub fn add( | |||
30 | /// # Errors | 30 | /// # Errors |
31 | pub fn change(config: &Cfg, id: &str) -> MLE<()> { | 31 | pub fn change(config: &Cfg, id: &str) -> MLE<()> { |
32 | let p = ProgressBar::new_spinner(); | 32 | let p = ProgressBar::new_spinner(); |
33 | p.set_style( | 33 | p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); |
34 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | ||
35 | MLErr::new(EType::LibIndicatif, "template error") | ||
36 | })?, | ||
37 | ); | ||
38 | p.set_message(format!("Change default list to {id}")); | 34 | p.set_message(format!("Change default list to {id}")); |
39 | 35 | ||
40 | if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) { | 36 | if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) { |
41 | return Err(MLErr::new(EType::ArgumentError, "List not found")); | 37 | return Err(Error::ListNotFound); |
42 | }; | 38 | }; |
43 | config_change_current_list(config, id)?; | 39 | config_change_current_list(config, id)?; |
44 | 40 | ||
@@ -49,11 +45,7 @@ pub fn change(config: &Cfg, id: &str) -> MLE<()> { | |||
49 | /// # Errors | 45 | /// # Errors |
50 | pub fn remove(config: &Cfg, id: &str) -> MLE<()> { | 46 | pub fn remove(config: &Cfg, id: &str) -> MLE<()> { |
51 | let p = ProgressBar::new_spinner(); | 47 | let p = ProgressBar::new_spinner(); |
52 | p.set_style( | 48 | p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); |
53 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | ||
54 | MLErr::new(EType::LibIndicatif, "template error") | ||
55 | })?, | ||
56 | ); | ||
57 | p.set_message(format!("Remove {id}")); | 49 | p.set_message(format!("Remove {id}")); |
58 | lists_remove(config, id)?; | 50 | lists_remove(config, id)?; |
59 | p.finish_with_message(format!("Removed {id}")); | 51 | p.finish_with_message(format!("Removed {id}")); |
@@ -75,11 +67,7 @@ pub async fn version( | |||
75 | delete: bool, | 67 | delete: bool, |
76 | ) -> MLE<()> { | 68 | ) -> MLE<()> { |
77 | let p = ProgressBar::new_spinner(); | 69 | let p = ProgressBar::new_spinner(); |
78 | p.set_style( | 70 | p.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); |
79 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | ||
80 | MLErr::new(EType::LibIndicatif, "template error") | ||
81 | })?, | ||
82 | ); | ||
83 | p.set_message(format!( | 71 | p.set_message(format!( |
84 | "Change version for list {id} to minecraft version: {mc_version}" | 72 | "Change version for list {id} to minecraft version: {mc_version}" |
85 | )); | 73 | )); |