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 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) (limited to 'src/data/gameversion.rs') 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) } } } -- cgit v1.2.3