From 89193143f90e1b8cbb91445d14942fa39509acbb Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 9 Jan 2023 23:12:52 +0100 Subject: implemented more Error (dumb) --- src/files.rs | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs index a3f838a..998ed32 100644 --- a/src/files.rs +++ b/src/files.rs @@ -2,16 +2,19 @@ use std::{fs::{File, read_dir, remove_file, rename}, io::Write, collections::Has use futures_util::StreamExt; use reqwest::Client; -use crate::{List, modrinth::Version, db::userlist_add_disabled_versions, config::Cfg}; +use crate::{List, modrinth::Version, db::userlist_add_disabled_versions, config::Cfg, error::{MLE, MLError, ErrorType}}; -pub async fn download_versions(current_list: List, versions: Vec) -> Result> { +pub async fn download_versions(current_list: List, versions: Vec) -> MLE { let dl_path = String::from(¤t_list.download_folder); for ver in versions { let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); - let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?; + let extension = match splitname.pop().ok_or("") { + Ok(e) => e, + Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")), + }; let filename = format!("{}.mr.{}.{}.{}", splitname.join("."), ver.project_id, ver.id, extension); download_file(primary_file.url, current_list.clone().download_folder, filename).await?; } @@ -19,14 +22,14 @@ pub async fn download_versions(current_list: List, versions: Vec) -> Re Ok(dl_path) } -async fn download_file(url: String, path: String, name: String) -> Result<(), Box> { +async fn download_file(url: String, path: String, name: String) -> MLE<()> { println!("Downloading {}", url); let dl_path_file = format!("{}/{}", path, name); let res = Client::new() .get(String::from(&url)) .send() .await?; - + // download chunks let mut file = File::create(&dl_path_file)?; let mut stream = res.bytes_stream(); @@ -39,7 +42,7 @@ async fn download_file(url: String, path: String, name: String) -> Result<(), Bo Ok(()) } -pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> Result<(), Box> { +pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> MLE<()> { println!("Disabling version {} for mod {}", versionid, mod_id); let file = get_file_path(current_list.clone(), String::from(&versionid))?; let disabled = format!("{}.disabled", file); @@ -51,7 +54,7 @@ pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_i Ok(()) } -pub fn delete_version(list: List, version: String) -> Result<(), Box> { +pub fn delete_version(list: List, version: String) -> MLE<()> { let file = get_file_path(list, version)?; remove_file(file)?; @@ -59,19 +62,25 @@ pub fn delete_version(list: List, version: String) -> Result<(), Box Result> { +pub fn get_file_path(list: List, versionid: String) -> MLE { let mut names: HashMap = HashMap::new(); for file in read_dir(list.download_folder)? { let path = file?.path(); if path.is_file() { - let pathstr = path.to_str().ok_or("BAH")?; + let pathstr = match path.to_str().ok_or("") { + Ok(s) => s, + Err(..) => return Err(MLError::new(ErrorType::Other, "INVALID_PATH")) + }; let namesplit: Vec<&str> = pathstr.split('.').collect(); let ver_id = namesplit[namesplit.len() - 2]; names.insert(String::from(ver_id), String::from(pathstr)); } }; - let filename = names.get(&versionid).ok_or("VERSION_NOT_FOUND_IN_FILES")?; + let filename = match names.get(&versionid).ok_or("") { + Ok(n) => n, + Err(..) => return Err(MLError::new(ErrorType::ArgumentError, "VERSION_NOT_FOUND_IN_FILES")) + }; Ok(filename.to_owned()) } -- cgit v1.2.3