diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 52 |
1 files changed, 39 insertions, 13 deletions
@@ -16,7 +16,7 @@ use std::{ | |||
16 | use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; | 16 | use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; |
17 | pub use apis::*; | 17 | pub use apis::*; |
18 | pub use commands::*; | 18 | pub use commands::*; |
19 | use error::{ErrorType, MLError, MLE}; | 19 | use error::{EType, MLErr, MLE}; |
20 | use indicatif::{ProgressBar, ProgressStyle}; | 20 | use indicatif::{ProgressBar, ProgressStyle}; |
21 | use serde::{Deserialize, Serialize}; | 21 | use serde::{Deserialize, Serialize}; |
22 | 22 | ||
@@ -39,13 +39,15 @@ pub enum Modloader { | |||
39 | } | 39 | } |
40 | 40 | ||
41 | impl Modloader { | 41 | impl Modloader { |
42 | /// # Errors | ||
43 | /// # Panics | ||
42 | pub fn from(string: &str) -> MLE<Modloader> { | 44 | pub fn from(string: &str) -> MLE<Modloader> { |
43 | match string { | 45 | match string { |
44 | "forge" => Ok(Modloader::Forge), | 46 | "forge" => Ok(Modloader::Forge), |
45 | "fabric" => Ok(Modloader::Fabric), | 47 | "fabric" => Ok(Modloader::Fabric), |
46 | "quilt" => Ok(Modloader::Quilt), | 48 | "quilt" => Ok(Modloader::Quilt), |
47 | _ => { | 49 | _ => { |
48 | Err(MLError::new(ErrorType::ArgumentError, "UNKNOWN_MODLOADER")) | 50 | Err(MLErr::new(EType::ArgumentError, "UNKNOWN_MODLOADER")) |
49 | } | 51 | } |
50 | } | 52 | } |
51 | } | 53 | } |
@@ -72,14 +74,18 @@ pub enum VersionLevel { | |||
72 | 74 | ||
73 | /// Checks if update needed (time) | 75 | /// Checks if update needed (time) |
74 | /// if yes: get versions, update | 76 | /// if yes: get versions, update |
77 | /// # Errors | ||
78 | /// # Panics | ||
75 | pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { | 79 | pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { |
76 | let p = ProgressBar::new(1); | 80 | let p = ProgressBar::new(1); |
77 | p.set_style(ProgressStyle::with_template(STYLE_MESSAGE).unwrap()); | 81 | p.set_style(ProgressStyle::with_template(STYLE_MESSAGE).map_err(|_| { |
82 | MLErr::new(EType::LibIndicatif, "template error") | ||
83 | })?); | ||
78 | p.set_message("Update minecraft versions"); | 84 | p.set_message("Update minecraft versions"); |
79 | 85 | ||
80 | let creation_time = fs::metadata(path)?.created()?; | 86 | let creation_time = fs::metadata(path)?.created()?; |
81 | if !force | 87 | if !force |
82 | && creation_time.elapsed().unwrap() < Duration::from_secs(60 * 60 * 24) | 88 | && creation_time.elapsed().map_err(|_| MLErr::new(EType::LibIndicatif, "SystemTimeError"))? < Duration::from_secs(60 * 60 * 24) |
83 | { | 89 | { |
84 | return Ok(()); | 90 | return Ok(()); |
85 | } | 91 | } |
@@ -94,6 +100,7 @@ pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { | |||
94 | } | 100 | } |
95 | 101 | ||
96 | /// Loads game versions from file | 102 | /// Loads game versions from file |
103 | /// # Errors | ||
97 | pub fn load_game_versions(path: &str) -> MLE<Vec<GameVersion>> { | 104 | pub fn load_game_versions(path: &str) -> MLE<Vec<GameVersion>> { |
98 | let mut file = File::open(path)?; | 105 | let mut file = File::open(path)?; |
99 | let mut data = String::new(); | 106 | let mut data = String::new(); |
@@ -103,7 +110,8 @@ pub fn load_game_versions(path: &str) -> MLE<Vec<GameVersion>> { | |||
103 | } | 110 | } |
104 | 111 | ||
105 | impl VersionLevel { | 112 | impl VersionLevel { |
106 | #[must_use] pub fn from(str: &str) -> Self { | 113 | #[must_use] |
114 | pub fn from(str: &str) -> Self { | ||
107 | match str { | 115 | match str { |
108 | "release" => VersionLevel::Release, | 116 | "release" => VersionLevel::Release, |
109 | "snapshot" => VersionLevel::Snapshot, | 117 | "snapshot" => VersionLevel::Snapshot, |
@@ -111,6 +119,12 @@ impl VersionLevel { | |||
111 | } | 119 | } |
112 | } | 120 | } |
113 | 121 | ||
122 | /// . | ||
123 | /// | ||
124 | /// # Panics | ||
125 | /// | ||
126 | /// Panics if . | ||
127 | /// # Errors | ||
114 | pub async fn get( | 128 | pub async fn get( |
115 | self, | 129 | self, |
116 | versions_path: &str, | 130 | versions_path: &str, |
@@ -122,23 +136,35 @@ impl VersionLevel { | |||
122 | 136 | ||
123 | match self { | 137 | match self { |
124 | VersionLevel::Release => { | 138 | VersionLevel::Release => { |
125 | let release = versions | 139 | if let Some(release) = versions |
126 | .find(|ver| ver.version_type == GameVersionType::release) | 140 | .find(|ver| ver.version_type == GameVersionType::release) |
127 | .unwrap(); | 141 | { |
128 | Ok(release.version) | 142 | Ok(release.version) |
143 | } else { | ||
144 | Err(MLErr::new( | ||
145 | EType::Other, | ||
146 | "no minecraft release version found", | ||
147 | )) | ||
148 | } | ||
129 | } | 149 | } |
130 | VersionLevel::Snapshot => { | 150 | VersionLevel::Snapshot => { |
131 | let snapshot = versions | 151 | if let Some(snapshot) = versions |
132 | .find(|ver| ver.version_type == GameVersionType::snapshot) | 152 | .find(|ver| ver.version_type == GameVersionType::snapshot) |
133 | .unwrap(); | 153 | { |
134 | Ok(snapshot.version) | 154 | Ok(snapshot.version) |
155 | } else { | ||
156 | Err(MLErr::new( | ||
157 | EType::Other, | ||
158 | "no minecraft snapshot version found", | ||
159 | )) | ||
160 | } | ||
135 | } | 161 | } |
136 | VersionLevel::Version(v) => { | 162 | VersionLevel::Version(v) => { |
137 | if versions.any(|ver| ver.version == v) { | 163 | if versions.any(|ver| ver.version == v) { |
138 | Ok(v) | 164 | Ok(v) |
139 | } else { | 165 | } else { |
140 | Err(MLError::new( | 166 | Err(MLErr::new( |
141 | ErrorType::ConfigError, | 167 | EType::ConfigError, |
142 | "unknown minecraft version", | 168 | "unknown minecraft version", |
143 | )) | 169 | )) |
144 | } | 170 | } |