refactor: simplify filter plugin signatures by removing boxed parameters

This commit is contained in:
Andrew Phillips
2025-09-12 10:36:09 -03:00
committed by Andrew Phillips (aider)
parent 9c354d5ef4
commit 059bde09e4
10 changed files with 158 additions and 301 deletions

View File

@@ -5,17 +5,23 @@ use std::io::Write;
#[derive(Clone)]
enum Hasher {
Md5(md5::Context),
Sha256(Sha256),
Md5(md5::Context),
Sha512(Sha512),
}
impl Default for Hasher {
fn default() -> Self {
Hasher::Sha256(Sha256::default())
}
}
// Manual Debug implementation to avoid md5::Context not implementing Debug
impl std::fmt::Debug for Hasher {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Hasher::Md5(_) => write!(f, "Hasher::Md5"),
Hasher::Sha256(_) => write!(f, "Hasher::Sha256"),
Hasher::Md5(_) => write!(f, "Hasher::Md5"),
Hasher::Sha512(_) => write!(f, "Hasher::Sha512"),
}
}
@@ -24,24 +30,24 @@ impl std::fmt::Debug for Hasher {
impl Hasher {
fn update(&mut self, data: &[u8]) {
match self {
Hasher::Sha256(hasher) => hasher.update(data),
Hasher::Md5(hasher) => {
let _ = hasher.write(data);
},
Hasher::Sha256(hasher) => hasher.update(data),
Hasher::Sha512(hasher) => hasher.update(data),
}
}
fn finalize(&mut self) -> String {
match self {
Hasher::Md5(hasher) => {
let result = hasher.clone().compute();
format!("{:x}", result)
}
Hasher::Sha256(hasher) => {
let result = std::mem::replace(hasher, Sha256::new()).finalize_reset();
format!("{:x}", result)
}
Hasher::Md5(hasher) => {
let result = hasher.clone().compute();
format!("{:x}", result)
}
Hasher::Sha512(hasher) => {
let result = std::mem::replace(hasher, Sha512::new()).finalize_reset();
format!("{:x}", result)
@@ -51,14 +57,14 @@ impl Hasher {
fn output_name(&self) -> &'static str {
match self {
Hasher::Md5(_) => "digest_md5",
Hasher::Sha256(_) => "digest_sha256",
Hasher::Md5(_) => "digest_md5",
Hasher::Sha512(_) => "digest_sha512",
}
}
}
#[derive(Debug, Clone)]
#[derive(Debug, Default)]
pub struct DigestMetaPlugin {
hasher: Option<Hasher>,
is_finalized: bool,
@@ -66,17 +72,6 @@ pub struct DigestMetaPlugin {
options: std::collections::HashMap<String, serde_yaml::Value>,
}
impl Default for DigestMetaPlugin {
fn default() -> Self {
Self {
hasher: None,
is_finalized: false,
outputs: std::collections::HashMap::new(),
options: std::collections::HashMap::new(),
}
}
}
impl DigestMetaPlugin {
pub fn new(
@@ -119,7 +114,7 @@ impl DigestMetaPlugin {
// Add the method to options so it shows up in the status
plugin.options.insert("method".to_string(), serde_yaml::Value::String(method.to_string()));
// Set outputs based on the selected method
// Set outputs based on the selected hash method
// Only the selected method's output should be enabled, others should be None
let all_outputs = vec!["digest_md5", "digest_sha256", "digest_sha512"];
for output_name in all_outputs {
@@ -134,10 +129,9 @@ impl DigestMetaPlugin {
if let Some(outs) = outputs {
for (key, value) in outs {
// Only update if the output is not disabled (not None)
if let Some(current_value) = plugin.outputs.get_mut(&key) {
if !current_value.is_null() {
*current_value = value;
}
if let Some(current_value) = plugin.outputs.get_mut(&key)
&& !current_value.is_null() {
*current_value = value;
}
}
}
@@ -253,4 +247,4 @@ fn register_digest_plugin() {
register_meta_plugin(MetaPluginType::Digest, |options, outputs| {
Box::new(DigestMetaPlugin::new(options, outputs))
});
}
}