From ecc4743fdec43eb578e9c35bb008c68909f1517e Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 17:32:19 +0200 Subject: better error handling --- src/data/gameversion.rs | 33 ++++++++++++--------------------- src/data/list.rs | 4 ++-- src/data/modloader.rs | 14 ++++++-------- src/data/project.rs | 12 ------------ src/data/projectinfo.rs | 12 ++++++++++++ 5 files changed, 32 insertions(+), 43 deletions(-) delete mode 100644 src/data/project.rs create mode 100644 src/data/projectinfo.rs (limited to 'src/data') 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::{ }; use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; -use error::{EType, MLErr, MLE}; +use errors::MLE; use indicatif::{ProgressBar, ProgressStyle}; use serde::{Deserialize, Serialize}; -use crate::{apis, error, STYLE_MESSAGE}; +use crate::{ + apis, + errors::{self, Error}, + STYLE_MESSAGE, +}; #[derive(Debug, Clone, Deserialize, Serialize)] pub enum VersionLevel { @@ -25,15 +29,11 @@ pub enum VersionLevel { /// # Errors pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { let p = ProgressBar::new(1); - p.set_style(ProgressStyle::with_template(STYLE_MESSAGE).map_err(|_| { - MLErr::new(EType::LibIndicatif, "template error") - })?); + p.set_style(ProgressStyle::with_template(STYLE_MESSAGE)?); p.set_message("Update minecraft versions"); let creation_time = fs::metadata(path)?.created()?; - if !force - && creation_time.elapsed().map_err(|_| MLErr::new(EType::LibIndicatif, "SystemTimeError"))? < Duration::from_secs(60 * 60 * 24) - { + if !force && creation_time.elapsed()? < Duration::from_secs(60 * 60 * 24) { return Ok(()); } @@ -57,7 +57,7 @@ pub fn load_game_versions(path: &str) -> MLE> { } impl VersionLevel { - pub fn from(str: &str) -> Self { + #[must_use] pub fn from(str: &str) -> Self { match str { "release" => VersionLevel::Release, "snapshot" => VersionLevel::Snapshot, @@ -85,10 +85,7 @@ impl VersionLevel { { Ok(release.version) } else { - Err(MLErr::new( - EType::Other, - "no minecraft release version found", - )) + Err(Error::MinecraftVersionNotFound) } } VersionLevel::Snapshot => { @@ -97,20 +94,14 @@ impl VersionLevel { { Ok(snapshot.version) } else { - Err(MLErr::new( - EType::Other, - "no minecraft snapshot version found", - )) + Err(Error::MinecraftVersionNotFound) } } VersionLevel::Version(v) => { if versions.any(|ver| ver.version == v) { Ok(v) } else { - Err(MLErr::new( - EType::ConfigError, - "unknown minecraft version", - )) + Err(Error::MinecraftVersionNotFound) } } } 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 @@ -use crate::{config::Cfg, db::{config_get_current_list, lists_get}, error::MLE}; +use crate::{config::Cfg, db::{config_get_current_list, lists_get}, errors::MLE}; use super::modloader::Modloader; @@ -14,6 +14,6 @@ impl List { /// # Errors pub fn get_current_list(config: &Cfg) -> MLE { let id = config_get_current_list(config)?; - lists_get(config, &id) + Ok(lists_get(config, &id)?) } } 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; use serde::{Deserialize, Serialize}; -use crate::error::{EType, MLErr, MLE}; +use crate::errors::ConversionError; #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] pub enum Modloader { @@ -14,16 +14,14 @@ pub enum Modloader { Quilt, } -impl Modloader { - /// # Errors - pub fn from(string: &str) -> MLE { - match string { +impl TryFrom<&str> for Modloader { + type Error = ConversionError; + fn try_from(value: &str) -> Result { + match value { "forge" => Ok(Modloader::Forge), "fabric" => Ok(Modloader::Fabric), "quilt" => Ok(Modloader::Quilt), - _ => { - Err(MLErr::new(EType::ArgumentError, "UNKNOWN_MODLOADER")) - } + _ => Err(ConversionError::Modloader(value.to_string())) } } } diff --git a/src/data/project.rs b/src/data/project.rs deleted file mode 100644 index 9807867..0000000 --- a/src/data/project.rs +++ /dev/null @@ -1,12 +0,0 @@ -use crate::apis::modrinth::Version; - -#[derive(Debug, Clone)] -pub struct ProjectInfo { - pub mod_id: String, - pub slug: String, - pub title: String, - pub current_version: Option, - pub applicable_versions: Vec, - pub download_link: String, - pub set_version: bool, -} diff --git a/src/data/projectinfo.rs b/src/data/projectinfo.rs new file mode 100644 index 0000000..9807867 --- /dev/null +++ b/src/data/projectinfo.rs @@ -0,0 +1,12 @@ +use crate::apis::modrinth::Version; + +#[derive(Debug, Clone)] +pub struct ProjectInfo { + pub mod_id: String, + pub slug: String, + pub title: String, + pub current_version: Option, + pub applicable_versions: Vec, + pub download_link: String, + pub set_version: bool, +} -- cgit v1.2.3