summaryrefslogtreecommitdiff
path: root/src/data/gameversion.rs
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/gameversion.rs
parent11e64fc7560de3cd0def718edf68c31e3dc8be72 (diff)
downloadmodlist-refactor.tar
modlist-refactor.tar.gz
modlist-refactor.zip
better error handlingrefactor
Diffstat (limited to 'src/data/gameversion.rs')
-rw-r--r--src/data/gameversion.rs33
1 files changed, 12 insertions, 21 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 }