refactor: simplify filter plugin signatures by removing boxed parameters

This commit is contained in:
Andrew Phillips
2025-09-12 10:36:09 -03:00
committed by Andrew Phillips (aider)
parent 9c354d5ef4
commit 059bde09e4
10 changed files with 158 additions and 301 deletions

View File

@@ -53,8 +53,8 @@ impl GrepFilter {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `reader` - A boxed mutable reference to the input reader providing the data stream. /// * `reader` - Mutable reference to the input data stream.
/// * `writer` - A boxed mutable reference to the output writer where matching lines are sent. /// * `writer` - Mutable reference to the output writer where matching lines are sent.
/// ///
/// # Returns /// # Returns
/// ///
@@ -67,33 +67,33 @@ impl GrepFilter {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// filter.filter(Box::new(&mut input), Box::new(&mut output))?; /// filter.filter(&mut input, &mut output)?;
/// ``` /// ```
impl FilterPlugin for GrepFilter { impl FilterPlugin for GrepFilter {
fn filter(&mut self, mut reader: Box<&mut dyn Read>, mut writer: Box<&mut dyn Write>) -> Result<()> { fn filter(&mut self, reader: &mut dyn Read, writer: &mut dyn Write) -> Result<()> {
let mut buf_reader = std::io::BufReader::new(&mut *reader); let mut buf_reader = std::io::BufReader::new(reader);
for line in buf_reader.by_ref().lines() { for line in buf_reader.by_ref().lines() {
let line = line?; let line = line?;
if self.regex.is_match(&line) { if self.regex.is_match(&line) {
writeln!(&mut *writer, "{}", line)?; writeln!(writer, "{}", line)?;
} }
} }
Ok(()) Ok(())
} }
/// Clones this filter into a new boxed instance. /// Clones this filter into a new boxed instance.
/// ///
/// Creates a new GrepFilter with the same regex pattern. /// Creates a new GrepFilter with the same regex pattern.
/// ///
/// # Returns /// # Returns
/// ///
/// A new `Box<dyn FilterPlugin>` representing a clone of this filter. /// A new `Box<dyn FilterPlugin>` representing a clone of this filter.
/// ///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// let cloned = filter.clone_box(); /// let cloned = filter.clone_box();
/// ``` /// ```
fn clone_box(&self) -> Box<dyn FilterPlugin> { fn clone_box(&self) -> Box<dyn FilterPlugin> {
Box::new(Self { Box::new(Self {
regex: self.regex.clone(), regex: self.regex.clone(),
@@ -101,20 +101,20 @@ impl FilterPlugin for GrepFilter {
} }
/// Returns the configuration options for this filter. /// Returns the configuration options for this filter.
/// ///
/// The only option is the required "pattern" for the regex. /// The only option is the required "pattern" for the regex.
/// ///
/// # Returns /// # Returns
/// ///
/// A vector containing one `FilterOption` for "pattern" (required, no default). /// A vector containing one `FilterOption` for "pattern" (required, no default).
/// ///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// let opts = filter.options(); /// let opts = filter.options();
/// assert_eq!(opts.len(), 1); /// assert_eq!(opts.len(), 1);
/// assert!(opts[0].required); /// assert!(opts[0].required);
/// ``` /// ```
fn options(&self) -> Vec<FilterOption> { fn options(&self) -> Vec<FilterOption> {
vec![ vec![
FilterOption { FilterOption {

View File

@@ -3,7 +3,6 @@ use std::io::{Result, Read, Write, BufRead};
use crate::common::PIPESIZE; use crate::common::PIPESIZE;
use crate::services::filter_service::register_filter_plugin; use crate::services::filter_service::register_filter_plugin;
/// A filter that reads the first N bytes from the input stream.
/// A filter that reads the first N bytes from the input stream. /// A filter that reads the first N bytes from the input stream.
/// ///
/// Limits the output to the initial bytes specified in the configuration. /// Limits the output to the initial bytes specified in the configuration.
@@ -55,8 +54,8 @@ impl HeadBytesFilter {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `reader` - Boxed mutable reference to the input data stream. /// * `reader` - Mutable reference to the input data stream.
/// * `writer` - Boxed mutable reference to the output stream. /// * `writer` - Mutable reference to the output stream.
/// ///
/// # Returns /// # Returns
/// ///
@@ -73,7 +72,7 @@ impl HeadBytesFilter {
/// // Input "Hello World" becomes "Hello" /// // Input "Hello World" becomes "Hello"
/// ``` /// ```
impl FilterPlugin for HeadBytesFilter { impl FilterPlugin for HeadBytesFilter {
fn filter(&mut self, mut reader: Box<&mut dyn Read>, mut writer: Box<&mut dyn Write>) -> Result<()> { fn filter(&mut self, reader: &mut dyn Read, writer: &mut dyn Write) -> Result<()> {
if self.remaining == 0 { if self.remaining == 0 {
return Ok(()); return Ok(());
} }
@@ -81,11 +80,11 @@ impl FilterPlugin for HeadBytesFilter {
let mut buffer = vec![0; PIPESIZE]; let mut buffer = vec![0; PIPESIZE];
while self.remaining > 0 { while self.remaining > 0 {
let to_read = std::cmp::min(self.remaining, PIPESIZE); let to_read = std::cmp::min(self.remaining, PIPESIZE);
let bytes_read = (&mut *reader).read(&mut buffer[..to_read])?; let bytes_read = reader.read(&mut buffer[..to_read])?;
if bytes_read == 0 { if bytes_read == 0 {
break; break;
} }
(&mut *writer).write_all(&buffer[..bytes_read])?; writer.write_all(&buffer[..bytes_read])?;
self.remaining -= bytes_read; self.remaining -= bytes_read;
} }
Ok(()) Ok(())
@@ -123,14 +122,6 @@ impl FilterPlugin for HeadBytesFilter {
} }
/// A filter that reads the first N lines from the input stream. /// A filter that reads the first N lines from the input stream.
/// A filter that reads the first N lines from the input stream.
///
/// Limits output to the initial lines specified, writing each full line to output.
/// Handles line endings properly using buffered reading.
///
/// # Fields
///
/// * `remaining` - Number of lines left to read before stopping.
pub struct HeadLinesFilter { pub struct HeadLinesFilter {
remaining: usize, remaining: usize,
} }
@@ -173,8 +164,8 @@ impl HeadLinesFilter {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `reader` - Boxed mutable reference to the input data stream. /// * `reader` - Mutable reference to the input data stream.
/// * `writer` - Boxed mutable reference to the output stream. /// * `writer` - Mutable reference to the output stream.
/// ///
/// # Returns /// # Returns
/// ///
@@ -191,15 +182,15 @@ impl HeadLinesFilter {
/// // Input "Line1\nLine2\nLine3" becomes "Line1\nLine2\n" /// // Input "Line1\nLine2\nLine3" becomes "Line1\nLine2\n"
/// ``` /// ```
impl FilterPlugin for HeadLinesFilter { impl FilterPlugin for HeadLinesFilter {
fn filter(&mut self, mut reader: Box<&mut dyn Read>, mut writer: Box<&mut dyn Write>) -> Result<()> { fn filter(&mut self, reader: &mut dyn Read, writer: &mut dyn Write) -> Result<()> {
if self.remaining == 0 { if self.remaining == 0 {
return Ok(()); return Ok(());
} }
let mut buf_reader = std::io::BufReader::new(&mut *reader); let mut buf_reader = std::io::BufReader::new(reader);
for line in buf_reader.by_ref().lines() { for line in buf_reader.by_ref().lines() {
let line = line?; let line = line?;
writeln!(&mut *writer, "{}", line)?; writeln!(writer, "{}", line)?;
self.remaining -= 1; self.remaining -= 1;
if self.remaining == 0 { if self.remaining == 0 {
break; break;

View File

@@ -19,36 +19,6 @@ impl SkipBytesFilter {
remaining: count, remaining: count,
} }
} }
/// Creates a new instance from options.
///
/// # Arguments
///
/// * `options` - An optional JSON value containing configuration options for the filter.
///
/// # Returns
///
/// A `Result` containing a boxed `FilterPlugin` on success, or an `io::Error` if options are invalid.
pub fn create(options: Option<serde_json::Value>) -> Result<Box<dyn FilterPlugin>> {
let options = options.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"skip_bytes filter requires options"
)
})?;
let count = options.get("n")
.and_then(|v| v.as_u64())
.map(|n| n as usize)
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"skip_bytes filter requires 'n' parameter"
)
})?;
Ok(Box::new(Self::new(count)))
}
} }
impl FilterPlugin for SkipBytesFilter { impl FilterPlugin for SkipBytesFilter {
@@ -56,19 +26,19 @@ impl FilterPlugin for SkipBytesFilter {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `reader` - A boxed mutable reference to the input reader providing the data stream. /// * `reader` - Mutable reference to the input reader providing the data stream.
/// * `writer` - A boxed mutable reference to the output writer where filtered data is sent. /// * `writer` - Mutable reference to the output writer where filtered data is sent.
/// ///
/// # Returns /// # Returns
/// ///
/// Returns `Ok(())` on success, or an `io::Error` if reading or writing fails. /// Returns `Ok(())` on success, or an `io::Error` if reading or writing fails.
fn filter(&mut self, mut reader: Box<&mut dyn Read>, mut writer: Box<&mut dyn Write>) -> Result<()> { fn filter(&mut self, reader: &mut dyn Read, writer: &mut dyn Write) -> Result<()> {
// Skip bytes in chunks // Skip bytes in chunks
if self.remaining > 0 { if self.remaining > 0 {
let mut buffer = vec![0; PIPESIZE]; let mut buffer = vec![0; PIPESIZE];
while self.remaining > 0 { while self.remaining > 0 {
let to_read = std::cmp::min(self.remaining, PIPESIZE); let to_read = std::cmp::min(self.remaining, PIPESIZE);
let bytes_read = (&mut *reader).read(&mut buffer[..to_read])?; let bytes_read = reader.read(&mut buffer[..to_read])?;
if bytes_read == 0 { if bytes_read == 0 {
break; break;
} }
@@ -77,7 +47,7 @@ impl FilterPlugin for SkipBytesFilter {
} }
// Copy the remaining data using io::copy for efficiency // Copy the remaining data using io::copy for efficiency
std::io::copy(&mut *reader, &mut *writer)?; std::io::copy(reader, writer)?;
Ok(()) Ok(())
} }
@@ -124,36 +94,6 @@ impl SkipLinesFilter {
remaining: count, remaining: count,
} }
} }
/// Creates a new instance from options.
///
/// # Arguments
///
/// * `options` - An optional JSON value containing configuration options for the filter.
///
/// # Returns
///
/// A `Result` containing a boxed `FilterPlugin` on success, or an `io::Error` if options are invalid.
pub fn create(options: Option<serde_json::Value>) -> Result<Box<dyn FilterPlugin>> {
let options = options.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"skip_lines filter requires options"
)
})?;
let count = options.get("n")
.and_then(|v| v.as_u64())
.map(|n| n as usize)
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"skip_lines filter requires 'n' parameter"
)
})?;
Ok(Box::new(Self::new(count)))
}
} }
impl FilterPlugin for SkipLinesFilter { impl FilterPlugin for SkipLinesFilter {
@@ -161,20 +101,20 @@ impl FilterPlugin for SkipLinesFilter {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `reader` - A boxed mutable reference to the input reader providing the data stream. /// * `reader` - Mutable reference to the input reader providing the data stream.
/// * `writer` - A boxed mutable reference to the output writer where filtered data is sent. /// * `writer` - Mutable reference to the output writer where filtered data is sent.
/// ///
/// # Returns /// # Returns
/// ///
/// Returns `Ok(())` on success, or an `io::Error` if reading or writing fails. /// Returns `Ok(())` on success, or an `io::Error` if reading or writing fails.
fn filter(&mut self, mut reader: Box<&mut dyn Read>, mut writer: Box<&mut dyn Write>) -> Result<()> { fn filter(&mut self, reader: &mut dyn Read, writer: &mut dyn Write) -> Result<()> {
let mut buf_reader = std::io::BufReader::new(&mut *reader); let mut buf_reader = std::io::BufReader::new(reader);
for line in buf_reader.by_ref().lines() { for line in buf_reader.by_ref().lines() {
let line = line?; let line = line?;
if self.remaining > 0 { if self.remaining > 0 {
self.remaining -= 1; self.remaining -= 1;
} else { } else {
writeln!(&mut *writer, "{}", line)?; writeln!(writer, "{}", line)?;
} }
} }
Ok(()) Ok(())

View File

@@ -3,6 +3,11 @@ use strip_ansi_escapes::Writer;
use super::{FilterPlugin, FilterOption}; use super::{FilterPlugin, FilterOption};
/// A filter that removes ANSI escape sequences from the input. /// A filter that removes ANSI escape sequences from the input.
///
/// # Fields
///
/// None, stateless filter.
#[derive(Default)]
pub struct StripAnsiFilter; pub struct StripAnsiFilter;
impl StripAnsiFilter { impl StripAnsiFilter {
@@ -21,16 +26,17 @@ impl FilterPlugin for StripAnsiFilter {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `reader` - A boxed mutable reference to the input reader providing the data stream with potential ANSI codes. /// * `reader` - Mutable reference to the input reader providing the data stream with potential ANSI codes.
/// * `writer` - A boxed mutable reference to the output writer where plain text is sent. /// * `writer` - Mutable reference to the output writer where plain text is sent.
/// ///
/// # Returns /// # Returns
/// ///
/// Returns `Ok(())` on success, or an `io::Error` if reading or writing fails. /// Returns `Ok(())` on success, or an `io::Error` if reading or writing fails.
fn filter(&mut self, mut reader: Box<&mut dyn Read>, mut writer: Box<&mut dyn Write>) -> Result<()> { fn filter(&mut self, reader: &mut dyn Read, writer: &mut dyn Write) -> Result<()> {
let mut ansi_writer = Writer::new(&mut *writer); let mut ansi_writer = Writer::new(writer);
std::io::copy(&mut *reader, &mut ansi_writer)?; std::io::copy(reader, &mut ansi_writer)?;
ansi_writer.flush() ansi_writer.flush()?;
Ok(())
} }
/// Clones this filter into a new boxed instance. /// Clones this filter into a new boxed instance.

View File

@@ -22,36 +22,6 @@ impl TailBytesFilter {
count, count,
} }
} }
/// Creates a new instance from options.
///
/// # Arguments
///
/// * `options` - An optional JSON value containing configuration options for the filter.
///
/// # Returns
///
/// A `Result` containing a boxed `FilterPlugin` on success, or an `io::Error` if options are invalid.
pub fn create(options: Option<serde_json::Value>) -> Result<Box<dyn FilterPlugin>> {
let options = options.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"tail_bytes filter requires options"
)
})?;
let count = options.get("n")
.and_then(|v| v.as_u64())
.map(|n| n as usize)
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"tail_bytes filter requires 'n' parameter"
)
})?;
Ok(Box::new(Self::new(count)))
}
} }
impl FilterPlugin for TailBytesFilter { impl FilterPlugin for TailBytesFilter {
@@ -59,16 +29,16 @@ impl FilterPlugin for TailBytesFilter {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `reader` - A boxed mutable reference to the input reader providing the data stream. /// * `reader` - Mutable reference to the input reader providing the data stream.
/// * `writer` - A boxed mutable reference to the output writer where filtered data is sent. /// * `writer` - Mutable reference to the output writer where filtered data is sent.
/// ///
/// # Returns /// # Returns
/// ///
/// Returns `Ok(())` on success, or an `io::Error` if reading or writing fails. /// Returns `Ok(())` on success, or an `io::Error` if reading or writing fails.
fn filter(&mut self, mut reader: Box<&mut dyn Read>, mut writer: Box<&mut dyn Write>) -> Result<()> { fn filter(&mut self, reader: &mut dyn Read, writer: &mut dyn Write) -> Result<()> {
let mut temp_buffer = vec![0; PIPESIZE]; let mut temp_buffer = vec![0; PIPESIZE];
loop { loop {
let bytes_read = (&mut *reader).read(&mut temp_buffer)?; let bytes_read = reader.read(&mut temp_buffer)?;
if bytes_read == 0 { if bytes_read == 0 {
break; break;
} }
@@ -84,7 +54,7 @@ impl FilterPlugin for TailBytesFilter {
// Write the buffered data at the end // Write the buffered data at the end
let result: Vec<u8> = self.buffer.iter().cloned().collect(); let result: Vec<u8> = self.buffer.iter().cloned().collect();
(&mut *writer).write_all(&result)?; writer.write_all(&result)?;
Ok(()) Ok(())
} }
@@ -134,36 +104,6 @@ impl TailLinesFilter {
count, count,
} }
} }
/// Creates a new instance from options.
///
/// # Arguments
///
/// * `options` - An optional JSON value containing configuration options for the filter.
///
/// # Returns
///
/// A `Result` containing a boxed `FilterPlugin` on success, or an `io::Error` if options are invalid.
pub fn create(options: Option<serde_json::Value>) -> Result<Box<dyn FilterPlugin>> {
let options = options.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"tail_lines filter requires options"
)
})?;
let count = options.get("n")
.and_then(|v| v.as_u64())
.map(|n| n as usize)
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"tail_lines filter requires 'n' parameter"
)
})?;
Ok(Box::new(Self::new(count)))
}
} }
impl FilterPlugin for TailLinesFilter { impl FilterPlugin for TailLinesFilter {
@@ -171,14 +111,14 @@ impl FilterPlugin for TailLinesFilter {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `reader` - A boxed mutable reference to the input reader providing the data stream. /// * `reader` - Mutable reference to the input reader providing the data stream.
/// * `writer` - A boxed mutable reference to the output writer where filtered data is sent. /// * `writer` - Mutable reference to the output writer where filtered data is sent.
/// ///
/// # Returns /// # Returns
/// ///
/// Returns `Ok(())` on success, or an `io::Error` if reading or writing fails. /// Returns `Ok(())` on success, or an `io::Error` if reading or writing fails.
fn filter(&mut self, mut reader: Box<&mut dyn Read>, mut writer: Box<&mut dyn Write>) -> Result<()> { fn filter(&mut self, reader: &mut dyn Read, writer: &mut dyn Write) -> Result<()> {
let mut buf_reader = std::io::BufReader::new(&mut *reader); let mut buf_reader = std::io::BufReader::new(reader);
for line in buf_reader.by_ref().lines() { for line in buf_reader.by_ref().lines() {
let line = line?; let line = line?;
if self.lines.len() == self.count { if self.lines.len() == self.count {
@@ -189,7 +129,7 @@ impl FilterPlugin for TailLinesFilter {
// Write the buffered lines // Write the buffered lines
for line in &self.lines { for line in &self.lines {
writeln!(&mut *writer, "{}", line)?; writeln!(writer, "{}", line)?;
} }
Ok(()) Ok(())
} }

View File

@@ -5,17 +5,23 @@ use std::io::Write;
#[derive(Clone)] #[derive(Clone)]
enum Hasher { enum Hasher {
Md5(md5::Context),
Sha256(Sha256), Sha256(Sha256),
Md5(md5::Context),
Sha512(Sha512), Sha512(Sha512),
} }
impl Default for Hasher {
fn default() -> Self {
Hasher::Sha256(Sha256::default())
}
}
// Manual Debug implementation to avoid md5::Context not implementing Debug // Manual Debug implementation to avoid md5::Context not implementing Debug
impl std::fmt::Debug for Hasher { impl std::fmt::Debug for Hasher {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Hasher::Md5(_) => write!(f, "Hasher::Md5"),
Hasher::Sha256(_) => write!(f, "Hasher::Sha256"), Hasher::Sha256(_) => write!(f, "Hasher::Sha256"),
Hasher::Md5(_) => write!(f, "Hasher::Md5"),
Hasher::Sha512(_) => write!(f, "Hasher::Sha512"), Hasher::Sha512(_) => write!(f, "Hasher::Sha512"),
} }
} }
@@ -24,24 +30,24 @@ impl std::fmt::Debug for Hasher {
impl Hasher { impl Hasher {
fn update(&mut self, data: &[u8]) { fn update(&mut self, data: &[u8]) {
match self { match self {
Hasher::Sha256(hasher) => hasher.update(data),
Hasher::Md5(hasher) => { Hasher::Md5(hasher) => {
let _ = hasher.write(data); let _ = hasher.write(data);
}, },
Hasher::Sha256(hasher) => hasher.update(data),
Hasher::Sha512(hasher) => hasher.update(data), Hasher::Sha512(hasher) => hasher.update(data),
} }
} }
fn finalize(&mut self) -> String { fn finalize(&mut self) -> String {
match self { match self {
Hasher::Md5(hasher) => {
let result = hasher.clone().compute();
format!("{:x}", result)
}
Hasher::Sha256(hasher) => { Hasher::Sha256(hasher) => {
let result = std::mem::replace(hasher, Sha256::new()).finalize_reset(); let result = std::mem::replace(hasher, Sha256::new()).finalize_reset();
format!("{:x}", result) format!("{:x}", result)
} }
Hasher::Md5(hasher) => {
let result = hasher.clone().compute();
format!("{:x}", result)
}
Hasher::Sha512(hasher) => { Hasher::Sha512(hasher) => {
let result = std::mem::replace(hasher, Sha512::new()).finalize_reset(); let result = std::mem::replace(hasher, Sha512::new()).finalize_reset();
format!("{:x}", result) format!("{:x}", result)
@@ -51,14 +57,14 @@ impl Hasher {
fn output_name(&self) -> &'static str { fn output_name(&self) -> &'static str {
match self { match self {
Hasher::Md5(_) => "digest_md5",
Hasher::Sha256(_) => "digest_sha256", Hasher::Sha256(_) => "digest_sha256",
Hasher::Md5(_) => "digest_md5",
Hasher::Sha512(_) => "digest_sha512", Hasher::Sha512(_) => "digest_sha512",
} }
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Default)]
pub struct DigestMetaPlugin { pub struct DigestMetaPlugin {
hasher: Option<Hasher>, hasher: Option<Hasher>,
is_finalized: bool, is_finalized: bool,
@@ -66,17 +72,6 @@ pub struct DigestMetaPlugin {
options: 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 { impl DigestMetaPlugin {
pub fn new( pub fn new(
@@ -119,7 +114,7 @@ impl DigestMetaPlugin {
// Add the method to options so it shows up in the status // 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())); 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 // Only the selected method's output should be enabled, others should be None
let all_outputs = vec!["digest_md5", "digest_sha256", "digest_sha512"]; let all_outputs = vec!["digest_md5", "digest_sha256", "digest_sha512"];
for output_name in all_outputs { for output_name in all_outputs {
@@ -134,13 +129,12 @@ impl DigestMetaPlugin {
if let Some(outs) = outputs { if let Some(outs) = outputs {
for (key, value) in outs { for (key, value) in outs {
// Only update if the output is not disabled (not None) // 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) = plugin.outputs.get_mut(&key)
if !current_value.is_null() { && !current_value.is_null() {
*current_value = value; *current_value = value;
} }
} }
} }
}
plugin plugin
} }

View File

@@ -205,7 +205,7 @@ impl MetaPlugin for EnvMetaPlugin {
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> { fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
static EMPTY: Lazy<std::collections::HashMap<String, serde_yaml::Value>> = static EMPTY: Lazy<std::collections::HashMap<String, serde_yaml::Value>> =
Lazy::new(|| std::collections::HashMap::new()); Lazy::new(std::collections::HashMap::new);
&EMPTY &EMPTY
} }

View File

@@ -278,11 +278,10 @@ impl MetaPlugin for MetaPluginExec {
/// ///
/// * `MetaPluginResponse` - Empty metadata, not finalized. /// * `MetaPluginResponse` - Empty metadata, not finalized.
fn update(&mut self, data: &[u8]) -> MetaPluginResponse { fn update(&mut self, data: &[u8]) -> MetaPluginResponse {
if let Some(ref mut writer) = self.writer { if let Some(ref mut writer) = self.writer
if let Err(e) = writer.write_all(data) { && let Err(e) = writer.write_all(data) {
debug!("META: Failed to write to process stdin: {}", e); debug!("META: Failed to write to process stdin: {}", e);
} }
}
MetaPluginResponse { MetaPluginResponse {
metadata: Vec::new(), metadata: Vec::new(),
is_finalized: false, is_finalized: false,
@@ -377,27 +376,24 @@ fn register_exec_plugin() {
let mut split_whitespace = true; let mut split_whitespace = true;
if let Some(opts) = &options { if let Some(opts) = &options {
if let Some(command_value) = opts.get("command") { if let Some(command_value) = opts.get("command")
if let Some(command_str) = command_value.as_str() { && let Some(command_str) = command_value.as_str() {
let parts: Vec<&str> = command_str.split_whitespace().collect(); let parts: Vec<&str> = command_str.split_whitespace().collect();
if !parts.is_empty() { if !parts.is_empty() {
program_name = parts[0].to_string(); program_name = parts[0].to_string();
args = parts[1..].iter().map(|s| s.to_string()).collect(); args = parts[1..].iter().map(|s| s.to_string()).collect();
} }
} }
}
// Handle other options if needed // Handle other options if needed
if let Some(split_value) = opts.get("split_whitespace") { if let Some(split_value) = opts.get("split_whitespace")
if let Some(split_bool) = split_value.as_bool() { && let Some(split_bool) = split_value.as_bool() {
split_whitespace = split_bool; split_whitespace = split_bool;
} }
} if let Some(name_value) = opts.get("name")
if let Some(name_value) = opts.get("name") { && let Some(name_str) = name_value.as_str() {
if let Some(name_str) = name_value.as_str() {
meta_name = name_str.to_string(); meta_name = name_str.to_string();
} }
} }
}
Box::new(MetaPluginExec::new(&program_name, Box::new(MetaPluginExec::new(&program_name,
args.iter().map(|s| s.as_str()).collect(), args.iter().map(|s| s.as_str()).collect(),

View File

@@ -34,8 +34,8 @@ impl HostnameMetaPlugin {
if let Some(opts) = options { if let Some(opts) = options {
for (key, value) in opts { for (key, value) in opts {
// Convert string "true"/"false" to boolean for hostname option // Convert string "true"/"false" to boolean for hostname option
if key == "hostname" { if key == "hostname"
if let serde_yaml::Value::String(s) = &value { && let serde_yaml::Value::String(s) = &value {
if s == "false" { if s == "false" {
final_options.insert(key, serde_yaml::Value::Bool(false)); final_options.insert(key, serde_yaml::Value::Bool(false));
continue; continue;
@@ -44,7 +44,6 @@ impl HostnameMetaPlugin {
continue; continue;
} }
} }
}
final_options.insert(key, value); final_options.insert(key, value);
} }
} }
@@ -152,8 +151,8 @@ impl HostnameMetaPlugin {
if let Some(_first_addr) = addrs.first() { if let Some(_first_addr) = addrs.first() {
// For local addresses, we might not get a reverse lookup, so try to infer // For local addresses, we might not get a reverse lookup, so try to infer
// from the system's domain name // from the system's domain name
if let Ok(domain) = std::process::Command::new("domainname").output() { if let Ok(domain) = std::process::Command::new("domainname").output()
if domain.status.success() { && domain.status.success() {
let domain_str = String::from_utf8_lossy(&domain.stdout).trim().to_string(); let domain_str = String::from_utf8_lossy(&domain.stdout).trim().to_string();
if !domain_str.is_empty() && domain_str != "(none)" { if !domain_str.is_empty() && domain_str != "(none)" {
return format!("{}.{}", short_hostname, domain_str); return format!("{}.{}", short_hostname, domain_str);
@@ -161,21 +160,18 @@ impl HostnameMetaPlugin {
} }
} }
} }
}
// Fallback: try to get the FQDN using the system's hostname resolution // Fallback: try to get the FQDN using the system's hostname resolution
// This should give us the full hostname if configured // This should give us the full hostname if configured
if let Ok(full_hostname) = std::process::Command::new("hostname") if let Ok(full_hostname) = std::process::Command::new("hostname")
.arg("-f") .arg("-f")
.output() .output()
{ && full_hostname.status.success() {
if full_hostname.status.success() {
let full_hostname_str = String::from_utf8_lossy(&full_hostname.stdout).trim().to_string(); 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 { if !full_hostname_str.is_empty() && full_hostname_str != short_hostname {
return full_hostname_str; return full_hostname_str;
} }
} }
}
// Final fallback: return the short hostname // Final fallback: return the short hostname
short_hostname short_hostname
@@ -265,33 +261,30 @@ impl MetaPlugin for HostnameMetaPlugin {
let mut metadata = Vec::new(); let mut metadata = Vec::new();
// Add enabled metadata to the response using process_metadata_outputs // Add enabled metadata to the response using process_metadata_outputs
if hostname_enabled { if hostname_enabled
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( && let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
"hostname", "hostname",
serde_yaml::Value::String(hostname_value.clone()), serde_yaml::Value::String(hostname_value.clone()),
&self.outputs &self.outputs
) { ) {
metadata.push(meta_data); metadata.push(meta_data);
} }
} if hostname_full_enabled
if hostname_full_enabled { && let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
"hostname_full", "hostname_full",
serde_yaml::Value::String(full_hostname.clone()), serde_yaml::Value::String(full_hostname.clone()),
&self.outputs &self.outputs
) { ) {
metadata.push(meta_data); metadata.push(meta_data);
} }
} if hostname_short_enabled
if hostname_short_enabled { && let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
"hostname_short", "hostname_short",
serde_yaml::Value::String(short_hostname.clone()), serde_yaml::Value::String(short_hostname.clone()),
&self.outputs &self.outputs
) { ) {
metadata.push(meta_data); metadata.push(meta_data);
} }
}
// Update outputs based on enabled status // Update outputs based on enabled status
// Handle hostname output // Handle hostname output

View File

@@ -385,8 +385,8 @@ impl TextMetaPlugin {
/// ///
/// * `Option<MetaData>` - Metadata entry if enabled and data exists. /// * `Option<MetaData>` - Metadata entry if enabled and data exists.
fn output_median_line_length_metadata(&self) -> Option<crate::meta_plugin::MetaData> { fn output_median_line_length_metadata(&self) -> Option<crate::meta_plugin::MetaData> {
if self.output_line_median_len { if self.output_line_median_len
if let Some(lengths) = &self.line_lengths { && let Some(lengths) = &self.line_lengths {
if !lengths.is_empty() { if !lengths.is_empty() {
let mut sorted_lengths = lengths.clone(); let mut sorted_lengths = lengths.clone();
sorted_lengths.sort(); sorted_lengths.sort();
@@ -403,11 +403,9 @@ impl TextMetaPlugin {
); );
} }
} }
}
None None
} }
/// Helper method to output word and line counts. /// Helper method to output word and line counts.
/// ///
/// Finalizes pending data and collects all enabled text statistics metadata. /// Finalizes pending data and collects all enabled text statistics metadata.
@@ -476,7 +474,6 @@ impl MetaPlugin for TextMetaPlugin {
} }
/// Updates the plugin with new data chunk. /// Updates the plugin with new data chunk.
/// ///
/// Accumulates data for binary detection (if pending) or text statistics. /// Accumulates data for binary detection (if pending) or text statistics.