refactor: add update method to digest engines for consistent write interface
This commit is contained in:
@@ -32,6 +32,9 @@ pub trait DigestEngine {
|
|||||||
}
|
}
|
||||||
fn create(&self) -> Result<Box<dyn Write>>;
|
fn create(&self) -> Result<Box<dyn Write>>;
|
||||||
fn finalize(&mut self) -> io::Result<String>;
|
fn finalize(&mut self) -> io::Result<String>;
|
||||||
|
|
||||||
|
// Update the digest with new data
|
||||||
|
fn update(&mut self, data: &[u8]);
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
|||||||
@@ -19,4 +19,6 @@ impl DigestEngine for DigestEngineNone {
|
|||||||
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]) {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ pub struct DigestEngineProgram {
|
|||||||
pub program: String,
|
pub program: String,
|
||||||
pub args: Vec<String>,
|
pub args: Vec<String>,
|
||||||
pub supported: bool,
|
pub supported: bool,
|
||||||
|
pub stdin: Option<tokio::io::BufWriter<std::process::ChildStdin>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DigestEngineProgram {
|
impl DigestEngineProgram {
|
||||||
@@ -26,6 +27,7 @@ impl DigestEngineProgram {
|
|||||||
program: program_path.unwrap_or(program.to_string()),
|
program: program_path.unwrap_or(program.to_string()),
|
||||||
args: args.iter().map(|s| s.to_string()).collect(),
|
args: args.iter().map(|s| s.to_string()).collect(),
|
||||||
supported,
|
supported,
|
||||||
|
stdin: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,6 +36,7 @@ impl DigestEngine for DigestEngineProgram {
|
|||||||
fn is_supported(&self) -> bool {
|
fn is_supported(&self) -> bool {
|
||||||
self.supported
|
self.supported
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create(&self) -> Result<Box<dyn Write>> {
|
fn create(&self) -> Result<Box<dyn Write>> {
|
||||||
debug!("DIGEST: Writting using {:?}", *self);
|
debug!("DIGEST: Writting using {:?}", *self);
|
||||||
|
|
||||||
@@ -45,7 +48,7 @@ impl DigestEngine for DigestEngineProgram {
|
|||||||
program, args
|
program, args
|
||||||
);
|
);
|
||||||
|
|
||||||
let process = Command::new(program.clone())
|
let mut process = Command::new(program.clone())
|
||||||
.args(args.clone())
|
.args(args.clone())
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
@@ -56,12 +59,26 @@ impl DigestEngine for DigestEngineProgram {
|
|||||||
args
|
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> {
|
fn finalize(&mut self) -> io::Result<String> {
|
||||||
Ok("program".to_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> {
|
fn get_program_path(program: &str) -> Result<String> {
|
||||||
|
|||||||
@@ -31,4 +31,8 @@ impl DigestEngine for DigestEngineSha256 {
|
|||||||
let result = self.hasher.clone().finalize();
|
let result = self.hasher.clone().finalize();
|
||||||
Ok(format!("{:x}", result))
|
Ok(format!("{:x}", result))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update(&mut self, data: &[u8]) {
|
||||||
|
self.hasher.update(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user