refactor: simplify filter plugin signatures by removing boxed parameters
This commit is contained in:
committed by
Andrew Phillips (aider)
parent
9c354d5ef4
commit
059bde09e4
@@ -5,17 +5,23 @@ use std::io::Write;
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Hasher {
|
||||
Md5(md5::Context),
|
||||
Sha256(Sha256),
|
||||
Md5(md5::Context),
|
||||
Sha512(Sha512),
|
||||
}
|
||||
|
||||
impl Default for Hasher {
|
||||
fn default() -> Self {
|
||||
Hasher::Sha256(Sha256::default())
|
||||
}
|
||||
}
|
||||
|
||||
// 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::Md5(_) => write!(f, "Hasher::Md5"),
|
||||
Hasher::Sha512(_) => write!(f, "Hasher::Sha512"),
|
||||
}
|
||||
}
|
||||
@@ -24,24 +30,24 @@ impl std::fmt::Debug for Hasher {
|
||||
impl Hasher {
|
||||
fn update(&mut self, data: &[u8]) {
|
||||
match self {
|
||||
Hasher::Sha256(hasher) => hasher.update(data),
|
||||
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::Md5(hasher) => {
|
||||
let result = hasher.clone().compute();
|
||||
format!("{:x}", result)
|
||||
}
|
||||
Hasher::Sha512(hasher) => {
|
||||
let result = std::mem::replace(hasher, Sha512::new()).finalize_reset();
|
||||
format!("{:x}", result)
|
||||
@@ -51,14 +57,14 @@ impl Hasher {
|
||||
|
||||
fn output_name(&self) -> &'static str {
|
||||
match self {
|
||||
Hasher::Md5(_) => "digest_md5",
|
||||
Hasher::Sha256(_) => "digest_sha256",
|
||||
Hasher::Md5(_) => "digest_md5",
|
||||
Hasher::Sha512(_) => "digest_sha512",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Default)]
|
||||
pub struct DigestMetaPlugin {
|
||||
hasher: Option<Hasher>,
|
||||
is_finalized: bool,
|
||||
@@ -66,17 +72,6 @@ pub struct DigestMetaPlugin {
|
||||
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(
|
||||
@@ -119,7 +114,7 @@ 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()));
|
||||
|
||||
// Set outputs based on the selected method
|
||||
// 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 {
|
||||
@@ -134,10 +129,9 @@ 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 !current_value.is_null() {
|
||||
*current_value = value;
|
||||
}
|
||||
if let Some(current_value) = plugin.outputs.get_mut(&key)
|
||||
&& !current_value.is_null() {
|
||||
*current_value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -253,4 +247,4 @@ fn register_digest_plugin() {
|
||||
register_meta_plugin(MetaPluginType::Digest, |options, outputs| {
|
||||
Box::new(DigestMetaPlugin::new(options, outputs))
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -205,7 +205,7 @@ impl MetaPlugin for EnvMetaPlugin {
|
||||
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
||||
use once_cell::sync::Lazy;
|
||||
static EMPTY: Lazy<std::collections::HashMap<String, serde_yaml::Value>> =
|
||||
Lazy::new(|| std::collections::HashMap::new());
|
||||
Lazy::new(std::collections::HashMap::new);
|
||||
&EMPTY
|
||||
}
|
||||
|
||||
@@ -226,4 +226,4 @@ fn register_env_plugin() {
|
||||
register_meta_plugin(MetaPluginType::Env, |options, outputs| {
|
||||
Box::new(EnvMetaPlugin::new(options, outputs))
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -278,10 +278,9 @@ impl MetaPlugin for MetaPluginExec {
|
||||
///
|
||||
/// * `MetaPluginResponse` - Empty metadata, not finalized.
|
||||
fn update(&mut self, data: &[u8]) -> MetaPluginResponse {
|
||||
if let Some(ref mut writer) = self.writer {
|
||||
if let Err(e) = writer.write_all(data) {
|
||||
debug!("META: Failed to write to process stdin: {}", e);
|
||||
}
|
||||
if let Some(ref mut writer) = self.writer
|
||||
&& let Err(e) = writer.write_all(data) {
|
||||
debug!("META: Failed to write to process stdin: {}", e);
|
||||
}
|
||||
MetaPluginResponse {
|
||||
metadata: Vec::new(),
|
||||
@@ -377,25 +376,22 @@ fn register_exec_plugin() {
|
||||
let mut split_whitespace = true;
|
||||
|
||||
if let Some(opts) = &options {
|
||||
if let Some(command_value) = opts.get("command") {
|
||||
if let Some(command_str) = command_value.as_str() {
|
||||
let parts: Vec<&str> = command_str.split_whitespace().collect();
|
||||
if !parts.is_empty() {
|
||||
program_name = parts[0].to_string();
|
||||
args = parts[1..].iter().map(|s| s.to_string()).collect();
|
||||
}
|
||||
if let Some(command_value) = opts.get("command")
|
||||
&& let Some(command_str) = command_value.as_str() {
|
||||
let parts: Vec<&str> = command_str.split_whitespace().collect();
|
||||
if !parts.is_empty() {
|
||||
program_name = parts[0].to_string();
|
||||
args = parts[1..].iter().map(|s| s.to_string()).collect();
|
||||
}
|
||||
}
|
||||
// Handle other options if needed
|
||||
if let Some(split_value) = opts.get("split_whitespace") {
|
||||
if let Some(split_bool) = split_value.as_bool() {
|
||||
split_whitespace = split_bool;
|
||||
}
|
||||
if let Some(split_value) = opts.get("split_whitespace")
|
||||
&& let Some(split_bool) = split_value.as_bool() {
|
||||
split_whitespace = split_bool;
|
||||
}
|
||||
if let Some(name_value) = opts.get("name") {
|
||||
if let Some(name_str) = name_value.as_str() {
|
||||
meta_name = name_str.to_string();
|
||||
}
|
||||
if let Some(name_value) = opts.get("name")
|
||||
&& let Some(name_str) = name_value.as_str() {
|
||||
meta_name = name_str.to_string();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -406,4 +402,4 @@ fn register_exec_plugin() {
|
||||
options,
|
||||
outputs))
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -34,15 +34,14 @@ impl HostnameMetaPlugin {
|
||||
if let Some(opts) = options {
|
||||
for (key, value) in opts {
|
||||
// Convert string "true"/"false" to boolean for hostname option
|
||||
if key == "hostname" {
|
||||
if let serde_yaml::Value::String(s) = &value {
|
||||
if s == "false" {
|
||||
final_options.insert(key, serde_yaml::Value::Bool(false));
|
||||
continue;
|
||||
} else if s == "true" {
|
||||
final_options.insert(key, serde_yaml::Value::Bool(true));
|
||||
continue;
|
||||
}
|
||||
if key == "hostname"
|
||||
&& let serde_yaml::Value::String(s) = &value {
|
||||
if s == "false" {
|
||||
final_options.insert(key, serde_yaml::Value::Bool(false));
|
||||
continue;
|
||||
} else if s == "true" {
|
||||
final_options.insert(key, serde_yaml::Value::Bool(true));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
final_options.insert(key, value);
|
||||
@@ -152,12 +151,11 @@ impl HostnameMetaPlugin {
|
||||
if let Some(_first_addr) = addrs.first() {
|
||||
// For local addresses, we might not get a reverse lookup, so try to infer
|
||||
// from the system's domain name
|
||||
if let Ok(domain) = std::process::Command::new("domainname").output() {
|
||||
if domain.status.success() {
|
||||
let domain_str = String::from_utf8_lossy(&domain.stdout).trim().to_string();
|
||||
if !domain_str.is_empty() && domain_str != "(none)" {
|
||||
return format!("{}.{}", short_hostname, domain_str);
|
||||
}
|
||||
if let Ok(domain) = std::process::Command::new("domainname").output()
|
||||
&& domain.status.success() {
|
||||
let domain_str = String::from_utf8_lossy(&domain.stdout).trim().to_string();
|
||||
if !domain_str.is_empty() && domain_str != "(none)" {
|
||||
return format!("{}.{}", short_hostname, domain_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -168,12 +166,10 @@ impl HostnameMetaPlugin {
|
||||
if let Ok(full_hostname) = std::process::Command::new("hostname")
|
||||
.arg("-f")
|
||||
.output()
|
||||
{
|
||||
if full_hostname.status.success() {
|
||||
let full_hostname_str = String::from_utf8_lossy(&full_hostname.stdout).trim().to_string();
|
||||
if !full_hostname_str.is_empty() && full_hostname_str != short_hostname {
|
||||
return full_hostname_str;
|
||||
}
|
||||
&& full_hostname.status.success() {
|
||||
let full_hostname_str = String::from_utf8_lossy(&full_hostname.stdout).trim().to_string();
|
||||
if !full_hostname_str.is_empty() && full_hostname_str != short_hostname {
|
||||
return full_hostname_str;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,32 +261,29 @@ impl MetaPlugin for HostnameMetaPlugin {
|
||||
let mut metadata = Vec::new();
|
||||
|
||||
// Add enabled metadata to the response using process_metadata_outputs
|
||||
if hostname_enabled {
|
||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
if hostname_enabled
|
||||
&& let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
"hostname",
|
||||
serde_yaml::Value::String(hostname_value.clone()),
|
||||
&self.outputs
|
||||
) {
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
if hostname_full_enabled {
|
||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
if hostname_full_enabled
|
||||
&& let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
"hostname_full",
|
||||
serde_yaml::Value::String(full_hostname.clone()),
|
||||
&self.outputs
|
||||
) {
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
if hostname_short_enabled {
|
||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
if hostname_short_enabled
|
||||
&& let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
"hostname_short",
|
||||
serde_yaml::Value::String(short_hostname.clone()),
|
||||
&self.outputs
|
||||
) {
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
|
||||
// Update outputs based on enabled status
|
||||
@@ -364,4 +357,4 @@ fn register_hostname_plugin() {
|
||||
register_meta_plugin(MetaPluginType::Hostname, |options, outputs| {
|
||||
Box::new(HostnameMetaPlugin::new(options, outputs))
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -385,28 +385,26 @@ impl TextMetaPlugin {
|
||||
///
|
||||
/// * `Option<MetaData>` - Metadata entry if enabled and data exists.
|
||||
fn output_median_line_length_metadata(&self) -> Option<crate::meta_plugin::MetaData> {
|
||||
if self.output_line_median_len {
|
||||
if let Some(lengths) = &self.line_lengths {
|
||||
if !lengths.is_empty() {
|
||||
let mut sorted_lengths = lengths.clone();
|
||||
sorted_lengths.sort();
|
||||
let median_len = if lengths.len() % 2 == 0 {
|
||||
(sorted_lengths[lengths.len() / 2 - 1] + sorted_lengths[lengths.len() / 2]) as f64 / 2.0
|
||||
} else {
|
||||
sorted_lengths[lengths.len() / 2] as f64
|
||||
};
|
||||
|
||||
return crate::meta_plugin::process_metadata_outputs(
|
||||
"text_line_median_len",
|
||||
serde_yaml::Value::String(median_len.to_string()),
|
||||
self.base.outputs()
|
||||
);
|
||||
}
|
||||
if self.output_line_median_len
|
||||
&& let Some(lengths) = &self.line_lengths {
|
||||
if !lengths.is_empty() {
|
||||
let mut sorted_lengths = lengths.clone();
|
||||
sorted_lengths.sort();
|
||||
let median_len = if lengths.len() % 2 == 0 {
|
||||
(sorted_lengths[lengths.len() / 2 - 1] + sorted_lengths[lengths.len() / 2]) as f64 / 2.0
|
||||
} else {
|
||||
sorted_lengths[lengths.len() / 2] as f64
|
||||
};
|
||||
|
||||
return crate::meta_plugin::process_metadata_outputs(
|
||||
"text_line_median_len",
|
||||
serde_yaml::Value::String(median_len.to_string()),
|
||||
self.base.outputs()
|
||||
);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
/// Helper method to output word and line counts.
|
||||
///
|
||||
@@ -476,7 +474,6 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Updates the plugin with new data chunk.
|
||||
///
|
||||
/// Accumulates data for binary detection (if pending) or text statistics.
|
||||
@@ -756,4 +753,4 @@ fn register_text_plugin() {
|
||||
register_meta_plugin(MetaPluginType::Text, |options, outputs| {
|
||||
Box::new(TextMetaPlugin::new(options, outputs))
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user