From 29635b9e3833296b2c908914104ba7165d22d3d5 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 12:03:13 +0200 Subject: remove `# Panics` and fix clippy --- src/cache.rs | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) (limited to 'src/cache.rs') diff --git a/src/cache.rs b/src/cache.rs index ef096f7..6ffeb52 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -3,17 +3,15 @@ use std::{ fs::{copy, read_dir}, }; -/// . -/// -/// # Panics -/// -/// Panics if . -#[must_use] pub fn get_cached_versions(path: &str) -> HashMap { +use crate::error::{EType, MLErr, MLE}; + +/// # Errors +pub fn get_cached_versions(path: &str) -> MLE> { let mut versions: HashMap = HashMap::new(); - for file in read_dir(path).unwrap() { - let path = file.unwrap().path(); - if path.is_file() && path.extension().ok_or("BAH").unwrap() == "jar" { - let pathstr = path.to_str().ok_or("BAH").unwrap(); + for file in read_dir(path).map_err(|_| MLErr::new(EType::IoError, "readdir"))? { + let path = file.map_err(|_| MLErr::new(EType::IoError, "file"))?.path(); + if path.is_file() && path.extension().ok_or(MLErr::new(EType::IoError, "ext"))? == "jar" { + let pathstr = path.to_str().ok_or(MLErr::new(EType::IoError, "path"))?; let namesplit: Vec<&str> = pathstr.split('.').collect(); versions.insert( String::from(namesplit[namesplit.len() - 2]), @@ -21,17 +19,14 @@ use std::{ ); } } - versions + Ok(versions) } -/// . -/// -/// # Panics -/// -/// Panics if . -pub fn copy_cached_version(version_path: &str, download_path: &str) { +/// # Errors +pub fn copy_cached_version(version_path: &str, download_path: &str) -> MLE<()> { let versplit: Vec<&str> = version_path.split('/').collect(); let download = format!("{}/{}", download_path, versplit[versplit.len() - 1]); - copy(version_path, download).unwrap(); + copy(version_path, download).map_err(|err| MLErr::new(EType::IoError, &err.to_string()))?; + Ok(()) } -- cgit v1.2.3