refactor: reimplement digest engines as meta plugins

Co-authored-by: aider (openai/andrew.openrouter.qwen.qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-07-28 17:12:13 -03:00
parent 0bf898c0e7
commit d666cfce00
3 changed files with 82 additions and 9 deletions

View File

@@ -0,0 +1,60 @@
use anyhow::Result;
use sha2::{Digest, Sha256};
use std::io;
use std::io::Write;
use crate::meta_plugin::MetaPlugin;
#[derive(Debug, Clone, Default)]
pub struct DigestSha256MetaPlugin {
hasher: Sha256,
meta_name: String,
}
impl DigestSha256MetaPlugin {
pub fn new() -> DigestSha256MetaPlugin {
DigestSha256MetaPlugin {
hasher: Sha256::new(),
meta_name: "digest_sha256".to_string(),
}
}
}
impl MetaPlugin for DigestSha256MetaPlugin {
fn create(&self) -> Result<Box<dyn Write>> {
// For meta plugins, we don't actually create a writer since we're buffering data internally
// This method is required by the trait but not used in the same way as digest engines
Ok(Box::new(DummyWriter))
}
fn finalize(&mut self) -> io::Result<String> {
let result = self.hasher.clone().finalize();
Ok(format!("{:x}", result))
}
fn update(&mut self, data: &[u8]) {
self.hasher.update(data);
}
fn meta_name(&mut self) -> String {
self.meta_name.clone()
}
fn is_default(&self) -> bool {
true
}
}
// Dummy writer that implements Write but doesn't do anything
// This is needed to satisfy the MetaPlugin trait requirements
struct DummyWriter;
impl Write for DummyWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}