feat: implement single hasher selection for digest plugin
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -2,11 +2,51 @@ use sha2::{Digest, Sha256, Sha512};
|
||||
use md5::Md5;
|
||||
use crate::meta_plugin::MetaPlugin;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Hasher {
|
||||
Md5(Md5),
|
||||
Sha256(Sha256),
|
||||
Sha512(Sha512),
|
||||
}
|
||||
|
||||
impl Hasher {
|
||||
fn update(&mut self, data: &[u8]) {
|
||||
match self {
|
||||
Hasher::Md5(hasher) => hasher.update(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 = std::mem::replace(hasher, Md5::new()).finalize();
|
||||
format!("{:x}", result)
|
||||
}
|
||||
Hasher::Sha256(hasher) => {
|
||||
let result = std::mem::replace(hasher, Sha256::new()).finalize_reset();
|
||||
format!("{:x}", result)
|
||||
}
|
||||
Hasher::Sha512(hasher) => {
|
||||
let result = std::mem::replace(hasher, Sha512::new()).finalize_reset();
|
||||
format!("{:x}", result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn output_name(&self) -> &'static str {
|
||||
match self {
|
||||
Hasher::Md5(_) => "digest_md5",
|
||||
Hasher::Sha256(_) => "digest_sha256",
|
||||
Hasher::Sha512(_) => "digest_sha512",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DigestMetaPlugin {
|
||||
md5_hasher: Md5,
|
||||
sha256_hasher: Sha256,
|
||||
sha512_hasher: Sha512,
|
||||
hasher: Option<Hasher>,
|
||||
is_finalized: bool,
|
||||
meta_name: String,
|
||||
outputs: std::collections::HashMap<String, serde_yaml::Value>,
|
||||
@@ -16,9 +56,7 @@ pub struct DigestMetaPlugin {
|
||||
impl Default for DigestMetaPlugin {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
md5_hasher: Md5::new(),
|
||||
sha256_hasher: Sha256::new(),
|
||||
sha512_hasher: Sha512::new(),
|
||||
hasher: None,
|
||||
is_finalized: false,
|
||||
meta_name: "digest".to_string(),
|
||||
outputs: std::collections::HashMap::new(),
|
||||
@@ -58,6 +96,23 @@ impl DigestMetaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the hasher based on the method option
|
||||
if let Some(method_value) = plugin.options.get("method") {
|
||||
if let Some(method_str) = method_value.as_str() {
|
||||
plugin.hasher = match method_str {
|
||||
"md5" => Some(Hasher::Md5(Md5::new())),
|
||||
"sha256" => Some(Hasher::Sha256(Sha256::new())),
|
||||
"sha512" => Some(Hasher::Sha512(Sha512::new())),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Default to sha256 if no valid method is specified
|
||||
if plugin.hasher.is_none() {
|
||||
plugin.hasher = Some(Hasher::Sha256(Sha256::new()));
|
||||
}
|
||||
|
||||
plugin
|
||||
}
|
||||
|
||||
@@ -92,35 +147,18 @@ impl MetaPlugin for DigestMetaPlugin {
|
||||
|
||||
let mut metadata = Vec::new();
|
||||
|
||||
// Always compute all three hashes
|
||||
let md5_result = self.md5_hasher.clone().finalize();
|
||||
let md5_hex = format!("{:x}", md5_result);
|
||||
// Compute the selected hash
|
||||
if let Some(hasher) = &mut self.hasher {
|
||||
let hash_value = hasher.finalize();
|
||||
let output_name = hasher.output_name();
|
||||
|
||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
"digest_md5",
|
||||
md5_hex,
|
||||
output_name,
|
||||
hash_value,
|
||||
&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;
|
||||
@@ -138,10 +176,10 @@ impl MetaPlugin for DigestMetaPlugin {
|
||||
};
|
||||
}
|
||||
|
||||
// Update all hashers
|
||||
self.md5_hasher.update(data);
|
||||
self.sha256_hasher.update(data);
|
||||
self.sha512_hasher.update(data);
|
||||
// Update the active hasher
|
||||
if let Some(hasher) = &mut self.hasher {
|
||||
hasher.update(data);
|
||||
}
|
||||
|
||||
crate::meta_plugin::MetaPluginResponse {
|
||||
metadata: Vec::new(),
|
||||
|
||||
Reference in New Issue
Block a user