From 3cf9d38ae22fc724b4c08cf5229cddf888ab5748 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Tue, 26 Aug 2025 17:18:23 -0300 Subject: [PATCH] refactor: reduce boilerplate by using default implementations and base struct Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) --- src/meta_plugin.rs | 55 ++++++++++++++--- src/meta_plugin/binary.rs | 126 +++++++++++++++++--------------------- 2 files changed, 103 insertions(+), 78 deletions(-) diff --git a/src/meta_plugin.rs b/src/meta_plugin.rs index e2ad643..3614034 100644 --- a/src/meta_plugin.rs +++ b/src/meta_plugin.rs @@ -29,6 +29,37 @@ pub struct MetaPluginResponse { pub is_finalized: bool, } +/// Base implementation for meta plugins to reduce boilerplate +#[derive(Debug, Clone, Default)] +pub struct BaseMetaPlugin { + pub outputs: std::collections::HashMap, + pub options: std::collections::HashMap, +} + +impl BaseMetaPlugin { + pub fn new() -> Self { + Self::default() + } +} + +impl MetaPlugin for BaseMetaPlugin { + fn outputs(&self) -> &std::collections::HashMap { + &self.outputs + } + + fn outputs_mut(&mut self) -> &mut std::collections::HashMap { + &mut self.outputs + } + + fn options(&self) -> &std::collections::HashMap { + &self.options + } + + fn options_mut(&mut self) -> &mut std::collections::HashMap { + &mut self.options + } +} + #[derive(Debug, Eq, PartialEq, Clone, strum::EnumIter, strum::Display, strum::EnumString)] #[strum(ascii_case_insensitive)] pub enum MetaPluginType { @@ -93,7 +124,7 @@ pub trait MetaPlugin { } // Update the meta plugin with new data - fn update(&mut self, data: &[u8]) -> MetaPluginResponse { + fn update(&mut self, _data: &[u8]) -> MetaPluginResponse { // Default implementation does nothing MetaPluginResponse { metadata: Vec::new(), @@ -125,13 +156,23 @@ pub trait MetaPlugin { } } - // Access to outputs mapping - fn outputs(&self) -> &std::collections::HashMap; - fn outputs_mut(&mut self) -> &mut std::collections::HashMap; + // Access to outputs mapping with default implementation + fn outputs(&self) -> &std::collections::HashMap { + &std::collections::HashMap::new() + } - // Access to options mapping - fn options(&self) -> &std::collections::HashMap; - fn options_mut(&mut self) -> &mut std::collections::HashMap; + fn outputs_mut(&mut self) -> &mut std::collections::HashMap { + panic!("outputs_mut() not implemented for this plugin") + } + + // Access to options mapping with default implementation + fn options(&self) -> &std::collections::HashMap { + &std::collections::HashMap::new() + } + + fn options_mut(&mut self) -> &mut std::collections::HashMap { + panic!("options_mut() not implemented for this plugin") + } // Get the default output names this plugin can produce fn default_outputs(&self) -> Vec { diff --git a/src/meta_plugin/binary.rs b/src/meta_plugin/binary.rs index b848015..1abb09f 100644 --- a/src/meta_plugin/binary.rs +++ b/src/meta_plugin/binary.rs @@ -12,8 +12,7 @@ pub struct BinaryMetaPlugin { max_buffer_size: usize, is_saved: bool, item_id: Option, - outputs: std::collections::HashMap, - options: std::collections::HashMap, + base: crate::meta_plugin::BaseMetaPlugin, } impl BinaryMetaPlugin { @@ -52,8 +51,10 @@ impl BinaryMetaPlugin { max_buffer_size, is_saved: false, item_id: None, - outputs: final_outputs, - options: final_options, + base: crate::meta_plugin::BaseMetaPlugin { + outputs: final_outputs, + options: final_options, + }, } } @@ -68,30 +69,6 @@ impl MetaPlugin for BinaryMetaPlugin { true } - fn finalize(&mut self) -> MetaPluginResponse { - let mut metadata = Vec::new(); - - // Save the binary detection result when finalizing, if not already saved - if let Some(_item_id) = self.item_id { - let is_binary_result = is_binary(&self.buffer); - let value = if is_binary_result { "true".to_string() } else { "false".to_string() }; - - // Use process_metadata_outputs to handle output mapping - if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( - "binary", - value, - &self.outputs - ) { - metadata.push(meta_data); - } - } - - MetaPluginResponse { - metadata, - is_finalized: true, - } - } - fn update(&mut self, data: &[u8]) -> MetaPluginResponse { // Calculate how much data we can still accept let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len()); @@ -106,18 +83,16 @@ impl MetaPlugin for BinaryMetaPlugin { // If we've reached our buffer limit, return metadata let mut metadata = Vec::new(); if self.buffer.len() >= self.max_buffer_size { - if let Some(_item_id) = self.item_id { - let is_binary_result = is_binary(&self.buffer); - let value = if is_binary_result { "true".to_string() } else { "false".to_string() }; + let is_binary_result = is_binary(&self.buffer); + let value = if is_binary_result { "true".to_string() } else { "false".to_string() }; - // Use process_metadata_outputs to handle output mapping - if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( - "binary", - value, - &self.outputs - ) { - metadata.push(meta_data); - } + // Use process_metadata_outputs to handle output mapping + if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( + "binary", + value, + &self.outputs + ) { + metadata.push(meta_data); } } @@ -127,15 +102,50 @@ impl MetaPlugin for BinaryMetaPlugin { } } + fn finalize(&mut self) -> MetaPluginResponse { + let mut metadata = Vec::new(); + + // Save the binary detection result when finalizing + let is_binary_result = is_binary(&self.buffer); + let value = if is_binary_result { "true".to_string() } else { "false".to_string() }; + + // Use process_metadata_outputs to handle output mapping + if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( + "binary", + value, + &self.outputs + ) { + metadata.push(meta_data); + } + + MetaPluginResponse { + metadata, + is_finalized: true, + } + } + fn meta_name(&self) -> String { self.meta_name.clone() } - - fn initialize(&mut self) -> MetaPluginResponse { - MetaPluginResponse { - metadata: Vec::new(), - is_finalized: false, - } + + fn outputs(&self) -> &std::collections::HashMap { + &self.base.outputs + } + + fn outputs_mut(&mut self) -> &mut std::collections::HashMap { + &mut self.base.outputs + } + + fn default_outputs(&self) -> Vec { + vec!["binary".to_string()] + } + + fn options(&self) -> &std::collections::HashMap { + &self.base.options + } + + fn options_mut(&mut self) -> &mut std::collections::HashMap { + &mut self.base.options } fn configure_options(&mut self, options: &std::collections::HashMap) -> Result<()> { @@ -146,30 +156,4 @@ impl MetaPlugin for BinaryMetaPlugin { } Ok(()) } - - fn outputs(&self) -> &std::collections::HashMap { - &self.outputs - } - - fn outputs_mut(&mut self) -> &mut std::collections::HashMap { - &mut self.outputs - } - - fn default_outputs(&self) -> Vec { - vec!["binary".to_string()] - } - - fn default_options(&self) -> std::collections::HashMap { - let mut options = std::collections::HashMap::new(); - options.insert("max_buffer_size".to_string(), serde_yaml::Value::Number(PIPESIZE.into())); - options - } - - fn options(&self) -> &std::collections::HashMap { - &self.options - } - - fn options_mut(&mut self) -> &mut std::collections::HashMap { - &mut self.options - } }