summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs170
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 @@
1pub mod apis;
2pub mod cache;
3pub mod commands;
4pub mod config;
5pub mod db;
6pub mod error;
7pub mod files;
8
9use std::{
10 fmt::Display,
11 fs::{self, remove_file, File},
12 io::{Read, Write},
13 time::Duration,
14};
15
16use apis::modrinth::{get_game_versions, GameVersion, GameVersionType};
17pub use apis::*;
18pub use commands::*;
19use error::{EType, MLErr, MLE};
20use indicatif::{ProgressBar, ProgressStyle};
21use serde::{Deserialize, Serialize};
22
23pub static STYLE_BAR_BYTE: &str =
24 "{spinner:.green}{wide_msg}{bytes}/{total_bytes} [{bar:.green/lime}]";
25pub static STYLE_BAR_POS: &str = " {wide_msg}{pos}/{len} [{bar:.green/lime}]";
26pub static STYLE_SPINNER: &str = "{spinner:.green}{wide_msg}";
27pub static STYLE_OPERATION: &str = " {wide_msg}";
28pub static STYLE_MESSAGE: &str = "{wide_msg}";
29pub static PROGRESS_CHARS: &str = "#>-";
30
31#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
32pub 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
41impl 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
55impl 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)]
66pub 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
77pub 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
102pub 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
110impl 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}