refactor: add update method to digest engines for consistent write interface
This commit is contained in:
@@ -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]) {}
|
||||
}
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user