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:
@@ -29,6 +29,37 @@ pub struct MetaPluginResponse {
|
|||||||
pub is_finalized: bool,
|
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)]
|
#[derive(Debug, Eq, PartialEq, Clone, strum::EnumIter, strum::Display, strum::EnumString)]
|
||||||
#[strum(ascii_case_insensitive)]
|
#[strum(ascii_case_insensitive)]
|
||||||
pub enum MetaPluginType {
|
pub enum MetaPluginType {
|
||||||
@@ -93,7 +124,7 @@ pub trait MetaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update the meta plugin with new data
|
// 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
|
// Default implementation does nothing
|
||||||
MetaPluginResponse {
|
MetaPluginResponse {
|
||||||
metadata: Vec::new(),
|
metadata: Vec::new(),
|
||||||
@@ -125,13 +156,23 @@ pub trait MetaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Access to outputs mapping
|
// Access to outputs mapping with default implementation
|
||||||
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value>;
|
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
||||||
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value>;
|
&std::collections::HashMap::new()
|
||||||
|
}
|
||||||
|
|
||||||
// Access to options mapping
|
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
|
||||||
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value>;
|
panic!("outputs_mut() not implemented for this plugin")
|
||||||
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value>;
|
}
|
||||||
|
|
||||||
|
// 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
|
// Get the default output names this plugin can produce
|
||||||
fn default_outputs(&self) -> Vec<String> {
|
fn default_outputs(&self) -> Vec<String> {
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ pub struct BinaryMetaPlugin {
|
|||||||
max_buffer_size: usize,
|
max_buffer_size: usize,
|
||||||
is_saved: bool,
|
is_saved: bool,
|
||||||
item_id: Option<i64>,
|
item_id: Option<i64>,
|
||||||
outputs: std::collections::HashMap<String, serde_yaml::Value>,
|
base: crate::meta_plugin::BaseMetaPlugin,
|
||||||
options: std::collections::HashMap<String, serde_yaml::Value>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BinaryMetaPlugin {
|
impl BinaryMetaPlugin {
|
||||||
@@ -52,8 +51,10 @@ impl BinaryMetaPlugin {
|
|||||||
max_buffer_size,
|
max_buffer_size,
|
||||||
is_saved: false,
|
is_saved: false,
|
||||||
item_id: None,
|
item_id: None,
|
||||||
|
base: crate::meta_plugin::BaseMetaPlugin {
|
||||||
outputs: final_outputs,
|
outputs: final_outputs,
|
||||||
options: final_options,
|
options: final_options,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,30 +69,6 @@ impl MetaPlugin for BinaryMetaPlugin {
|
|||||||
true
|
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 {
|
fn update(&mut self, data: &[u8]) -> MetaPluginResponse {
|
||||||
// Calculate how much data we can still accept
|
// Calculate how much data we can still accept
|
||||||
let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len());
|
let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len());
|
||||||
@@ -106,7 +83,6 @@ impl MetaPlugin for BinaryMetaPlugin {
|
|||||||
// If we've reached our buffer limit, return metadata
|
// If we've reached our buffer limit, return metadata
|
||||||
let mut metadata = Vec::new();
|
let mut metadata = Vec::new();
|
||||||
if self.buffer.len() >= self.max_buffer_size {
|
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 is_binary_result = is_binary(&self.buffer);
|
||||||
let value = if is_binary_result { "true".to_string() } else { "false".to_string() };
|
let value = if is_binary_result { "true".to_string() } else { "false".to_string() };
|
||||||
|
|
||||||
@@ -119,7 +95,6 @@ impl MetaPlugin for BinaryMetaPlugin {
|
|||||||
metadata.push(meta_data);
|
metadata.push(meta_data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
MetaPluginResponse {
|
MetaPluginResponse {
|
||||||
metadata,
|
metadata,
|
||||||
@@ -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 {
|
fn meta_name(&self) -> String {
|
||||||
self.meta_name.clone()
|
self.meta_name.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn initialize(&mut self) -> MetaPluginResponse {
|
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
||||||
MetaPluginResponse {
|
&self.base.outputs
|
||||||
metadata: Vec::new(),
|
|
||||||
is_finalized: false,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<()> {
|
fn configure_options(&mut self, options: &std::collections::HashMap<String, serde_yaml::Value>) -> Result<()> {
|
||||||
@@ -146,30 +156,4 @@ impl MetaPlugin for BinaryMetaPlugin {
|
|||||||
}
|
}
|
||||||
Ok(())
|
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user