diff --git a/src/args_cli.rs b/src/args_cli.rs index 6da31a9..e1c4b7f 100644 --- a/src/args_cli.rs +++ b/src/args_cli.rs @@ -130,5 +130,5 @@ pub struct ChecksumOptions { #[arg(short, long)] /// Output file path to save checksum - pub output: String, + pub output: Option, } \ No newline at end of file diff --git a/src/dump_sync.rs b/src/dump_sync.rs index d0453cb..9bee2be 100644 --- a/src/dump_sync.rs +++ b/src/dump_sync.rs @@ -216,7 +216,7 @@ impl DumpSync { let _ = Checksum::new( &file, - &output, + output.as_deref(), ).generated(); } diff --git a/src/plugins/checksum.rs b/src/plugins/checksum.rs index 44365eb..ecfc473 100644 --- a/src/plugins/checksum.rs +++ b/src/plugins/checksum.rs @@ -15,29 +15,29 @@ use std::{ }, io::{ - self, Read, - Write + Write, + Result, }, }; -use crate::ui::success_alerts::SuccessAlerts; +use crate::ui::checksum_alerts::ChecksumAlerts; pub struct Checksum { file_path: String, - output_path: String, + output_path: Option, } impl Checksum { - pub fn new(file_path: &str, output_path: &str) -> Self { + pub fn new(file_path: &str, output_path: Option<&str>) -> Self { Self { file_path: file_path.to_string(), - output_path: output_path.to_string(), + output_path: output_path.map(|s| s.to_string()), } } - pub fn calculate_hashes(&self) -> io::Result<(u32, String, String, String)> { + pub fn calculate_hashes(&self) -> Result<(u32, String, String, String)> { let mut file = File::open(&self.file_path)?; let mut buffer = Vec::new(); file.read_to_end(&mut buffer)?; @@ -61,21 +61,37 @@ impl Checksum { Ok((crc32, md5, sha1, sha256)) } - pub fn generated(&self) -> io::Result<()> { + pub fn generated(&self) -> Result<()> { let (crc32, md5, sha1, sha256) = &self.calculate_hashes()?; + let _ = &self.printable()?; - let mut output_file = OpenOptions::new() + if let Some(output_path) = &self.output_path { + let mut output_file = OpenOptions::new() .write(true) .create(true) .truncate(true) - .open(&self.output_path)?; + .open(output_path)?; - writeln!(output_file, "CRC32: {:08x}", crc32)?; - writeln!(output_file, "MD5: {}", md5)?; - writeln!(output_file, "SHA1: {}", sha1)?; - writeln!(output_file, "SHA256: {}", sha256)?; - - SuccessAlerts::checksum(&self.output_path); + writeln!(output_file, "CRC32: {:08x}", crc32)?; + writeln!(output_file, "MD5: {}", md5)?; + writeln!(output_file, "SHA1: {}", sha1)?; + writeln!(output_file, "SHA256: {}", sha256)?; + + ChecksumAlerts::checksum(output_path); + } + + Ok(()) + } + + pub fn printable(&self) -> Result<()> { + let (crc32, md5, sha1, sha256) = &self.calculate_hashes()?; + + ChecksumAlerts::file(&self.file_path); + ChecksumAlerts::printable("crc32", &format!("{:08x}", crc32)); + ChecksumAlerts::printable("md5", &md5); + ChecksumAlerts::printable("sha1", &sha1); + ChecksumAlerts::printable("sha256", &sha256); + Ok(()) } diff --git a/src/ui/checksum_alerts.rs b/src/ui/checksum_alerts.rs new file mode 100644 index 0000000..ce476c9 --- /dev/null +++ b/src/ui/checksum_alerts.rs @@ -0,0 +1,32 @@ +extern crate colored; + +use colored::*; + +use crate::utils::date::Date; + +pub struct ChecksumAlerts; + +impl ChecksumAlerts { + + pub fn file(file: &str) { + println!("File: {}", file.blue()); + } + + pub fn checksum(file: &str) { + let current_datetime = Date::date_time(); + + println!( + "\r{} The checksum was successfully generated and saved in: {}", + current_datetime.green().bold(), + file.blue() + ); + } + + pub fn printable(algo: &str, hash: &str) { + println!( + "{}: {}", + algo.cyan(), hash.yellow() + ); + } + +} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index e685a92..0646291 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,8 +1,11 @@ pub mod ui_base; + pub mod scan_alerts; pub mod share_alerts; pub mod schema_alerts; pub mod report_alerts; +pub mod checksum_alerts; + pub mod normal_alerts; pub mod errors_alerts; pub mod success_alerts; \ No newline at end of file diff --git a/src/ui/success_alerts.rs b/src/ui/success_alerts.rs index 88ee8e4..e269b57 100644 --- a/src/ui/success_alerts.rs +++ b/src/ui/success_alerts.rs @@ -58,14 +58,4 @@ impl SuccessAlerts { ); } - pub fn checksum(file: &str) { - let current_datetime = Date::date_time(); - - println!( - "\r{} The checksum was successfully generated and saved in: {}", - current_datetime.green().bold(), - file.blue() - ); - } - }