From a8324ca73c4ba759e237da2d8cf9019015dbdc04 Mon Sep 17 00:00:00 2001 From: peshwar9 Date: Tue, 24 Nov 2020 22:18:21 +0530 Subject: [PATCH] chapter 4 FD revisions --- chapter4/Cargo.toml | 1 - chapter4/src/imagecli.rs | 6 +++--- chapter4/src/imagix/error.rs | 6 +++--- chapter4/src/imagix/resize.rs | 10 ++++------ chapter4/src/imagix/stats.rs | 8 +++++--- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/chapter4/Cargo.toml b/chapter4/Cargo.toml index cbbb971..d70e22e 100644 --- a/chapter4/Cargo.toml +++ b/chapter4/Cargo.toml @@ -4,7 +4,6 @@ version = "0.1.0" authors = ["peshwar9"] edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] name = "imagix" diff --git a/chapter4/src/imagecli.rs b/chapter4/src/imagecli.rs index 3838897..af994bd 100644 --- a/chapter4/src/imagecli.rs +++ b/chapter4/src/imagecli.rs @@ -1,3 +1,4 @@ +#[allow(dead_code)] mod imagix; use ::imagix::error::ImagixError; use ::imagix::resize::{process_resize_request, Mode, SizeOption}; @@ -35,10 +36,9 @@ fn main() { Commandline::Resize { size, mode, - srcfolder, + mut srcfolder, } => { - let mut src_folder = srcfolder; - match process_resize_request(size, mode, &mut src_folder) { + match process_resize_request(size, mode, &mut srcfolder) { Ok(_) => println!("Image(s) resized successfully"), Err(e) => match e { ImagixError::FileIOError(e) => println!("{}", e), diff --git a/chapter4/src/imagix/error.rs b/chapter4/src/imagix/error.rs index 901c83d..d701e38 100644 --- a/chapter4/src/imagix/error.rs +++ b/chapter4/src/imagix/error.rs @@ -11,19 +11,19 @@ pub enum ImagixError { } impl From for ImagixError { - fn from(error: io::Error) -> Self { + fn from(_error: io::Error) -> Self { ImagixError::FileIOError("Error in File I/O".to_string()) } } impl From for ImagixError { - fn from(error: error::ImageError) -> Self { + fn from(_error: error::ImageError) -> Self { ImagixError::ImageResizingError("Error in image processing".to_string()) } } impl From for ImagixError { - fn from(error: io::ErrorKind) -> Self { + fn from(_error: io::ErrorKind) -> Self { ImagixError::UserInputError("Error in user input".to_string()) } } diff --git a/chapter4/src/imagix/resize.rs b/chapter4/src/imagix/resize.rs index 6fc952a..d90f422 100644 --- a/chapter4/src/imagix/resize.rs +++ b/chapter4/src/imagix/resize.rs @@ -38,9 +38,7 @@ impl FromStr for SizeOption { "small" => Ok(SizeOption::Small), "medium" => Ok(SizeOption::Medium), "large" => Ok(SizeOption::Large), - _ => Err(ImagixError::UserInputError( - "Invalid input for size".to_string(), - )), + _ => Ok(SizeOption::Small), //default } } @@ -104,7 +102,6 @@ fn resize_image(size: u32, src_folder: &mut PathBuf) -> Result<(), ImagixError> .unwrap() .to_str() .ok_or(std::io::ErrorKind::InvalidInput) - // .map(|f| format!("{}-{:?}.png", f, size)); .map(|f| format!("{}.png", f)); // Construct path to destination folder i.e. create /tmp under source folder if not exists @@ -135,9 +132,10 @@ fn resize_image(size: u32, src_folder: &mut PathBuf) -> Result<(), ImagixError> Ok(()) } +// The program supports only files of type jpg/JPG and png/PNG. pub fn get_image_files(src_folder: PathBuf) -> Result, ImagixError> { let entries = fs::read_dir(src_folder) - .map_err(|e| ImagixError::UserInputError("Invalid source folder".to_string()))? + .map_err(|_e| ImagixError::UserInputError("Invalid source folder".to_string()))? .map(|res| res.map(|e| e.path())) .collect::, io::Error>>()? .into_iter() @@ -159,7 +157,7 @@ mod tests { let mut path = PathBuf::from("/tmp/images/image1.jpg"); let destination_path = PathBuf::from("/tmp/images/tmp/image1.png"); match process_resize_request(SizeOption::Small, Mode::Single, &mut path) { - Ok(dest) => assert_eq!(true, destination_path.exists()), + Ok(_) => println!("Successful resize of single image"), Err(e) => println!("Error in single image: {:?}", e), } assert_eq!(true, destination_path.exists()); diff --git a/chapter4/src/imagix/stats.rs b/chapter4/src/imagix/stats.rs index 4184dd1..32bf092 100644 --- a/chapter4/src/imagix/stats.rs +++ b/chapter4/src/imagix/stats.rs @@ -2,13 +2,13 @@ use super::error::ImagixError; use super::resize::get_image_files; use std::path::PathBuf; -pub fn get_stats(src_folder: PathBuf) -> Result<(usize, u64), ImagixError> { +pub fn get_stats(src_folder: PathBuf) -> Result<(usize, f64), ImagixError> { let image_files = get_image_files(src_folder.to_path_buf())?; let size = image_files .iter() .map(move |f| f.metadata().unwrap().len()) .sum::(); - Ok((image_files.len(), size / 1000000)) + Ok((image_files.len(), (size / 1000000) as f64)) } #[cfg(test)] @@ -18,7 +18,9 @@ mod tests { fn test_get_stats() { let path = PathBuf::from("/tmp/images"); let (count, size) = get_stats(path).unwrap(); + // Note: For this test to pass, + // alter the count and size with the right values assert_eq!(count, 2); - assert_eq!(size, 8); + assert_eq!(size, 17.0); } }