Skip to content

Commit

Permalink
refactor: update Checksum functionality to support optional output path
Browse files Browse the repository at this point in the history
  • Loading branch information
Kremilly committed Dec 24, 2024
1 parent 5a118bb commit c8ddc3c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/args_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,5 @@ pub struct ChecksumOptions {

#[arg(short, long)]
/// Output file path to save checksum
pub output: String,
pub output: Option<String>,
}
2 changes: 1 addition & 1 deletion src/dump_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl DumpSync {

let _ = Checksum::new(
&file,
&output,
output.as_deref(),
).generated();
}

Expand Down
48 changes: 32 additions & 16 deletions src/plugins/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

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)?;
Expand All @@ -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(())
}

Expand Down
32 changes: 32 additions & 0 deletions src/ui/checksum_alerts.rs
Original file line number Diff line number Diff line change
@@ -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()
);
}

}
3 changes: 3 additions & 0 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 0 additions & 10 deletions src/ui/success_alerts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

}

0 comments on commit c8ddc3c

Please sign in to comment.