fix: implement Write trait for digest engines

This commit is contained in:
Andrew Phillips (aider)
2025-05-12 20:36:15 -03:00
parent f748fdd0f3
commit c7533890c8
4 changed files with 71 additions and 13 deletions

View File

@@ -37,6 +37,25 @@ pub trait DigestEngine {
fn update(&mut self, data: &[u8]); fn update(&mut self, data: &[u8]);
} }
// Dummy writer that implements Write for the none digest engine
struct DummyWriter;
impl DummyWriter {
fn new() -> Self {
DummyWriter
}
}
impl Write for DummyWriter {
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
Ok(0)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
lazy_static! { lazy_static! {
pub static ref DIGEST_PROGRAMS: EnumMap<DigestType, Option<DigestEngineProgram>> = enum_map! { pub static ref DIGEST_PROGRAMS: EnumMap<DigestType, Option<DigestEngineProgram>> = enum_map! {
DigestType::Sha256 => None, DigestType::Sha256 => None,

View File

@@ -14,11 +14,31 @@ impl DigestEngineNone {
impl DigestEngine for DigestEngineNone { impl DigestEngine for DigestEngineNone {
fn create(&self) -> Result<Box<dyn Write>> { fn create(&self) -> Result<Box<dyn Write>> {
Ok(Box::new(Self::new())) Ok(Box::new(DummyWriter::new()))
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
Ok("none".to_string()) Ok("none".to_string())
} }
fn update(&mut self, _data: &[u8]) {} fn update(&mut self, _data: &[u8]) {}
} }
// Dummy writer that implements Write for the none digest engine
struct DummyWriter;
impl DummyWriter {
fn new() -> Self {
DummyWriter
}
}
impl Write for DummyWriter {
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
Ok(0)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

View File

@@ -59,14 +59,9 @@ impl DigestEngine for DigestEngineProgram {
args args
))?; ))?;
let stdin = process.stdin.unwrap(); Ok(Box::new(ProgramWriter {
let stdin = tokio::io::BufWriter::new(stdin); stdin: process.stdin.take().unwrap(),
process: Some(process),
Ok(Box::new(Self {
program: program.clone(),
args: args.clone(),
stdin: Some(stdin),
supported: self.supported,
})) }))
} }
@@ -75,9 +70,7 @@ impl DigestEngine for DigestEngineProgram {
} }
fn update(&mut self, data: &[u8]) { fn update(&mut self, data: &[u8]) {
if let Some(stdin) = &mut self.stdin { // This is handled by the ProgramWriter implementation
let _ = stdin.write(data);
}
} }
} }

View File

@@ -20,11 +20,37 @@ impl DigestEngineSha256 {
} }
} }
// Create a writer that updates the hasher
fn create_writer(&self) -> Sha256Writer {
Sha256Writer::new(self.hasher.clone())
}
}
// Wrapper that implements Write for the Sha256 hasher
struct Sha256Writer {
hasher: Sha256,
}
impl Sha256Writer {
fn new(hasher: Sha256) -> Self {
Sha256Writer { hasher }
}
}
impl Write for Sha256Writer {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.hasher.update(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
} }
impl DigestEngine for DigestEngineSha256 { impl DigestEngine for DigestEngineSha256 {
fn create(&self) -> Result<Box<dyn Write>> { fn create(&self) -> Result<Box<dyn Write>> {
Box::new(Self::new()) Ok(Box::new(self.create_writer()))
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {