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:
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user