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/io.rs | |
parent | 11e64fc7560de3cd0def718edf68c31e3dc8be72 (diff) | |
download | modlist-refactor.tar modlist-refactor.tar.gz modlist-refactor.zip |
better error handlingrefactor
Diffstat (limited to 'src/commands/io.rs')
-rw-r--r-- | src/commands/io.rs | 29 |
1 files changed, 12 insertions, 17 deletions
diff --git a/src/commands/io.rs b/src/commands/io.rs index dea0d84..80bc7d6 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs | |||
@@ -4,10 +4,14 @@ use std::fs::File; | |||
4 | use std::io::prelude::*; | 4 | use std::io::prelude::*; |
5 | 5 | ||
6 | use crate::{ | 6 | use crate::{ |
7 | config::Cfg, data::modification::{AddMod, IDSelector}, db::{ | 7 | config::Cfg, |
8 | data::modification::{AddMod, IDSelector}, | ||
9 | db::{ | ||
8 | lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids, | 10 | lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids, |
9 | userlist_get_current_version, userlist_get_set_version, | 11 | userlist_get_current_version, userlist_get_set_version, |
10 | }, error::{EType, MLErr, MLE}, mod_add, List, Modloader, STYLE_OPERATION | 12 | }, |
13 | errors::{ConversionError, Error, MLE}, | ||
14 | mod_add, List, Modloader, STYLE_OPERATION, | ||
11 | }; | 15 | }; |
12 | 16 | ||
13 | #[derive(Debug, Serialize, Deserialize)] | 17 | #[derive(Debug, Serialize, Deserialize)] |
@@ -67,22 +71,13 @@ impl ExportList { | |||
67 | /// # Errors | 71 | /// # Errors |
68 | pub fn export(config: &Cfg, list: Option<String>) -> MLE<()> { | 72 | pub fn export(config: &Cfg, list: Option<String>) -> MLE<()> { |
69 | let progress = ProgressBar::new_spinner(); | 73 | let progress = ProgressBar::new_spinner(); |
70 | progress.set_style( | 74 | progress.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); |
71 | ProgressStyle::with_template(STYLE_OPERATION) | ||
72 | .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?, | ||
73 | ); | ||
74 | 75 | ||
75 | let mut list_ids: Vec<String> = vec![]; | 76 | let mut list_ids: Vec<String> = vec![]; |
76 | if list.is_none() { | 77 | if list.is_none() { |
77 | list_ids = lists_get_all_ids(config)?; | 78 | list_ids = lists_get_all_ids(config)?; |
78 | } else { | 79 | } else { |
79 | list_ids.push( | 80 | list_ids.push(lists_get(config, &list.ok_or(Error::ListNotFound)?)?.id); |
80 | lists_get( | ||
81 | config, | ||
82 | &list.ok_or(MLErr::new(EType::Other, "nolist"))?, | ||
83 | )? | ||
84 | .id, | ||
85 | ); | ||
86 | } | 81 | } |
87 | 82 | ||
88 | let mut lists: Vec<ExportList> = vec![]; | 83 | let mut lists: Vec<ExportList> = vec![]; |
@@ -95,11 +90,11 @@ pub fn export(config: &Cfg, list: Option<String>) -> MLE<()> { | |||
95 | let toml = toml::to_string(&Export { lists })?; | 90 | let toml = toml::to_string(&Export { lists })?; |
96 | 91 | ||
97 | let filestr = dirs::home_dir() | 92 | let filestr = dirs::home_dir() |
98 | .ok_or(MLErr::new(EType::Other, "no home"))? | 93 | .ok_or(Error::SysDirNotFound("home".to_string()))? |
99 | .join("mlexport.toml") | 94 | .join("mlexport.toml") |
100 | .into_os_string() | 95 | .into_os_string() |
101 | .into_string() | 96 | .into_string() |
102 | .map_err(|_| MLErr::new(EType::IoError, "No String"))?; | 97 | .map_err(|_| ConversionError::InvalidPath)?; |
103 | 98 | ||
104 | progress.set_message("Create file"); | 99 | progress.set_message("Create file"); |
105 | let mut file = File::create(&filestr)?; | 100 | let mut file = File::create(&filestr)?; |
@@ -124,10 +119,10 @@ pub async fn import( | |||
124 | let list = List { | 119 | let list = List { |
125 | id: exportlist.id, | 120 | id: exportlist.id, |
126 | mc_version: exportlist.mc_version, | 121 | mc_version: exportlist.mc_version, |
127 | modloader: Modloader::from(&exportlist.launcher)?, | 122 | modloader: Modloader::try_from(exportlist.launcher.as_str())?, |
128 | download_folder: exportlist | 123 | download_folder: exportlist |
129 | .download_folder | 124 | .download_folder |
130 | .ok_or(MLErr::new(EType::Other, "NO_DL"))?, | 125 | .ok_or(Error::NoDownloadFolder)?, |
131 | }; | 126 | }; |
132 | lists_insert( | 127 | lists_insert( |
133 | config, | 128 | config, |