summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs46
1 files changed, 30 insertions, 16 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 7287660..8196f1c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,20 +6,26 @@ pub mod db;
6pub mod error; 6pub mod error;
7pub mod files; 7pub mod files;
8 8
9use std::{fmt::Display, fs::{File, remove_file, self}, io::{Write, Read}, time::Duration}; 9use std::{
10 fmt::Display,
11 fs::{self, remove_file, File},
12 io::{Read, Write},
13 time::Duration,
14};
10 15
11pub use apis::*;
12use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; 16use apis::modrinth::{get_game_versions, GameVersion, GameVersionType};
17pub use apis::*;
13pub use commands::*; 18pub use commands::*;
14use error::{ErrorType, MLError, MLE}; 19use error::{ErrorType, MLError, MLE};
15use indicatif::{ProgressStyle, ProgressBar}; 20use indicatif::{ProgressBar, ProgressStyle};
16use serde::{Deserialize, Serialize}; 21use serde::{Deserialize, Serialize};
17 22
18pub static STYLE_BAR_BYTE: &str = "{spinner:.green}{wide_msg}{bytes}/{total_bytes} [{bar:.green/lime}]"; 23pub static STYLE_BAR_BYTE: &str =
24 "{spinner:.green}{wide_msg}{bytes}/{total_bytes} [{bar:.green/lime}]";
19pub static STYLE_BAR_POS: &str = " {wide_msg}{pos}/{len} [{bar:.green/lime}]"; 25pub static STYLE_BAR_POS: &str = " {wide_msg}{pos}/{len} [{bar:.green/lime}]";
20pub static STYLE_SPINNER: &str = "{spinner:.green}{wide_msg}"; 26pub static STYLE_SPINNER: &str = "{spinner:.green}{wide_msg}";
21pub static STYLE_OPERATION: &str = " {wide_msg}"; 27pub static STYLE_OPERATION: &str = " {wide_msg}";
22pub static STYLE_MESSAGE: &str = "{wide_msg}"; 28pub static STYLE_MESSAGE: &str = "{wide_msg}";
23pub static PROGRESS_CHARS: &str = "#>-"; 29pub static PROGRESS_CHARS: &str = "#>-";
24 30
25#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] 31#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
@@ -59,7 +65,7 @@ pub enum VersionLevel {
59 Release, 65 Release,
60 #[serde(rename(serialize = "snapshot", deserialize = "snapshot"))] 66 #[serde(rename(serialize = "snapshot", deserialize = "snapshot"))]
61 Snapshot, 67 Snapshot,
62 Version(String) 68 Version(String),
63} 69}
64 70
65/// Checks if update needed (time) 71/// Checks if update needed (time)
@@ -70,7 +76,9 @@ pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> {
70 p.set_message("Update minecraft versions"); 76 p.set_message("Update minecraft versions");
71 77
72 let creation_time = fs::metadata(path)?.created()?; 78 let creation_time = fs::metadata(path)?.created()?;
73 if !force && creation_time.elapsed().unwrap() < Duration::from_secs(60 * 60 * 24) { return Ok(()); } 79 if !force && creation_time.elapsed().unwrap() < Duration::from_secs(60 * 60 * 24) {
80 return Ok(());
81 }
74 82
75 let versions = get_game_versions().await; 83 let versions = get_game_versions().await;
76 remove_file(path)?; 84 remove_file(path)?;
@@ -91,7 +99,6 @@ pub fn load_game_versions(path: &str) -> MLE<Vec<GameVersion>> {
91} 99}
92 100
93impl VersionLevel { 101impl VersionLevel {
94
95 pub fn from(str: &str) -> Self { 102 pub fn from(str: &str) -> Self {
96 match str { 103 match str {
97 "release" => VersionLevel::Release, 104 "release" => VersionLevel::Release,
@@ -107,22 +114,29 @@ impl VersionLevel {
107 114
108 match self { 115 match self {
109 VersionLevel::Release => { 116 VersionLevel::Release => {
110 let release = versions.find(|ver| ver.version_type == GameVersionType::release).unwrap(); 117 let release = versions
118 .find(|ver| ver.version_type == GameVersionType::release)
119 .unwrap();
111 println!("{:?}", release); 120 println!("{:?}", release);
112 Ok(release.version) 121 Ok(release.version)
113 }, 122 }
114 VersionLevel::Snapshot => { 123 VersionLevel::Snapshot => {
115 let snapshot = versions.find(|ver| ver.version_type == GameVersionType::snapshot).unwrap(); 124 let snapshot = versions
125 .find(|ver| ver.version_type == GameVersionType::snapshot)
126 .unwrap();
116 println!("{:?}", snapshot); 127 println!("{:?}", snapshot);
117 Ok(snapshot.version) 128 Ok(snapshot.version)
118 }, 129 }
119 VersionLevel::Version(v) => { 130 VersionLevel::Version(v) => {
120 if versions.any(|ver| ver.version == v) { 131 if versions.any(|ver| ver.version == v) {
121 Ok(v) 132 Ok(v)
122 } else { 133 } else {
123 Err(MLError::new(ErrorType::ConfigError, "unknown minecraft version")) 134 Err(MLError::new(
135 ErrorType::ConfigError,
136 "unknown minecraft version",
137 ))
124 } 138 }
125 }, 139 }
126 } 140 }
127 } 141 }
128} 142}