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 md5::Md5;
|
||||||
use crate::meta_plugin::MetaPlugin;
|
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)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct DigestMetaPlugin {
|
pub struct DigestMetaPlugin {
|
||||||
md5_hasher: Md5,
|
hasher: Option<Hasher>,
|
||||||
sha256_hasher: Sha256,
|
|
||||||
sha512_hasher: Sha512,
|
|
||||||
is_finalized: bool,
|
is_finalized: bool,
|
||||||
meta_name: String,
|
meta_name: String,
|
||||||
outputs: std::collections::HashMap<String, serde_yaml::Value>,
|
outputs: std::collections::HashMap<String, serde_yaml::Value>,
|
||||||
@@ -16,9 +56,7 @@ pub struct DigestMetaPlugin {
|
|||||||
impl Default for DigestMetaPlugin {
|
impl Default for DigestMetaPlugin {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
md5_hasher: Md5::new(),
|
hasher: None,
|
||||||
sha256_hasher: Sha256::new(),
|
|
||||||
sha512_hasher: Sha512::new(),
|
|
||||||
is_finalized: false,
|
is_finalized: false,
|
||||||
meta_name: "digest".to_string(),
|
meta_name: "digest".to_string(),
|
||||||
outputs: std::collections::HashMap::new(),
|
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
|
plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,35 +147,18 @@ impl MetaPlugin for DigestMetaPlugin {
|
|||||||
|
|
||||||
let mut metadata = Vec::new();
|
let mut metadata = Vec::new();
|
||||||
|
|
||||||
// Always compute all three hashes
|
// Compute the selected hash
|
||||||
let md5_result = self.md5_hasher.clone().finalize();
|
if let Some(hasher) = &mut self.hasher {
|
||||||
let md5_hex = format!("{:x}", md5_result);
|
let hash_value = hasher.finalize();
|
||||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
let output_name = hasher.output_name();
|
||||||
"digest_md5",
|
|
||||||
md5_hex,
|
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||||
&self.outputs
|
output_name,
|
||||||
) {
|
hash_value,
|
||||||
metadata.push(meta_data);
|
&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;
|
self.is_finalized = true;
|
||||||
@@ -138,10 +176,10 @@ impl MetaPlugin for DigestMetaPlugin {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update all hashers
|
// Update the active hasher
|
||||||
self.md5_hasher.update(data);
|
if let Some(hasher) = &mut self.hasher {
|
||||||
self.sha256_hasher.update(data);
|
hasher.update(data);
|
||||||
self.sha512_hasher.update(data);
|
}
|
||||||
|
|
||||||
crate::meta_plugin::MetaPluginResponse {
|
crate::meta_plugin::MetaPluginResponse {
|
||||||
metadata: Vec::new(),
|
metadata: Vec::new(),
|
||||||
|
|||||||
Reference in New Issue
Block a user