Ugh
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use sha2::{Digest, Sha256, Sha512};
|
||||
use crate::meta_plugin::{BaseMetaPlugin, MetaPlugin, MetaPluginType};
|
||||
use md5;
|
||||
use crate::meta_plugin::{MetaPlugin, MetaPluginType, BaseMetaPlugin};
|
||||
use sha2::{Digest, Sha256, Sha512};
|
||||
use std::io::Write;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -33,11 +33,11 @@ impl Hasher {
|
||||
Hasher::Sha256(hasher) => hasher.update(data),
|
||||
Hasher::Md5(hasher) => {
|
||||
let _ = hasher.write(data);
|
||||
},
|
||||
}
|
||||
Hasher::Sha512(hasher) => hasher.update(data),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn finalize(&mut self) -> String {
|
||||
match self {
|
||||
Hasher::Sha256(hasher) => {
|
||||
@@ -54,7 +54,7 @@ impl Hasher {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn output_name(&self) -> &'static str {
|
||||
match self {
|
||||
Hasher::Sha256(_) => "digest_sha256",
|
||||
@@ -71,21 +71,20 @@ pub struct DigestMetaPlugin {
|
||||
base: BaseMetaPlugin,
|
||||
}
|
||||
|
||||
|
||||
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 base = BaseMetaPlugin::new();
|
||||
|
||||
|
||||
// Apply provided options
|
||||
if let Some(opts) = options {
|
||||
for (key, value) in opts {
|
||||
base.options.insert(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Determine the selected method
|
||||
let method = if let Some(method_value) = base.options.get("method") {
|
||||
if let Some(method_str) = method_value.as_str() {
|
||||
@@ -101,7 +100,7 @@ impl DigestMetaPlugin {
|
||||
} else {
|
||||
"sha256"
|
||||
};
|
||||
|
||||
|
||||
// Initialize the hasher based on the method
|
||||
let hasher = match method {
|
||||
"md5" => Some(Hasher::Md5(md5::Context::new())),
|
||||
@@ -109,32 +108,40 @@ impl DigestMetaPlugin {
|
||||
"sha512" => Some(Hasher::Sha512(Sha512::new())),
|
||||
_ => Some(Hasher::Sha256(Sha256::new())),
|
||||
};
|
||||
|
||||
|
||||
// Add the method to options so it shows up in the status
|
||||
base.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) {
|
||||
base.outputs.insert(output_name.to_string(), serde_yaml::Value::String(output_name.to_string()));
|
||||
base.outputs.insert(
|
||||
output_name.to_string(),
|
||||
serde_yaml::Value::String(output_name.to_string()),
|
||||
);
|
||||
} else {
|
||||
base.outputs.insert(output_name.to_string(), serde_yaml::Value::Null);
|
||||
base.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) = base.outputs.get_mut(&key)
|
||||
&& !current_value.is_null() {
|
||||
&& !current_value.is_null()
|
||||
{
|
||||
*current_value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
DigestMetaPlugin {
|
||||
hasher,
|
||||
is_finalized: false,
|
||||
@@ -147,11 +154,11 @@ 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(),
|
||||
@@ -166,32 +173,34 @@ impl MetaPlugin for DigestMetaPlugin {
|
||||
is_finalized: true,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
|
||||
|
||||
// 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()
|
||||
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.base.outputs.insert(output_name.to_string(), serde_yaml::Value::Null);
|
||||
self.base
|
||||
.outputs
|
||||
.insert(output_name.to_string(), serde_yaml::Value::Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
self.is_finalized = true;
|
||||
crate::meta_plugin::MetaPluginResponse {
|
||||
metadata,
|
||||
@@ -206,12 +215,12 @@ impl MetaPlugin for DigestMetaPlugin {
|
||||
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,
|
||||
@@ -221,15 +230,15 @@ impl MetaPlugin for DigestMetaPlugin {
|
||||
fn meta_type(&self) -> MetaPluginType {
|
||||
MetaPluginType::Digest
|
||||
}
|
||||
|
||||
|
||||
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
||||
self.base.outputs()
|
||||
}
|
||||
|
||||
|
||||
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
|
||||
self.base.outputs_mut()
|
||||
}
|
||||
|
||||
|
||||
fn default_outputs(&self) -> Vec<String> {
|
||||
vec![
|
||||
"digest_md5".to_string(),
|
||||
@@ -237,11 +246,11 @@ impl MetaPlugin for DigestMetaPlugin {
|
||||
"digest_sha512".to_string(),
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
||||
self.base.options()
|
||||
}
|
||||
|
||||
|
||||
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
|
||||
self.base.options_mut()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user