chapter 4 FD revisions

master
peshwar9 3 years ago
parent d481b0cb64
commit a8324ca73c

@ -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"

@ -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),

@ -11,19 +11,19 @@ pub enum ImagixError {
}
impl From<io::Error> 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<error::ImageError> for ImagixError {
fn from(error: error::ImageError) -> Self {
fn from(_error: error::ImageError) -> Self {
ImagixError::ImageResizingError("Error in image processing".to_string())
}
}
impl From<io::ErrorKind> for ImagixError {
fn from(error: io::ErrorKind) -> Self {
fn from(_error: io::ErrorKind) -> Self {
ImagixError::UserInputError("Error in user input".to_string())
}
}

@ -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<Vec<PathBuf>, 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::<Result<Vec<_>, 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());

@ -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::<u64>();
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);
}
}

Loading…
Cancel
Save