From 5a65550e58ce7b86865045f16c2a648035f5efd4 Mon Sep 17 00:00:00 2001 From: beeb Date: Sun, 8 Jan 2023 13:20:15 +0100 Subject: [PATCH 1/3] refactor: output.rs --- src/output.rs | 50 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) 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(); From 5eba7cdc30c786245f19db34f6893935e422c2b1 Mon Sep 17 00:00:00 2001 From: beeb Date: Sun, 8 Jan 2023 13:21:47 +0100 Subject: [PATCH 2/3] fix: still print items even if file size or modified time cannot be retrieved --- src/output.rs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/output.rs b/src/output.rs index 0c80516..fc96ce1 100644 --- a/src/output.rs +++ b/src/output.rs @@ -60,21 +60,12 @@ pub fn print(duplicates: Vec, opts: &Params) { 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(), - path.yellow(), - file_size.blue(), - modified_time.blue() + format_path(&file.path, opts).unwrap_or_default().yellow(), + file_size(&file.path).unwrap_or_default().blue(), + modified_time(&file.path).unwrap_or_default().blue() ); }); print_divider(); From 6087abe960dbe133cb31975a43b5e1a8f9c3e279 Mon Sep 17 00:00:00 2001 From: beeb Date: Sun, 8 Jan 2023 13:24:08 +0100 Subject: [PATCH 3/3] refactor: no need for into_iter --- src/output.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/output.rs b/src/output.rs index fc96ce1..4824921 100644 --- a/src/output.rs +++ b/src/output.rs @@ -58,8 +58,8 @@ pub fn print(duplicates: Vec, opts: &Params) { .or_insert_with(|| vec![file]); }); - dup_index.into_iter().for_each(|(_, group)| { - group.into_iter().for_each(|file| { + dup_index.iter().for_each(|(_, group)| { + group.iter().for_each(|file| { println!( "| {0: <16} | {1: <35} | {2: <16} | {3: <32} |", file.hash.red(),