refactor: add update method to digest engines for consistent write interface

This commit is contained in:
Andrew Phillips (aider)
2025-05-12 20:33:13 -03:00
parent 4ee0715e39
commit f748fdd0f3
4 changed files with 28 additions and 2 deletions

View File

@@ -32,6 +32,9 @@ pub trait DigestEngine {
}
fn create(&self) -> Result<Box<dyn Write>>;
fn finalize(&mut self) -> io::Result<String>;
// Update the digest with new data
fn update(&mut self, data: &[u8]);
}
lazy_static! {

View File

@@ -19,4 +19,6 @@ impl DigestEngine for DigestEngineNone {
fn finalize(&mut self) -> io::Result<String> {
Ok("none".to_string())
}
fn update(&mut self, _data: &[u8]) {}
}

View File

@@ -16,6 +16,7 @@ pub struct DigestEngineProgram {
pub program: String,
pub args: Vec<String>,
pub supported: bool,
pub stdin: Option<tokio::io::BufWriter<std::process::ChildStdin>>,
}
impl DigestEngineProgram {
@@ -26,6 +27,7 @@ impl DigestEngineProgram {
program: program_path.unwrap_or(program.to_string()),
args: args.iter().map(|s| s.to_string()).collect(),
supported,
stdin: None,
}
}
}
@@ -34,6 +36,7 @@ impl DigestEngine for DigestEngineProgram {
fn is_supported(&self) -> bool {
self.supported
}
fn create(&self) -> Result<Box<dyn Write>> {
debug!("DIGEST: Writting using {:?}", *self);
@@ -45,7 +48,7 @@ impl DigestEngine for DigestEngineProgram {
program, args
);
let process = Command::new(program.clone())
let mut process = Command::new(program.clone())
.args(args.clone())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
@@ -56,12 +59,26 @@ impl DigestEngine for DigestEngineProgram {
args
))?;
Ok(Box::new(process.stdin.unwrap()))
let stdin = process.stdin.unwrap();
let stdin = tokio::io::BufWriter::new(stdin);
Ok(Box::new(Self {
program: program.clone(),
args: args.clone(),
stdin: Some(stdin),
supported: self.supported,
}))
}
fn finalize(&mut self) -> io::Result<String> {
Ok("program".to_string())
}
fn update(&mut self, data: &[u8]) {
if let Some(stdin) = &mut self.stdin {
let _ = stdin.write(data);
}
}
}
fn get_program_path(program: &str) -> Result<String> {

View File

@@ -31,4 +31,8 @@ impl DigestEngine for DigestEngineSha256 {
let result = self.hasher.clone().finalize();
Ok(format!("{:x}", result))
}
fn update(&mut self, data: &[u8]) {
self.hasher.update(data);
}
}