feat: add default implementations for initialize, update, finalize in MetaPlugin
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use anyhow::Result;
|
||||
use rusqlite::Connection;
|
||||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod program;
|
||||
pub mod digest;
|
||||
@@ -14,6 +14,19 @@ use crate::meta_plugin::system::{CwdMetaPlugin, UidMetaPlugin, UserMetaPlugin, G
|
||||
use crate::meta_plugin::magic::MagicFileMetaPlugin;
|
||||
use crate::meta_plugin::binary::BinaryMetaPlugin;
|
||||
|
||||
/// Represents metadata to be stored
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MetaData {
|
||||
pub name: String,
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
/// Response from meta plugin operations
|
||||
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
||||
pub struct MetaPluginResponse {
|
||||
pub metadata: Vec<MetaData>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone, strum::EnumIter, strum::Display, strum::EnumString)]
|
||||
#[strum(ascii_case_insensitive)]
|
||||
pub enum MetaPluginType {
|
||||
@@ -42,36 +55,30 @@ pub enum MetaPluginType {
|
||||
|
||||
/// Central function to handle metadata output with name mapping
|
||||
/// outputs: HashMap where key is internal name, value is either custom name or "false" to disable
|
||||
pub fn output_metadata(conn: &Connection, item_id: i64, internal_name: &str, value: String, outputs: &std::collections::HashMap<String, serde_yaml::Value>) -> Result<()> {
|
||||
pub fn process_metadata_outputs(internal_name: &str, value: String, outputs: &std::collections::HashMap<String, serde_yaml::Value>) -> Option<MetaData> {
|
||||
// Check if this output is disabled
|
||||
if let Some(mapping) = outputs.get(internal_name) {
|
||||
if let Some(false_val) = mapping.as_bool() {
|
||||
if !false_val {
|
||||
debug!("META: Skipping disabled output: {}", internal_name);
|
||||
return Ok(());
|
||||
return None;
|
||||
}
|
||||
}
|
||||
if let Some(custom_name) = mapping.as_str() {
|
||||
debug!("META: Saving metadata: item_id={}, internal_name={}, custom_name={}, value={}", item_id, internal_name, custom_name, value);
|
||||
let meta = crate::db::Meta {
|
||||
id: item_id,
|
||||
debug!("META: Processing metadata: internal_name={}, custom_name={}, value={}", internal_name, custom_name, value);
|
||||
return Some(MetaData {
|
||||
name: custom_name.to_string(),
|
||||
value,
|
||||
};
|
||||
crate::db::store_meta(conn, meta)?;
|
||||
return Ok(());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Default: use internal name as output name
|
||||
debug!("META: Saving metadata: item_id={}, name={}, value={}", item_id, internal_name, value);
|
||||
let meta = crate::db::Meta {
|
||||
id: item_id,
|
||||
debug!("META: Processing metadata: name={}, value={}", internal_name, value);
|
||||
Some(MetaData {
|
||||
name: internal_name.to_string(),
|
||||
value,
|
||||
};
|
||||
crate::db::store_meta(conn, meta)?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
pub trait MetaPlugin {
|
||||
@@ -83,10 +90,16 @@ pub trait MetaPlugin {
|
||||
false
|
||||
}
|
||||
|
||||
fn finalize(&mut self, conn: &Connection) -> Result<()>;
|
||||
|
||||
// Update the meta plugin with new data
|
||||
fn update(&mut self, data: &[u8], conn: &Connection);
|
||||
fn update(&mut self, data: &[u8]) -> MetaPluginResponse {
|
||||
// Default implementation does nothing
|
||||
MetaPluginResponse::default()
|
||||
}
|
||||
|
||||
fn finalize(&mut self) -> MetaPluginResponse {
|
||||
// Default implementation does nothing
|
||||
MetaPluginResponse::default()
|
||||
}
|
||||
|
||||
fn meta_name(&self) -> String;
|
||||
|
||||
@@ -95,9 +108,10 @@ pub trait MetaPlugin {
|
||||
None
|
||||
}
|
||||
|
||||
// Initialize with database connection
|
||||
fn initialize(&mut self, _conn: &Connection, _item_id: i64) -> Result<()> {
|
||||
Ok(())
|
||||
// Initialize the plugin
|
||||
fn initialize(&mut self) -> MetaPluginResponse {
|
||||
// Default implementation does nothing
|
||||
MetaPluginResponse::default()
|
||||
}
|
||||
|
||||
// Access to outputs mapping
|
||||
@@ -120,11 +134,6 @@ pub trait MetaPlugin {
|
||||
std::collections::HashMap::new()
|
||||
}
|
||||
|
||||
// Save metadata to database using central output handler
|
||||
fn save_meta(&mut self, conn: &Connection, item_id: i64, internal_name: &str, value: String) -> Result<()> {
|
||||
output_metadata(conn, item_id, internal_name, value, self.outputs())
|
||||
}
|
||||
|
||||
// Configure plugin with options (excluding outputs)
|
||||
fn configure_options(&mut self, _options: &std::collections::HashMap<String, serde_yaml::Value>) -> Result<()> {
|
||||
// Default implementation does nothing - plugins can override this
|
||||
|
||||
@@ -112,9 +112,8 @@ impl MetaService {
|
||||
}
|
||||
|
||||
for meta_plugin in plugins.iter_mut() {
|
||||
if let Err(e) = meta_plugin.initialize(conn, item_id) {
|
||||
log::warn!("META_SERVICE: Failed to initialize meta plugin: {}", e);
|
||||
}
|
||||
let response = meta_plugin.initialize();
|
||||
self.process_plugin_response(conn, item_id, meta_plugin, response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,14 +124,40 @@ impl MetaService {
|
||||
conn: &Connection,
|
||||
) {
|
||||
for meta_plugin in plugins.iter_mut() {
|
||||
meta_plugin.update(chunk, conn);
|
||||
let response = meta_plugin.update(chunk);
|
||||
self.process_plugin_response(conn, item_id, meta_plugin, response);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finalize_plugins(&self, plugins: &mut [Box<dyn MetaPlugin>], conn: &Connection) {
|
||||
for meta_plugin in plugins.iter_mut() {
|
||||
if let Err(e) = meta_plugin.finalize(conn) {
|
||||
log::warn!("META_SERVICE: Failed to finalize meta plugin: {}", e);
|
||||
let response = meta_plugin.finalize();
|
||||
self.process_plugin_response(conn, item_id, meta_plugin, response);
|
||||
}
|
||||
}
|
||||
|
||||
fn process_plugin_response(
|
||||
&self,
|
||||
conn: &Connection,
|
||||
item_id: i64,
|
||||
plugin: &mut Box<dyn MetaPlugin>,
|
||||
response: crate::meta_plugin::MetaPluginResponse,
|
||||
) {
|
||||
for meta_data in response.metadata {
|
||||
if let Some(processed_meta) = crate::meta_plugin::process_metadata_outputs(
|
||||
&meta_data.name,
|
||||
meta_data.value,
|
||||
plugin.outputs()
|
||||
) {
|
||||
// Save to database
|
||||
let db_meta = crate::db::Meta {
|
||||
id: item_id,
|
||||
name: processed_meta.name,
|
||||
value: processed_meta.value,
|
||||
};
|
||||
if let Err(e) = crate::db::store_meta(conn, db_meta) {
|
||||
log::warn!("META_SERVICE: Failed to store metadata: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user