refactor: remove connection storage from plugin structs and pass as argument

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-18 15:52:27 -03:00
parent 2a16edcbe7
commit d96804bdfb
5 changed files with 51 additions and 76 deletions

View File

@@ -16,7 +16,6 @@ pub struct MetaPluginProgram {
process: Option<Child>,
writer: Option<Box<dyn Write>>,
item_id: Option<i64>,
conn: Option<*mut Connection>,
result: Option<String>,
}
@@ -48,7 +47,6 @@ impl MetaPluginProgram {
process: None,
writer: None,
item_id: None,
conn: None,
result: None,
}
}
@@ -63,12 +61,11 @@ impl MetaPlugin for MetaPluginProgram {
false
}
fn initialize(&mut self, conn: &rusqlite::Connection, item_id: i64) -> Result<()> {
fn initialize(&mut self, _conn: &rusqlite::Connection, item_id: i64) -> Result<()> {
debug!("META: Initializing program plugin: {:?}", self);
// Store database connection and item ID for later use
// Store item ID for later use
self.item_id = Some(item_id);
self.conn = Some(conn as *const _ as *mut Connection);
let program = self.program.clone();
let args = self.args.clone();
@@ -94,7 +91,7 @@ impl MetaPlugin for MetaPluginProgram {
Ok(())
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, conn: &Connection) -> Result<()> {
debug!("META: Finalizing program plugin");
if let Some(process) = self.process.take() {
// Close stdin to signal end of input
@@ -116,16 +113,14 @@ impl MetaPlugin for MetaPluginProgram {
debug!("META: Program output: {}", result);
self.result = Some(result);
// Save the result to database if we have connection and item_id
if let (Some(conn), Some(item_id)) = (self.conn, self.item_id) {
// Convert raw pointer back to reference (unsafe)
let conn_ref = unsafe { &*conn };
// Save the result to database if we have item_id
if let Some(item_id) = self.item_id {
let meta = crate::db::Meta {
id: item_id,
name: self.meta_name.clone(),
value: self.result.clone().unwrap(),
};
let _ = crate::db::store_meta(conn_ref, meta);
let _ = crate::db::store_meta(conn, meta);
}
}
} else {
@@ -139,7 +134,7 @@ impl MetaPlugin for MetaPluginProgram {
Ok(())
}
fn update(&mut self, data: &[u8]) {
fn update(&mut self, data: &[u8], _conn: &Connection) {
if let Some(ref mut writer) = self.writer {
if let Err(e) = writer.write_all(data) {
debug!("META: Failed to write to process stdin: {}", e);