refactor: compose BaseMetaPlugin in remaining meta plugins

Co-authored-by: aider (openai/andrew/openrouter/sonoma-sky-alpha) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-09-12 12:21:03 -03:00
parent 8693061338
commit cb1f330231
7 changed files with 161 additions and 492 deletions

View File

@@ -1,6 +1,6 @@
use sha2::{Digest, Sha256, Sha512};
use md5;
use crate::meta_plugin::{MetaPlugin, MetaPluginType};
use crate::meta_plugin::{MetaPlugin, MetaPluginType, BaseMetaPlugin};
use std::io::Write;
#[derive(Clone)]
@@ -68,8 +68,7 @@ impl Hasher {
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>,
base: BaseMetaPlugin,
}
@@ -78,17 +77,17 @@ impl DigestMetaPlugin {
options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
) -> DigestMetaPlugin {
let mut plugin = DigestMetaPlugin::default();
let mut base = BaseMetaPlugin::new();
// Apply provided options
if let Some(opts) = options {
for (key, value) in opts {
plugin.options.insert(key, value);
base.options.insert(key, value);
}
}
// Determine the selected method
let method = if let Some(method_value) = plugin.options.get("method") {
let method = if let Some(method_value) = base.options.get("method") {
if let Some(method_str) = method_value.as_str() {
match method_str {
"md5" => "md5",
@@ -104,7 +103,7 @@ impl DigestMetaPlugin {
};
// Initialize the hasher based on the method
plugin.hasher = match method {
let hasher = match method {
"md5" => Some(Hasher::Md5(md5::Context::new())),
"sha256" => Some(Hasher::Sha256(Sha256::new())),
"sha512" => Some(Hasher::Sha512(Sha512::new())),
@@ -112,16 +111,16 @@ impl DigestMetaPlugin {
};
// 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()));
base.options.insert("method".to_string(), serde_yaml::Value::String(method.to_string()));
// Set outputs based on the selected hash 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()));
for output_name in &all_outputs {
if output_name == &format!("digest_{}", method) {
base.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);
base.outputs.insert(output_name.to_string(), serde_yaml::Value::Null);
}
}
@@ -129,14 +128,18 @@ impl DigestMetaPlugin {
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 let Some(current_value) = base.outputs.get_mut(&key)
&& !current_value.is_null() {
*current_value = value;
}
}
}
plugin
DigestMetaPlugin {
hasher,
is_finalized: false,
base,
}
}
}
@@ -164,21 +167,27 @@ impl MetaPlugin for DigestMetaPlugin {
};
}
let metadata = Vec::new();
let mut 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));
// Use process_metadata_outputs to handle output mapping
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
output_name,
serde_yaml::Value::String(hash_value),
self.base.outputs()
) {
metadata.push(meta_data);
}
// 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.base.outputs.insert(output_name.to_string(), serde_yaml::Value::Null);
}
}
}
@@ -214,11 +223,11 @@ impl MetaPlugin for DigestMetaPlugin {
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
self.base.outputs()
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
self.base.outputs_mut()
}
fn default_outputs(&self) -> Vec<String> {
@@ -229,13 +238,12 @@ impl MetaPlugin for DigestMetaPlugin {
]
}
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.options
self.base.options()
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
self.base.options_mut()
}
}
@@ -247,4 +255,4 @@ fn register_digest_plugin() {
register_meta_plugin(MetaPluginType::Digest, |options, outputs| {
Box::new(DigestMetaPlugin::new(options, outputs))
});
}
}