feat: unify digest plugin types and always compute all hashes

Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-26 23:38:35 -03:00
parent cb83cc4b77
commit 538d565341
2 changed files with 41 additions and 116 deletions

View File

@@ -2,53 +2,27 @@ use sha2::{Digest, Sha256, Sha512};
use md5::Md5;
use crate::meta_plugin::MetaPlugin;
#[derive(Debug, Clone)]
enum HashMethod {
Md5,
Sha256,
Sha512,
}
impl Default for HashMethod {
fn default() -> Self {
HashMethod::Sha256
}
}
impl HashMethod {
fn from_str(s: &str) -> Option<Self> {
match s {
"md5" => Some(HashMethod::Md5),
"sha256" => Some(HashMethod::Sha256),
"sha512" => Some(HashMethod::Sha512),
_ => None,
}
}
}
#[derive(Debug, Clone)]
pub struct DigestMetaPlugin {
md5_hasher: Option<Md5>,
sha256_hasher: Option<Sha256>,
sha512_hasher: Option<Sha512>,
md5_hasher: Md5,
sha256_hasher: Sha256,
sha512_hasher: Sha512,
is_finalized: bool,
meta_name: String,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
options: std::collections::HashMap<String, serde_yaml::Value>,
methods: Vec<HashMethod>,
}
impl Default for DigestMetaPlugin {
fn default() -> Self {
Self {
md5_hasher: None,
sha256_hasher: None,
sha512_hasher: None,
md5_hasher: Md5::new(),
sha256_hasher: Sha256::new(),
sha512_hasher: Sha512::new(),
is_finalized: false,
meta_name: "digest".to_string(),
outputs: std::collections::HashMap::new(),
options: std::collections::HashMap::new(),
methods: vec![HashMethod::Sha256],
}
}
}
@@ -84,24 +58,6 @@ impl DigestMetaPlugin {
}
}
// Configure methods based on options
if let Some(method_value) = plugin.options.get("method") {
if let Some(method_str) = method_value.as_str() {
if let Some(method) = HashMethod::from_str(method_str) {
plugin.methods = vec![method];
}
}
}
// Initialize hashers based on selected methods
for method in &plugin.methods {
match method {
HashMethod::Md5 => plugin.md5_hasher = Some(Md5::new()),
HashMethod::Sha256 => plugin.sha256_hasher = Some(Sha256::new()),
HashMethod::Sha512 => plugin.sha512_hasher = Some(Sha512::new()),
}
}
plugin
}
@@ -136,49 +92,35 @@ impl MetaPlugin for DigestMetaPlugin {
let mut metadata = Vec::new();
// Process each method
for method in &self.methods {
match method {
HashMethod::Md5 => {
if let Some(hasher) = self.md5_hasher.take() {
let hash_result = hasher.finalize();
let hex_string = format!("{:x}", hash_result);
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
"digest_md5",
hex_string,
&self.outputs
) {
metadata.push(meta_data);
}
}
}
HashMethod::Sha256 => {
if let Some(hasher) = self.sha256_hasher.take() {
let hash_result = hasher.finalize_reset();
let hex_string = format!("{:x}", hash_result);
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
"digest_sha256",
hex_string,
&self.outputs
) {
metadata.push(meta_data);
}
}
}
HashMethod::Sha512 => {
if let Some(hasher) = self.sha512_hasher.take() {
let hash_result = hasher.finalize_reset();
let hex_string = format!("{:x}", hash_result);
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
"digest_sha512",
hex_string,
&self.outputs
) {
metadata.push(meta_data);
}
}
}
}
// Always compute all three hashes
let md5_result = self.md5_hasher.clone().finalize();
let md5_hex = format!("{:x}", md5_result);
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
"digest_md5",
md5_hex,
&self.outputs
) {
metadata.push(meta_data);
}
let sha256_result = self.sha256_hasher.clone().finalize_reset();
let sha256_hex = format!("{:x}", sha256_result);
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
"digest_sha256",
sha256_hex,
&self.outputs
) {
metadata.push(meta_data);
}
let sha512_result = self.sha512_hasher.clone().finalize_reset();
let sha512_hex = format!("{:x}", sha512_result);
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
"digest_sha512",
sha512_hex,
&self.outputs
) {
metadata.push(meta_data);
}
self.is_finalized = true;
@@ -196,25 +138,10 @@ impl MetaPlugin for DigestMetaPlugin {
};
}
for method in &self.methods {
match method {
HashMethod::Md5 => {
if let Some(hasher) = &mut self.md5_hasher {
hasher.update(data);
}
}
HashMethod::Sha256 => {
if let Some(hasher) = &mut self.sha256_hasher {
hasher.update(data);
}
}
HashMethod::Sha512 => {
if let Some(hasher) = &mut self.sha512_hasher {
hasher.update(data);
}
}
}
}
// Update all hashers
self.md5_hasher.update(data);
self.sha256_hasher.update(data);
self.sha512_hasher.update(data);
crate::meta_plugin::MetaPluginResponse {
metadata: Vec::new(),

View File

@@ -133,8 +133,7 @@ pub enum MetaPluginType {
Shell,
ShellPid,
KeepPid,
DigestSha256,
DigestMd5,
Digest,
ReadTime,
ReadRate,
Hostname,
@@ -296,8 +295,7 @@ pub fn get_meta_plugin(meta_plugin_type: MetaPluginType) -> Box<dyn MetaPlugin>
MetaPluginType::Shell => Box::new(ShellMetaPlugin::new_simple()),
MetaPluginType::ShellPid => Box::new(ShellPidMetaPlugin::new_simple()),
MetaPluginType::KeepPid => Box::new(KeepPidMetaPlugin::new_simple()),
MetaPluginType::DigestSha256 => Box::new(DigestMetaPlugin::new_simple()),
MetaPluginType::DigestMd5 => Box::new(MetaPluginProgram::new_simple("md5sum", vec![], "digest_md5".to_string(), true)),
MetaPluginType::Digest => Box::new(DigestMetaPlugin::new_simple()),
MetaPluginType::ReadTime => Box::new(ReadTimeMetaPlugin::new_simple()),
MetaPluginType::ReadRate => Box::new(ReadRateMetaPlugin::new_simple()),
MetaPluginType::Hostname => Box::new(HostnameMetaPlugin::new_simple()),