summaryrefslogtreecommitdiff
path: root/src/data
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2024-09-04 17:32:19 +0200
committerfxqnlr <[email protected]>2024-09-04 17:32:19 +0200
commitecc4743fdec43eb578e9c35bb008c68909f1517e (patch)
tree73916114bc2bff8c72f759f5aae11a95d4dede22 /src/data
parent11e64fc7560de3cd0def718edf68c31e3dc8be72 (diff)
downloadmodlist-refactor.tar
modlist-refactor.tar.gz
modlist-refactor.zip
better error handlingrefactor
Diffstat (limited to 'src/data')
-rw-r--r--src/data/gameversion.rs33
-rw-r--r--src/data/list.rs4
-rw-r--r--src/data/modloader.rs14
-rw-r--r--src/data/projectinfo.rs (renamed from src/data/project.rs)0
4 files changed, 20 insertions, 31 deletions
diff --git a/src/data/gameversion.rs b/src/data/gameversion.rs
index 3868502..1bda755 100644
--- a/src/data/gameversion.rs
+++ b/src/data/gameversion.rs
@@ -5,11 +5,15 @@ use std::{
5}; 5};
6 6
7use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; 7use apis::modrinth::{get_game_versions, GameVersion, GameVersionType};
8use error::{EType, MLErr, MLE}; 8use errors::MLE;
9use indicatif::{ProgressBar, ProgressStyle}; 9use indicatif::{ProgressBar, ProgressStyle};
10use serde::{Deserialize, Serialize}; 10use serde::{Deserialize, Serialize};
11 11
12use crate::{apis, error, STYLE_MESSAGE}; 12use crate::{
13 apis,
14 errors::{self, Error},
15 STYLE_MESSAGE,
16};
13 17
14#[derive(Debug, Clone, Deserialize, Serialize)] 18#[derive(Debug, Clone, Deserialize, Serialize)]
15pub enum VersionLevel { 19pub enum VersionLevel {
@@ -25,15 +29,11 @@ pub enum VersionLevel {
25/// # Errors 29/// # Errors
26pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { 30pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> {
27 let p = ProgressBar::new(1); 31 let p = ProgressBar::new(1);
28 p.set_style(ProgressStyle::with_template(STYLE_MESSAGE).map_err(|_| { 32 p.set_style(ProgressStyle::with_template(STYLE_MESSAGE)?);
29 MLErr::new(EType::LibIndicatif, "template error")
30 })?);
31 p.set_message("Update minecraft versions"); 33 p.set_message("Update minecraft versions");
32 34
33 let creation_time = fs::metadata(path)?.created()?; 35 let creation_time = fs::metadata(path)?.created()?;
34 if !force 36 if !force && creation_time.elapsed()? < Duration::from_secs(60 * 60 * 24) {
35 && creation_time.elapsed().map_err(|_| MLErr::new(EType::LibIndicatif, "SystemTimeError"))? < Duration::from_secs(60 * 60 * 24)
36 {
37 return Ok(()); 37 return Ok(());
38 } 38 }
39 39
@@ -57,7 +57,7 @@ pub fn load_game_versions(path: &str) -> MLE<Vec<GameVersion>> {
57} 57}
58 58
59impl VersionLevel { 59impl VersionLevel {
60 pub fn from(str: &str) -> Self { 60 #[must_use] pub fn from(str: &str) -> Self {
61 match str { 61 match str {
62 "release" => VersionLevel::Release, 62 "release" => VersionLevel::Release,
63 "snapshot" => VersionLevel::Snapshot, 63 "snapshot" => VersionLevel::Snapshot,
@@ -85,10 +85,7 @@ impl VersionLevel {
85 { 85 {
86 Ok(release.version) 86 Ok(release.version)
87 } else { 87 } else {
88 Err(MLErr::new( 88 Err(Error::MinecraftVersionNotFound)
89 EType::Other,
90 "no minecraft release version found",
91 ))
92 } 89 }
93 } 90 }
94 VersionLevel::Snapshot => { 91 VersionLevel::Snapshot => {
@@ -97,20 +94,14 @@ impl VersionLevel {
97 { 94 {
98 Ok(snapshot.version) 95 Ok(snapshot.version)
99 } else { 96 } else {
100 Err(MLErr::new( 97 Err(Error::MinecraftVersionNotFound)
101 EType::Other,
102 "no minecraft snapshot version found",
103 ))
104 } 98 }
105 } 99 }
106 VersionLevel::Version(v) => { 100 VersionLevel::Version(v) => {
107 if versions.any(|ver| ver.version == v) { 101 if versions.any(|ver| ver.version == v) {
108 Ok(v) 102 Ok(v)
109 } else { 103 } else {
110 Err(MLErr::new( 104 Err(Error::MinecraftVersionNotFound)
111 EType::ConfigError,
112 "unknown minecraft version",
113 ))
114 } 105 }
115 } 106 }
116 } 107 }
diff --git a/src/data/list.rs b/src/data/list.rs
index 0045b7a..b886af1 100644
--- a/src/data/list.rs
+++ b/src/data/list.rs
@@ -1,4 +1,4 @@
1use crate::{config::Cfg, db::{config_get_current_list, lists_get}, error::MLE}; 1use crate::{config::Cfg, db::{config_get_current_list, lists_get}, errors::MLE};
2 2
3use super::modloader::Modloader; 3use super::modloader::Modloader;
4 4
@@ -14,6 +14,6 @@ impl List {
14 /// # Errors 14 /// # Errors
15 pub fn get_current_list(config: &Cfg) -> MLE<List> { 15 pub fn get_current_list(config: &Cfg) -> MLE<List> {
16 let id = config_get_current_list(config)?; 16 let id = config_get_current_list(config)?;
17 lists_get(config, &id) 17 Ok(lists_get(config, &id)?)
18 } 18 }
19} 19}
diff --git a/src/data/modloader.rs b/src/data/modloader.rs
index 050213f..ef2611b 100644
--- a/src/data/modloader.rs
+++ b/src/data/modloader.rs
@@ -2,7 +2,7 @@ use std::fmt::Display;
2 2
3use serde::{Deserialize, Serialize}; 3use serde::{Deserialize, Serialize};
4 4
5use crate::error::{EType, MLErr, MLE}; 5use crate::errors::ConversionError;
6 6
7#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] 7#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
8pub enum Modloader { 8pub enum Modloader {
@@ -14,16 +14,14 @@ pub enum Modloader {
14 Quilt, 14 Quilt,
15} 15}
16 16
17impl Modloader { 17impl TryFrom<&str> for Modloader {
18 /// # Errors 18 type Error = ConversionError;
19 pub fn from(string: &str) -> MLE<Modloader> { 19 fn try_from(value: &str) -> Result<Self, Self::Error> {
20 match string { 20 match value {
21 "forge" => Ok(Modloader::Forge), 21 "forge" => Ok(Modloader::Forge),
22 "fabric" => Ok(Modloader::Fabric), 22 "fabric" => Ok(Modloader::Fabric),
23 "quilt" => Ok(Modloader::Quilt), 23 "quilt" => Ok(Modloader::Quilt),
24 _ => { 24 _ => Err(ConversionError::Modloader(value.to_string()))
25 Err(MLErr::new(EType::ArgumentError, "UNKNOWN_MODLOADER"))
26 }
27 } 25 }
28 } 26 }
29} 27}
diff --git a/src/data/project.rs b/src/data/projectinfo.rs
index 9807867..9807867 100644
--- a/src/data/project.rs
+++ b/src/data/projectinfo.rs