refactor: reduce boilerplate by using default implementations and base struct

Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-26 17:18:23 -03:00
parent 5661b78919
commit 3cf9d38ae2
2 changed files with 103 additions and 78 deletions

View File

@@ -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<String, serde_yaml::Value>,
pub options: std::collections::HashMap<String, serde_yaml::Value>,
}
impl BaseMetaPlugin {
pub fn new() -> Self {
Self::default()
}
}
impl MetaPlugin for BaseMetaPlugin {
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&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<String, serde_yaml::Value>;
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value>;
// Access to outputs mapping with default implementation
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&std::collections::HashMap::new()
}
// Access to options mapping
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value>;
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value>;
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
panic!("outputs_mut() not implemented for this plugin")
}
// Access to options mapping with default implementation
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&std::collections::HashMap::new()
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
panic!("options_mut() not implemented for this plugin")
}
// Get the default output names this plugin can produce
fn default_outputs(&self) -> Vec<String> {

View File

@@ -12,8 +12,7 @@ pub struct BinaryMetaPlugin {
max_buffer_size: usize,
is_saved: bool,
item_id: Option<i64>,
outputs: std::collections::HashMap<String, serde_yaml::Value>,
options: std::collections::HashMap<String, serde_yaml::Value>,
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<String, serde_yaml::Value> {
&self.base.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.base.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["binary".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> {
&mut self.base.options
}
fn configure_options(&mut self, options: &std::collections::HashMap<String, serde_yaml::Value>) -> Result<()> {
@@ -146,30 +156,4 @@ impl MetaPlugin for BinaryMetaPlugin {
}
Ok(())
}
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
&self.outputs
}
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.outputs
}
fn default_outputs(&self) -> Vec<String> {
vec!["binary".to_string()]
}
fn default_options(&self) -> std::collections::HashMap<String, serde_yaml::Value> {
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<String, serde_yaml::Value> {
&self.options
}
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
&mut self.options
}
}