Files
keep/src/meta_plugin/digest.rs
Andrew Phillips fed3722ef9 fix: Resolve compilation errors by refactoring imports and type annotations
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
2025-09-03 09:39:22 -03:00

257 lines
7.9 KiB
Rust

use sha2::{Digest, Sha256, Sha512};
use md5;
use crate::meta_plugin::{MetaPlugin, MetaPluginType};
use std::io::Write;
#[derive(Clone)]
enum Hasher {
Md5(md5::Context),
Sha256(Sha256),
Sha512(Sha512),
}
// 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::Sha512(_) => write!(f, "Hasher::Sha512"),
}
}
}
impl Hasher {
fn update(&mut self, data: &[u8]) {
match self {
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::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 {
hasher: Option<Hasher>,
is_finalized: bool,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
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(
options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> DigestMetaPlugin {
let mut plugin = DigestMetaPlugin::default();
// Apply provided options
if let Some(opts) = options {
for (key, value) in opts {
plugin.options.insert(key, value);
}
}
// Determine the selected method
let method = if let Some(method_value) = plugin.options.get("method") {
if let Some(method_str) = method_value.as_str() {
match method_str {
"md5" => "md5",
"sha256" => "sha256",
"sha512" => "sha512",
_ => "sha256",
}
} else {
"sha256"
}
} else {
"sha256"
};
// Initialize the hasher based on the method
plugin.hasher = match method {
"md5" => Some(Hasher::Md5(md5::Context::new())),
"sha256" => Some(Hasher::Sha256(Sha256::new())),
"sha512" => Some(Hasher::Sha512(Sha512::new())),
_ => Some(Hasher::Sha256(Sha256::new())),
};
// 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
// 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 {
if output_name == format!("digest_{}", method) {
plugin.outputs.insert(output_name.to_string(), serde_yaml::Value::String(output_name.to_string()));
} else {
plugin.outputs.insert(output_name.to_string(), serde_yaml::Value::Null);
}
}
// Apply provided outputs, but only for enabled outputs
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;
}
}
}
}
plugin
}
}
impl MetaPlugin for DigestMetaPlugin {
fn is_finalized(&self) -> bool {
self.is_finalized
}
fn set_finalized(&mut self, finalized: bool) {
self.is_finalized = finalized;
}
fn initialize(&mut self) -> crate::meta_plugin::MetaPluginResponse {
crate::meta_plugin::MetaPluginResponse {
metadata: Vec::new(),
is_finalized: false,
}
}
fn finalize(&mut self) -> crate::meta_plugin::MetaPluginResponse {
if self.is_finalized {
return crate::meta_plugin::MetaPluginResponse {
metadata: Vec::new(),
is_finalized: true,
};
}
let metadata = Vec::new();
// Update outputs based on the selected hash method
if let Some(hasher) = &mut self.hasher {
let hash_value = hasher.finalize();
let output_name = hasher.output_name();
// Set the selected hash output
self.outputs.insert(output_name.to_string(), serde_yaml::Value::String(hash_value));
// Set all other digest outputs to None
let all_outputs = vec!["digest_md5", "digest_sha256", "digest_sha512"];
for output_name in all_outputs {
if output_name != hasher.output_name() {
self.outputs.insert(output_name.to_string(), serde_yaml::Value::Null);
}
}
}
self.is_finalized = true;
crate::meta_plugin::MetaPluginResponse {
metadata,
is_finalized: true,
}
}
fn update(&mut self, data: &[u8]) -> crate::meta_plugin::MetaPluginResponse {
if self.is_finalized {
return crate::meta_plugin::MetaPluginResponse {
metadata: Vec::new(),
is_finalized: true,
};
}
// Update the active hasher
if let Some(hasher) = &mut self.hasher {
hasher.update(data);
}
crate::meta_plugin::MetaPluginResponse {
metadata: Vec::new(),
is_finalized: false,
}
}
fn meta_type(&self) -> MetaPluginType {
MetaPluginType::Digest
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec![
"digest_md5".to_string(),
"digest_sha256".to_string(),
"digest_sha512".to_string(),
]
}
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
}
}
use crate::meta_plugin::register_meta_plugin;
// Register the plugin at module initialization time
#[ctor::ctor]
fn register_digest_plugin() {
register_meta_plugin(MetaPluginType::Digest, |options, outputs| {
Box::new(DigestMetaPlugin::new(options, outputs))
});
}