diff --git a/src/filter_plugin/grep.rs b/src/filter_plugin/grep.rs index 2ee8bd4..8bdfda3 100644 --- a/src/filter_plugin/grep.rs +++ b/src/filter_plugin/grep.rs @@ -53,8 +53,8 @@ impl GrepFilter { /// /// # Arguments /// -/// * `reader` - A boxed mutable reference to the input reader providing the data stream. -/// * `writer` - A boxed mutable reference to the output writer where matching lines are sent. +/// * `reader` - Mutable reference to the input data stream. +/// * `writer` - Mutable reference to the output writer where matching lines are sent. /// /// # Returns /// @@ -67,33 +67,33 @@ impl GrepFilter { /// # Examples /// /// ``` -/// filter.filter(Box::new(&mut input), Box::new(&mut output))?; +/// filter.filter(&mut input, &mut output)?; /// ``` impl FilterPlugin for GrepFilter { - fn filter(&mut self, mut reader: Box<&mut dyn Read>, mut writer: Box<&mut dyn Write>) -> Result<()> { - let mut buf_reader = std::io::BufReader::new(&mut *reader); + fn filter(&mut self, reader: &mut dyn Read, writer: &mut dyn Write) -> Result<()> { + let mut buf_reader = std::io::BufReader::new(reader); for line in buf_reader.by_ref().lines() { let line = line?; if self.regex.is_match(&line) { - writeln!(&mut *writer, "{}", line)?; + writeln!(writer, "{}", line)?; } } Ok(()) } /// Clones this filter into a new boxed instance. -/// -/// Creates a new GrepFilter with the same regex pattern. -/// -/// # Returns -/// -/// A new `Box` representing a clone of this filter. -/// -/// # Examples -/// -/// ``` -/// let cloned = filter.clone_box(); -/// ``` + /// + /// Creates a new GrepFilter with the same regex pattern. + /// + /// # Returns + /// + /// A new `Box` representing a clone of this filter. + /// + /// # Examples + /// + /// ``` + /// let cloned = filter.clone_box(); + /// ``` fn clone_box(&self) -> Box { Box::new(Self { regex: self.regex.clone(), @@ -101,20 +101,20 @@ impl FilterPlugin for GrepFilter { } /// Returns the configuration options for this filter. -/// -/// The only option is the required "pattern" for the regex. -/// -/// # Returns -/// -/// A vector containing one `FilterOption` for "pattern" (required, no default). -/// -/// # Examples -/// -/// ``` -/// let opts = filter.options(); -/// assert_eq!(opts.len(), 1); -/// assert!(opts[0].required); -/// ``` + /// + /// The only option is the required "pattern" for the regex. + /// + /// # Returns + /// + /// A vector containing one `FilterOption` for "pattern" (required, no default). + /// + /// # Examples + /// + /// ``` + /// let opts = filter.options(); + /// assert_eq!(opts.len(), 1); + /// assert!(opts[0].required); + /// ``` fn options(&self) -> Vec { vec![ FilterOption { @@ -124,4 +124,4 @@ impl FilterPlugin for GrepFilter { } ] } -} +} \ No newline at end of file diff --git a/src/filter_plugin/head.rs b/src/filter_plugin/head.rs index 2b65956..68a61fe 100644 --- a/src/filter_plugin/head.rs +++ b/src/filter_plugin/head.rs @@ -3,7 +3,6 @@ use std::io::{Result, Read, Write, BufRead}; use crate::common::PIPESIZE; 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. /// /// Limits the output to the initial bytes specified in the configuration. @@ -55,8 +54,8 @@ impl HeadBytesFilter { /// /// # Arguments /// -/// * `reader` - Boxed mutable reference to the input data stream. -/// * `writer` - Boxed mutable reference to the output stream. +/// * `reader` - Mutable reference to the input data stream. +/// * `writer` - Mutable reference to the output stream. /// /// # Returns /// @@ -73,7 +72,7 @@ impl HeadBytesFilter { /// // Input "Hello World" becomes "Hello" /// ``` 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 { return Ok(()); } @@ -81,11 +80,11 @@ impl FilterPlugin for HeadBytesFilter { let mut buffer = vec![0; PIPESIZE]; while self.remaining > 0 { 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 { break; } - (&mut *writer).write_all(&buffer[..bytes_read])?; + writer.write_all(&buffer[..bytes_read])?; self.remaining -= bytes_read; } 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. -/// -/// 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 { remaining: usize, } @@ -173,8 +164,8 @@ impl HeadLinesFilter { /// /// # Arguments /// -/// * `reader` - Boxed mutable reference to the input data stream. -/// * `writer` - Boxed mutable reference to the output stream. +/// * `reader` - Mutable reference to the input data stream. +/// * `writer` - Mutable reference to the output stream. /// /// # Returns /// @@ -191,15 +182,15 @@ impl HeadLinesFilter { /// // Input "Line1\nLine2\nLine3" becomes "Line1\nLine2\n" /// ``` 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 { 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() { let line = line?; - writeln!(&mut *writer, "{}", line)?; + writeln!(writer, "{}", line)?; self.remaining -= 1; if self.remaining == 0 { break; diff --git a/src/filter_plugin/skip.rs b/src/filter_plugin/skip.rs index 980a49d..ac25f0c 100644 --- a/src/filter_plugin/skip.rs +++ b/src/filter_plugin/skip.rs @@ -19,36 +19,6 @@ impl SkipBytesFilter { 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) -> Result> { - 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 { @@ -56,19 +26,19 @@ impl FilterPlugin for SkipBytesFilter { /// /// # Arguments /// - /// * `reader` - A boxed mutable reference to the input reader providing the data stream. - /// * `writer` - A boxed mutable reference to the output writer where filtered data is sent. + /// * `reader` - Mutable reference to the input reader providing the data stream. + /// * `writer` - Mutable reference to the output writer where filtered data is sent. /// /// # Returns /// /// 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 if self.remaining > 0 { let mut buffer = vec![0; PIPESIZE]; while self.remaining > 0 { 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 { break; } @@ -77,7 +47,7 @@ impl FilterPlugin for SkipBytesFilter { } // Copy the remaining data using io::copy for efficiency - std::io::copy(&mut *reader, &mut *writer)?; + std::io::copy(reader, writer)?; Ok(()) } @@ -124,36 +94,6 @@ impl SkipLinesFilter { 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) -> Result> { - 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 { @@ -161,20 +101,20 @@ impl FilterPlugin for SkipLinesFilter { /// /// # Arguments /// - /// * `reader` - A boxed mutable reference to the input reader providing the data stream. - /// * `writer` - A boxed mutable reference to the output writer where filtered data is sent. + /// * `reader` - Mutable reference to the input reader providing the data stream. + /// * `writer` - Mutable reference to the output writer where filtered data is sent. /// /// # Returns /// /// 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<()> { - let mut buf_reader = std::io::BufReader::new(&mut *reader); + fn filter(&mut self, reader: &mut dyn Read, writer: &mut dyn Write) -> Result<()> { + let mut buf_reader = std::io::BufReader::new(reader); for line in buf_reader.by_ref().lines() { let line = line?; if self.remaining > 0 { self.remaining -= 1; } else { - writeln!(&mut *writer, "{}", line)?; + writeln!(writer, "{}", line)?; } } Ok(()) @@ -212,4 +152,4 @@ impl FilterPlugin for SkipLinesFilter { fn register_skip_filters() { register_filter_plugin("skip_bytes", || Box::new(SkipBytesFilter::new(0))); register_filter_plugin("skip_lines", || Box::new(SkipLinesFilter::new(0))); -} +} \ No newline at end of file diff --git a/src/filter_plugin/strip_ansi.rs b/src/filter_plugin/strip_ansi.rs index 2ec9679..265aaba 100644 --- a/src/filter_plugin/strip_ansi.rs +++ b/src/filter_plugin/strip_ansi.rs @@ -3,6 +3,11 @@ use strip_ansi_escapes::Writer; use super::{FilterPlugin, FilterOption}; /// A filter that removes ANSI escape sequences from the input. +/// +/// # Fields +/// +/// None, stateless filter. +#[derive(Default)] pub struct StripAnsiFilter; impl StripAnsiFilter { @@ -21,16 +26,17 @@ impl FilterPlugin for StripAnsiFilter { /// /// # Arguments /// - /// * `reader` - A boxed 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. + /// * `reader` - Mutable reference to the input reader providing the data stream with potential ANSI codes. + /// * `writer` - Mutable reference to the output writer where plain text is sent. /// /// # Returns /// /// 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<()> { - let mut ansi_writer = Writer::new(&mut *writer); - std::io::copy(&mut *reader, &mut ansi_writer)?; - ansi_writer.flush() + fn filter(&mut self, reader: &mut dyn Read, writer: &mut dyn Write) -> Result<()> { + let mut ansi_writer = Writer::new(writer); + std::io::copy(reader, &mut ansi_writer)?; + ansi_writer.flush()?; + Ok(()) } /// Clones this filter into a new boxed instance. diff --git a/src/filter_plugin/tail.rs b/src/filter_plugin/tail.rs index 6c77156..d99a669 100644 --- a/src/filter_plugin/tail.rs +++ b/src/filter_plugin/tail.rs @@ -22,36 +22,6 @@ impl TailBytesFilter { 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) -> Result> { - 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 { @@ -59,16 +29,16 @@ impl FilterPlugin for TailBytesFilter { /// /// # Arguments /// - /// * `reader` - A boxed mutable reference to the input reader providing the data stream. - /// * `writer` - A boxed mutable reference to the output writer where filtered data is sent. + /// * `reader` - Mutable reference to the input reader providing the data stream. + /// * `writer` - Mutable reference to the output writer where filtered data is sent. /// /// # Returns /// /// 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]; loop { - let bytes_read = (&mut *reader).read(&mut temp_buffer)?; + let bytes_read = reader.read(&mut temp_buffer)?; if bytes_read == 0 { break; } @@ -84,7 +54,7 @@ impl FilterPlugin for TailBytesFilter { // Write the buffered data at the end let result: Vec = self.buffer.iter().cloned().collect(); - (&mut *writer).write_all(&result)?; + writer.write_all(&result)?; Ok(()) } @@ -134,36 +104,6 @@ impl TailLinesFilter { 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) -> Result> { - 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 { @@ -171,14 +111,14 @@ impl FilterPlugin for TailLinesFilter { /// /// # Arguments /// - /// * `reader` - A boxed mutable reference to the input reader providing the data stream. - /// * `writer` - A boxed mutable reference to the output writer where filtered data is sent. + /// * `reader` - Mutable reference to the input reader providing the data stream. + /// * `writer` - Mutable reference to the output writer where filtered data is sent. /// /// # Returns /// /// 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<()> { - let mut buf_reader = std::io::BufReader::new(&mut *reader); + fn filter(&mut self, reader: &mut dyn Read, writer: &mut dyn Write) -> Result<()> { + let mut buf_reader = std::io::BufReader::new(reader); for line in buf_reader.by_ref().lines() { let line = line?; if self.lines.len() == self.count { @@ -189,7 +129,7 @@ impl FilterPlugin for TailLinesFilter { // Write the buffered lines for line in &self.lines { - writeln!(&mut *writer, "{}", line)?; + writeln!(writer, "{}", line)?; } Ok(()) } diff --git a/src/meta_plugin/digest.rs b/src/meta_plugin/digest.rs index a1c1af2..9264dbc 100644 --- a/src/meta_plugin/digest.rs +++ b/src/meta_plugin/digest.rs @@ -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, is_finalized: bool, @@ -66,17 +72,6 @@ pub struct DigestMetaPlugin { options: std::collections::HashMap, } -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)) }); -} +} \ No newline at end of file diff --git a/src/meta_plugin/env.rs b/src/meta_plugin/env.rs index 21e49d5..d6bbe68 100644 --- a/src/meta_plugin/env.rs +++ b/src/meta_plugin/env.rs @@ -205,7 +205,7 @@ impl MetaPlugin for EnvMetaPlugin { fn options(&self) -> &std::collections::HashMap { use once_cell::sync::Lazy; static EMPTY: Lazy> = - 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)) }); -} +} \ No newline at end of file diff --git a/src/meta_plugin/exec.rs b/src/meta_plugin/exec.rs index 6fc0cf3..ad81920 100644 --- a/src/meta_plugin/exec.rs +++ b/src/meta_plugin/exec.rs @@ -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)) }); -} +} \ No newline at end of file diff --git a/src/meta_plugin/hostname.rs b/src/meta_plugin/hostname.rs index ab5f1c7..d63c73d 100644 --- a/src/meta_plugin/hostname.rs +++ b/src/meta_plugin/hostname.rs @@ -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)) }); -} +} \ No newline at end of file diff --git a/src/meta_plugin/text.rs b/src/meta_plugin/text.rs index a602004..f01ef5a 100644 --- a/src/meta_plugin/text.rs +++ b/src/meta_plugin/text.rs @@ -385,28 +385,26 @@ impl TextMetaPlugin { /// /// * `Option` - Metadata entry if enabled and data exists. fn output_median_line_length_metadata(&self) -> Option { - 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)) }); -} +} \ No newline at end of file