From 7fe8fa06e740f405ab60d78ac04c22ffa549fe8e Mon Sep 17 00:00:00 2001 From: Drew Short Date: Wed, 6 Jan 2016 14:28:53 -0600 Subject: [PATCH] Project warnings cleanup Cache filling methods follow standard and return a Result The methods to place the data in the cache from the hash source are wrapped in a match statement. --- src/cache.rs | 64 +++++++++++++++++++++++++++++++++++++--------------- src/hash.rs | 29 ++++++++++++++++-------- 2 files changed, 66 insertions(+), 27 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index ef66567..4df0621 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -47,7 +47,10 @@ fn get_file_hash(path: &Path) -> Result { /** * Put an image buffer in the cache */ -pub fn put_image_in_cache(path: &Path, size: u32, image: &ImageBuffer, Vec>) { +pub fn put_image_in_cache(path: &Path, + size: u32, + image: &ImageBuffer, Vec>) + -> Result { let hash = get_file_hash(&path); match hash { Ok(sha1) => { @@ -61,11 +64,18 @@ pub fn put_image_in_cache(path: &Path, size: u32, image: &ImageBuffer {} - Err(e) => println!("Error: {}", e), + Err(e) => { + println!("Error: {}", e); + return Err(e); + } } } - Err(e) => println!("Error: {}", e), + Err(e) => { + println!("Error: {}", e); + return Err(e); + } } + Ok(true) } /** @@ -74,7 +84,8 @@ pub fn put_image_in_cache(path: &Path, size: u32, image: &ImageBuffer>) { + file_contents: &Vec>) + -> Result { let hash = get_file_hash(&path); match hash { Ok(sha1) => { @@ -91,20 +102,29 @@ pub fn put_matrix_in_cache(path: &Path, let desire_len = row_str.len() - 1; row_str.truncate(desire_len); row_str.push_str("\n"); - compressor.write(&row_str.into_bytes()); + try!(compressor.write(&row_str.into_bytes())); } let compressed_matrix = match compressor.finish() { Ok(data) => data, - Err(e) => { println!("Unable to compress matrix data: {}", e); return }, + Err(e) => { + println!("Unable to compress matrix data: {}", e); + return Err(e); + } }; - file.write(&compressed_matrix); - file.flush(); + try!(file.write(&compressed_matrix)); + try!(file.flush()); + } + Err(e) => { + return Err(e); } - Err(_) => {} } } - Err(e) => println!("Error: {}", e), + Err(e) => { + println!("Error: {}", e); + return Err(e); + } } + Ok(true) } /** @@ -154,19 +174,27 @@ pub fn get_matrix_from_cache(path: &Path, size: u32, extension: &str) -> Option< let cached_path = Path::new(&cache_path_str); // Try to open, if it does, then we can read the image in match File::open(&cached_path) { - Ok(mut file) => { - let mut compressed_matrix_data: Vec = Vec::new(); + Ok(file) => { let mut decoder = ZlibDecoder::new(&file); let mut matrix_data_str = String::new(); match decoder.read_to_string(&mut matrix_data_str) { - Ok(_) => {}, - Err(e) => { println!("Unable to decompress matrix: {}",e); return None } + Ok(_) => {} + Err(e) => { + println!("Unable to decompress matrix: {}", e); + return None; + } }; // convert the matrix - let matrix: Vec> = matrix_data_str.trim().split("\n") - .map(|line| line.split(",") - .map(|f| f64::from_str(f).unwrap()).collect()) - .collect(); + let matrix: Vec> = matrix_data_str.trim() + .split("\n") + .map(|line| { + line.split(",") + .map(|f| { + f64::from_str(f).unwrap() + }) + .collect() + }) + .collect(); Some(matrix) } diff --git a/src/hash.rs b/src/hash.rs index 0887700..83e66fb 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -119,7 +119,10 @@ pub fn prepare_image<'a>(path: &'a Path, let image = image::open(path).unwrap(); let small_image = image.resize_exact(size, size, FilterType::Lanczos3); let grey_image = small_image.to_luma(); - cache::put_image_in_cache(&path, size, &grey_image); + match cache::put_image_in_cache(&path, size, &grey_image) { + Ok(_) => {} + Err(e) => println!("Unable to store image in cache. {}", e), + }; PreparedImage { orig_path: &*image_path, image: grey_image, @@ -298,28 +301,36 @@ impl<'a> PerceptualHash for PHash<'a> { // Pretty fast already, so caching doesn't make a huge difference // Atleast compared to opening and processing the images let mut data_matrix: Vec> = Vec::new(); - match cache::get_matrix_from_cache(&Path::new(self.prepared_image.orig_path), width as u32, &"dft") { + match cache::get_matrix_from_cache(&Path::new(self.prepared_image.orig_path), + width as u32, + &"dft") { Some(matrix) => data_matrix = matrix, None => { - //Preparing the results + // Preparing the results for x in 0..width { data_matrix.push(Vec::new()); for y in 0..height { let pos_x = x as u32; let pos_y = y as u32; data_matrix[x] - .push(self.prepared_image.image.get_pixel(pos_x, pos_y).channels()[0] as f64); + .push(self.prepared_image + .image + .get_pixel(pos_x, pos_y) + .channels()[0] as f64); } } // Perform the 2D DFT operation on our matrix calculate_2d_dft(&mut data_matrix); // Store this DFT in the cache - cache::put_matrix_in_cache(&Path::new(self.prepared_image.orig_path), - width as u32, - &"dft", - &data_matrix); - }, + match cache::put_matrix_in_cache(&Path::new(self.prepared_image.orig_path), + width as u32, + &"dft", + &data_matrix) { + Ok(_) => {} + Err(e) => println!("Unable to store matrix in cache. {}", e), + }; + } } // Only need the top left quadrant