diff options
author | fxqnlr <[email protected]> | 2024-09-04 15:08:55 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-09-04 15:08:55 +0200 |
commit | 11e64fc7560de3cd0def718edf68c31e3dc8be72 (patch) | |
tree | f14ad1eb3d349e1f95e4d69c251e79ac85786a64 /src/lib.rs | |
parent | 7a85cf311c85ab45c75098dae58b5ebf5fef60bc (diff) | |
download | modlist-11e64fc7560de3cd0def718edf68c31e3dc8be72.tar modlist-11e64fc7560de3cd0def718edf68c31e3dc8be72.tar.gz modlist-11e64fc7560de3cd0def718edf68c31e3dc8be72.zip |
move stuff around, remove lib.rs for overview
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 170 |
1 files changed, 0 insertions, 170 deletions
diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index be63ff8..0000000 --- a/src/lib.rs +++ /dev/null | |||
@@ -1,170 +0,0 @@ | |||
1 | pub mod apis; | ||
2 | pub mod cache; | ||
3 | pub mod commands; | ||
4 | pub mod config; | ||
5 | pub mod db; | ||
6 | pub mod error; | ||
7 | pub mod files; | ||
8 | |||
9 | use std::{ | ||
10 | fmt::Display, | ||
11 | fs::{self, remove_file, File}, | ||
12 | io::{Read, Write}, | ||
13 | time::Duration, | ||
14 | }; | ||
15 | |||
16 | use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; | ||
17 | pub use apis::*; | ||
18 | pub use commands::*; | ||
19 | use error::{EType, MLErr, MLE}; | ||
20 | use indicatif::{ProgressBar, ProgressStyle}; | ||
21 | use serde::{Deserialize, Serialize}; | ||
22 | |||
23 | pub static STYLE_BAR_BYTE: &str = | ||
24 | "{spinner:.green}{wide_msg}{bytes}/{total_bytes} [{bar:.green/lime}]"; | ||
25 | pub static STYLE_BAR_POS: &str = " {wide_msg}{pos}/{len} [{bar:.green/lime}]"; | ||
26 | pub static STYLE_SPINNER: &str = "{spinner:.green}{wide_msg}"; | ||
27 | pub static STYLE_OPERATION: &str = " {wide_msg}"; | ||
28 | pub static STYLE_MESSAGE: &str = "{wide_msg}"; | ||
29 | pub static PROGRESS_CHARS: &str = "#>-"; | ||
30 | |||
31 | #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] | ||
32 | pub enum Modloader { | ||
33 | #[serde(rename(serialize = "fabric", deserialize = "fabric"))] | ||
34 | Fabric, | ||
35 | #[serde(rename(serialize = "forge", deserialize = "forge"))] | ||
36 | Forge, | ||
37 | #[serde(rename(serialize = "quilt", deserialize = "quilt"))] | ||
38 | Quilt, | ||
39 | } | ||
40 | |||
41 | impl Modloader { | ||
42 | /// # Errors | ||
43 | pub fn from(string: &str) -> MLE<Modloader> { | ||
44 | match string { | ||
45 | "forge" => Ok(Modloader::Forge), | ||
46 | "fabric" => Ok(Modloader::Fabric), | ||
47 | "quilt" => Ok(Modloader::Quilt), | ||
48 | _ => { | ||
49 | Err(MLErr::new(EType::ArgumentError, "UNKNOWN_MODLOADER")) | ||
50 | } | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | |||
55 | impl Display for Modloader { | ||
56 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
57 | match self { | ||
58 | Modloader::Fabric => write!(f, "fabric"), | ||
59 | Modloader::Forge => write!(f, "forge"), | ||
60 | Modloader::Quilt => write!(f, "quilt"), | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | #[derive(Debug, Clone, Deserialize, Serialize)] | ||
66 | pub enum VersionLevel { | ||
67 | #[serde(rename(serialize = "release", deserialize = "release"))] | ||
68 | Release, | ||
69 | #[serde(rename(serialize = "snapshot", deserialize = "snapshot"))] | ||
70 | Snapshot, | ||
71 | Version(String), | ||
72 | } | ||
73 | |||
74 | /// Checks if update needed (time) | ||
75 | /// if yes: get versions, update | ||
76 | /// # Errors | ||
77 | pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { | ||
78 | let p = ProgressBar::new(1); | ||
79 | p.set_style(ProgressStyle::with_template(STYLE_MESSAGE).map_err(|_| { | ||
80 | MLErr::new(EType::LibIndicatif, "template error") | ||
81 | })?); | ||
82 | p.set_message("Update minecraft versions"); | ||
83 | |||
84 | let creation_time = fs::metadata(path)?.created()?; | ||
85 | if !force | ||
86 | && creation_time.elapsed().map_err(|_| MLErr::new(EType::LibIndicatif, "SystemTimeError"))? < Duration::from_secs(60 * 60 * 24) | ||
87 | { | ||
88 | return Ok(()); | ||
89 | } | ||
90 | |||
91 | let versions = get_game_versions().await?; | ||
92 | remove_file(path)?; | ||
93 | let mut file = File::create(path)?; | ||
94 | file.write_all(serde_json::to_string_pretty(&versions)?.as_bytes())?; | ||
95 | |||
96 | p.finish_with_message("Updated minecraft versions"); | ||
97 | Ok(()) | ||
98 | } | ||
99 | |||
100 | /// Loads game versions from file | ||
101 | /// # Errors | ||
102 | pub fn load_game_versions(path: &str) -> MLE<Vec<GameVersion>> { | ||
103 | let mut file = File::open(path)?; | ||
104 | let mut data = String::new(); | ||
105 | file.read_to_string(&mut data)?; | ||
106 | let versions: Vec<GameVersion> = serde_json::from_str(&data)?; | ||
107 | Ok(versions) | ||
108 | } | ||
109 | |||
110 | impl VersionLevel { | ||
111 | #[must_use] | ||
112 | pub fn from(str: &str) -> Self { | ||
113 | match str { | ||
114 | "release" => VersionLevel::Release, | ||
115 | "snapshot" => VersionLevel::Snapshot, | ||
116 | _ => VersionLevel::Version(String::from(str)), | ||
117 | } | ||
118 | } | ||
119 | |||
120 | /// . | ||
121 | /// | ||
122 | /// Panics if . | ||
123 | /// # Errors | ||
124 | pub async fn get( | ||
125 | self, | ||
126 | versions_path: &str, | ||
127 | force_update: bool, | ||
128 | ) -> MLE<String> { | ||
129 | let path = format!("{versions_path}/versions.json"); | ||
130 | check_game_versions(&path, force_update).await?; | ||
131 | let mut versions = load_game_versions(&path)?.into_iter(); | ||
132 | |||
133 | match self { | ||
134 | VersionLevel::Release => { | ||
135 | if let Some(release) = versions | ||
136 | .find(|ver| ver.version_type == GameVersionType::release) | ||
137 | { | ||
138 | Ok(release.version) | ||
139 | } else { | ||
140 | Err(MLErr::new( | ||
141 | EType::Other, | ||
142 | "no minecraft release version found", | ||
143 | )) | ||
144 | } | ||
145 | } | ||
146 | VersionLevel::Snapshot => { | ||
147 | if let Some(snapshot) = versions | ||
148 | .find(|ver| ver.version_type == GameVersionType::snapshot) | ||
149 | { | ||
150 | Ok(snapshot.version) | ||
151 | } else { | ||
152 | Err(MLErr::new( | ||
153 | EType::Other, | ||
154 | "no minecraft snapshot version found", | ||
155 | )) | ||
156 | } | ||
157 | } | ||
158 | VersionLevel::Version(v) => { | ||
159 | if versions.any(|ver| ver.version == v) { | ||
160 | Ok(v) | ||
161 | } else { | ||
162 | Err(MLErr::new( | ||
163 | EType::ConfigError, | ||
164 | "unknown minecraft version", | ||
165 | )) | ||
166 | } | ||
167 | } | ||
168 | } | ||
169 | } | ||
170 | } | ||