diff --git a/src/output.rs b/src/output.rs index 2fee824..0c80516 100644 --- a/src/output.rs +++ b/src/output.rs @@ -1,35 +1,40 @@ -use crate::database::File; +use std::{collections::HashMap, fs}; + +use anyhow::Result; use chrono::offset::Utc; use chrono::DateTime; use colored::Colorize; use humansize::{format_size, DECIMAL}; -use std::{collections::HashMap, fs}; + +use crate::database::File; use crate::params::Params; -fn format_path(path: &String, opts: &Params) -> String { - let display_path = path.replace(&opts.get_directory().unwrap(), ""); +fn format_path(path: &str, opts: &Params) -> Result { + let display_path = path.replace(&opts.get_directory()?, ""); let text_vec = display_path.chars().collect::>(); let display_range = if text_vec.len() > 32 { - text_vec[(display_path.len() - 32)..].into_iter().collect::() + text_vec[(display_path.len() - 32)..] + .iter() + .collect::() } else { display_path }; - - format!("...{}", display_range) + + Ok(format!("...{}", display_range)) } -fn file_size(path: &String) -> String { - let mdata = fs::metadata(path).unwrap(); +fn file_size(path: &String) -> Result { + let mdata = fs::metadata(path)?; let formatted_size = format_size(mdata.len(), DECIMAL); - format!("{}", formatted_size) + Ok(formatted_size) } -fn modified_time(path: &String) -> String { - let mdata = fs::metadata(path).unwrap(); - let modified_time: DateTime = mdata.modified().unwrap().into(); +fn modified_time(path: &String) -> Result { + let mdata = fs::metadata(path)?; + let modified_time: DateTime = mdata.modified()?.into(); - modified_time.format("%Y-%m-%d %H:%M:%S").to_string() + Ok(modified_time.format("%Y-%m-%d %H:%M:%S").to_string()) } fn print_divider() { @@ -50,17 +55,26 @@ pub fn print(duplicates: Vec, opts: &Params) { dup_index .entry(file.hash.clone()) .and_modify(|value| value.push(file.clone())) - .or_insert(vec![file]); + .or_insert_with(|| vec![file]); }); dup_index.into_iter().for_each(|(_, group)| { group.into_iter().for_each(|file| { + let Ok(path) = format_path(&file.path, opts) else { + return; + }; + let Ok(file_size) = file_size(&file.path) else { + return; + }; + let Ok(modified_time) = modified_time(&file.path) else { + return; + }; println!( "| {0: <16} | {1: <35} | {2: <16} | {3: <32} |", file.hash.red(), - format_path(&file.path, opts).yellow(), - file_size(&file.path).blue(), - modified_time(&file.path).blue() + path.yellow(), + file_size.blue(), + modified_time.blue() ); }); print_divider();