summaryrefslogtreecommitdiff
path: root/src/files.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/files.rs')
-rw-r--r--src/files.rs66
1 files changed, 39 insertions, 27 deletions
diff --git a/src/files.rs b/src/files.rs
index bf5a0a0..59f9ed1 100644
--- a/src/files.rs
+++ b/src/files.rs
@@ -13,11 +13,13 @@ use crate::{
13 cache::{copy_cached_version, get_cached_versions}, 13 cache::{copy_cached_version, get_cached_versions},
14 config::Cfg, 14 config::Cfg,
15 db::{mods_get_info, userlist_add_disabled_versions}, 15 db::{mods_get_info, userlist_add_disabled_versions},
16 error::{ErrorType, MLError, MLE}, 16 error::{EType, MLErr, MLE},
17 modrinth::Version, 17 modrinth::Version,
18 List, PROGRESS_CHARS, STYLE_BAR_BYTE, STYLE_BAR_POS, STYLE_SPINNER, 18 List, PROGRESS_CHARS, STYLE_BAR_BYTE, STYLE_BAR_POS, STYLE_SPINNER,
19}; 19};
20 20
21/// # Errors
22/// # Panics
21pub async fn download_versions( 23pub async fn download_versions(
22 list: List, 24 list: List,
23 config: Cfg, 25 config: Cfg,
@@ -63,6 +65,8 @@ pub async fn download_versions(
63 Ok(()) 65 Ok(())
64} 66}
65 67
68/// # Errors
69/// # Panics
66async fn download_version( 70async fn download_version(
67 config: Cfg, 71 config: Cfg,
68 list: List, 72 list: List,
@@ -90,11 +94,8 @@ async fn download_version(
90 None => files[0].clone(), 94 None => files[0].clone(),
91 }; 95 };
92 let mut splitname: Vec<&str> = file.filename.split('.').collect(); 96 let mut splitname: Vec<&str> = file.filename.split('.').collect();
93 let extension = match splitname.pop().ok_or("") { 97 let Ok(extension) = splitname.pop().ok_or("") else {
94 Ok(e) => e, 98 return Err(MLErr::new(EType::Other, "NO_FILE_EXTENSION"))
95 Err(..) => {
96 return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION"))
97 }
98 }; 99 };
99 let filename = format!( 100 let filename = format!(
100 "{}.mr.{}.{}.{}", 101 "{}.mr.{}.{}.{}",
@@ -122,6 +123,8 @@ async fn download_version(
122 Ok(()) 123 Ok(())
123} 124}
124 125
126/// # Errors
127/// # Panics
125async fn download_file( 128async fn download_file(
126 url: &str, 129 url: &str,
127 path: &str, 130 path: &str,
@@ -162,23 +165,27 @@ async fn download_file(
162 Ok(()) 165 Ok(())
163} 166}
164 167
168/// # Errors
169/// # Panics
165pub fn disable_version( 170pub fn disable_version(
166 config: &Cfg, 171 config: &Cfg,
167 current_list: List, 172 current_list: &List,
168 versionid: String, 173 versionid: String,
169 mod_id: String, 174 mod_id: String,
170) -> MLE<()> { 175) -> MLE<()> {
171 let file = get_file_path(&current_list, String::from(&versionid))?; 176 let file = get_file_path(current_list, &versionid)?;
172 let disabled = format!("{file}.disabled"); 177 let disabled = format!("{file}.disabled");
173 178
174 rename(file, disabled)?; 179 rename(file, disabled)?;
175 180
176 userlist_add_disabled_versions(config, current_list.id, versionid, mod_id)?; 181 userlist_add_disabled_versions(config, &current_list.id, versionid, mod_id)?;
177 182
178 Ok(()) 183 Ok(())
179} 184}
180 185
181pub fn delete_version(list: &List, version: String) -> MLE<()> { 186/// # Errors
187/// # Panics
188pub fn delete_version(list: &List, version: &str) -> MLE<()> {
182 let file = get_file_path(list, version)?; 189 let file = get_file_path(list, version)?;
183 190
184 remove_file(file)?; 191 remove_file(file)?;
@@ -186,16 +193,15 @@ pub fn delete_version(list: &List, version: String) -> MLE<()> {
186 Ok(()) 193 Ok(())
187} 194}
188 195
189pub fn get_file_path(list: &List, versionid: String) -> MLE<String> { 196/// # Errors
197/// # Panics
198pub fn get_file_path(list: &List, versionid: &str) -> MLE<String> {
190 let mut names: HashMap<String, String> = HashMap::new(); 199 let mut names: HashMap<String, String> = HashMap::new();
191 for file in read_dir(&list.download_folder)? { 200 for file in read_dir(&list.download_folder)? {
192 let path = file?.path(); 201 let path = file?.path();
193 if path.is_file() { 202 if path.is_file() {
194 let pathstr = match path.to_str().ok_or("") { 203 let Ok(pathstr) = path.to_str().ok_or("") else {
195 Ok(s) => s, 204 return Err(MLErr::new(EType::Other, "INVALID_PATH"))
196 Err(..) => {
197 return Err(MLError::new(ErrorType::Other, "INVALID_PATH"))
198 }
199 }; 205 };
200 let namesplit: Vec<&str> = pathstr.split('.').collect(); 206 let namesplit: Vec<&str> = pathstr.split('.').collect();
201 let ver_id = namesplit[namesplit.len() - 2]; 207 let ver_id = namesplit[namesplit.len() - 2];
@@ -203,25 +209,29 @@ pub fn get_file_path(list: &List, versionid: String) -> MLE<String> {
203 } 209 }
204 } 210 }
205 211
206 let filename = match names.get(&versionid).ok_or("") { 212 let Ok(filename) = names.get(versionid).ok_or("") else {
207 Ok(n) => n, 213 return Err(MLErr::new(
208 Err(..) => { 214 EType::ArgumentError,
209 return Err(MLError::new( 215 "VERSION_NOT_FOUND_IN_FILES",
210 ErrorType::ArgumentError, 216 ))
211 "VERSION_NOT_FOUND_IN_FILES",
212 ))
213 }
214 }; 217 };
215 218
216 Ok(filename.to_owned()) 219 Ok(filename.to_owned())
217} 220}
218 221
219pub fn get_downloaded_versions(list: List) -> MLE<HashMap<String, String>> { 222/// # Errors
223/// # Panics
224pub fn get_downloaded_versions(list: &List) -> MLE<HashMap<String, String>> {
220 let mut versions: HashMap<String, String> = HashMap::new(); 225 let mut versions: HashMap<String, String> = HashMap::new();
221 for file in read_dir(&list.download_folder)? { 226 for file in read_dir(&list.download_folder)? {
222 let path = file?.path(); 227 let path = file?.path();
223 if path.is_file() && path.extension().ok_or("BAH").unwrap() == "jar" { 228 if path.is_file()
224 let pathstr = path.to_str().ok_or("BAH").unwrap(); 229 && path
230 .extension()
231 .ok_or(MLErr::new(EType::IoError, "extension"))?
232 == "jar"
233 {
234 let pathstr = path.to_str().ok_or(MLErr::new(EType::IoError, "path_to_str"))?;
225 let namesplit: Vec<&str> = pathstr.split('.').collect(); 235 let namesplit: Vec<&str> = pathstr.split('.').collect();
226 versions.insert( 236 versions.insert(
227 String::from(namesplit[namesplit.len() - 3]), 237 String::from(namesplit[namesplit.len() - 3]),
@@ -232,6 +242,8 @@ pub fn get_downloaded_versions(list: List) -> MLE<HashMap<String, String>> {
232 Ok(versions) 242 Ok(versions)
233} 243}
234 244
245/// # Errors
246/// # Panics
235pub fn clean_list_dir(list: &List) -> MLE<()> { 247pub fn clean_list_dir(list: &List) -> MLE<()> {
236 let dl_path = &list.download_folder; 248 let dl_path = &list.download_folder;
237 for entry in std::fs::read_dir(dl_path)? { 249 for entry in std::fs::read_dir(dl_path)? {