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:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user