feat: update finalize to return Result<()> and simplify save mode

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-18 08:12:49 -03:00
parent f11070dc60
commit 22206de5ab
5 changed files with 43 additions and 105 deletions

View File

@@ -25,9 +25,8 @@ impl MetaPlugin for DigestSha256MetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
let result = self.hasher.clone().finalize();
Ok(format!("{:x}", result))
fn finalize(&mut self) -> Result<()> {
Ok(())
}
fn update(&mut self, data: &[u8]) {
@@ -60,13 +59,8 @@ impl MetaPlugin for ReadTimeMetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
if let Some(start_time) = self.start_time {
let duration = start_time.elapsed();
Ok(format!("{:.6}s", duration.as_secs_f64()))
} else {
Ok("0.000000s".to_string())
}
fn finalize(&mut self) -> Result<()> {
Ok(())
}
fn update(&mut self, _data: &[u8]) {
@@ -102,18 +96,8 @@ impl MetaPlugin for ReadRateMetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
if let Some(start_time) = self.start_time {
let duration = start_time.elapsed();
if duration.as_secs_f64() > 0.0 {
let rate = self.bytes_read as f64 / duration.as_secs_f64();
Ok(format!("{:.0} B/s", rate))
} else {
Ok("0 B/s".to_string())
}
} else {
Ok("0 B/s".to_string())
}
fn finalize(&mut self) -> Result<()> {
Ok(())
}
fn update(&mut self, data: &[u8]) {

View File

@@ -114,15 +114,14 @@ impl MetaPlugin for MagicFileMetaPlugin {
Ok(())
}
fn finalize(&mut self) -> io::Result<String> {
fn finalize(&mut self) -> Result<()> {
// Save all magic metadata if not already saved
if !self.is_saved {
if let Err(e) = self.save_all_magic_metadata() {
eprintln!("Warning: Failed to save magic metadata: {}", e);
}
}
// Return empty string since we save during finalize or update
Ok("".to_string())
Ok(())
}
fn update(&mut self, data: &[u8]) {

View File

@@ -84,44 +84,9 @@ impl MetaPlugin for MetaPluginProgram {
Ok(())
}
fn finalize(&mut self) -> io::Result<String> {
fn finalize(&mut self) -> Result<()> {
debug!("META: Finalizing program plugin");
// Close stdin to signal EOF to the process
self.writer.take();
if let Some(process) = self.process.take() {
let output = process.wait_with_output()
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to wait for process: {}", e)))?;
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let trimmed_result = stdout.trim();
// For certain programs, we only want the first part before whitespace
if self.split_whitespace {
let parts: Vec<&str> = trimmed_result.split_whitespace().collect();
if !parts.is_empty() {
Ok(parts[0].to_string())
} else {
Ok(trimmed_result.to_string())
}
} else {
Ok(trimmed_result.to_string())
}
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
Err(io::Error::new(
io::ErrorKind::Other,
format!("Command failed: {}", stderr.trim()),
))
}
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"No process to finalize".to_string(),
))
}
Ok(())
}
fn update(&mut self, data: &[u8]) {

View File

@@ -46,9 +46,9 @@ impl MetaPlugin for BinaryMetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
// Since we save during initialize() or update(), return empty to avoid duplicate saves
Ok("".to_string())
fn finalize(&mut self) -> Result<()> {
// Since we save during initialize() or update(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, data: &[u8]) {
@@ -107,9 +107,9 @@ impl MetaPlugin for CwdMetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
// Since we save during initialize(), return empty to avoid duplicate saves
Ok("".to_string())
fn finalize(&mut self) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
@@ -151,9 +151,9 @@ impl MetaPlugin for UidMetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
// Since we save during initialize(), return empty to avoid duplicate saves
Ok("".to_string())
fn finalize(&mut self) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
@@ -192,9 +192,9 @@ impl MetaPlugin for UserMetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
// Since we save during initialize(), return empty to avoid duplicate saves
Ok("".to_string())
fn finalize(&mut self) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
@@ -236,9 +236,9 @@ impl MetaPlugin for GidMetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
// Since we save during initialize(), return empty to avoid duplicate saves
Ok("".to_string())
fn finalize(&mut self) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
@@ -277,9 +277,9 @@ impl MetaPlugin for GroupMetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
// Since we save during initialize(), return empty to avoid duplicate saves
Ok("".to_string())
fn finalize(&mut self) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
@@ -321,9 +321,9 @@ impl MetaPlugin for ShellMetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
// Since we save during initialize(), return empty to avoid duplicate saves
Ok("".to_string())
fn finalize(&mut self) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
@@ -365,9 +365,9 @@ impl MetaPlugin for ShellPidMetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
// Since we save during initialize(), return empty to avoid duplicate saves
Ok("".to_string())
fn finalize(&mut self) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
@@ -409,9 +409,9 @@ impl MetaPlugin for KeepPidMetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
// Since we save during initialize(), return empty to avoid duplicate saves
Ok("".to_string())
fn finalize(&mut self) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
@@ -450,9 +450,9 @@ impl MetaPlugin for HostnameMetaPlugin {
true
}
fn finalize(&mut self) -> io::Result<String> {
// Since we save during initialize(), return empty to avoid duplicate saves
Ok("".to_string())
fn finalize(&mut self) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {

View File

@@ -209,20 +209,10 @@ fn process_input_stream(
}
debug!("MAIN: Ending IO loop after {:?} bytes", item.size);
// Finalize meta plugins and save their metadata
// Finalize meta plugins
for meta_plugin in meta_plugins.iter_mut() {
match meta_plugin.finalize() {
Ok(value) => {
// Only save non-empty values (empty means already saved during initialize/update)
if !value.is_empty() {
if let Err(e) = meta_plugin.save_meta(conn, item_id, value) {
eprintln!("Warning: Failed to save meta value: {}", e);
}
}
}
Err(e) => {
eprintln!("Warning: Failed to finalize meta plugin: {}", e);
}
if let Err(e) = meta_plugin.finalize() {
eprintln!("Warning: Failed to finalize meta plugin: {}", e);
}
}