refactor: remove digest engine modules and related code

This commit is contained in:
Andrew Phillips
2025-07-29 10:16:31 -03:00
committed by Andrew Phillips (aider)
parent e51a902660
commit 1a70bfee79
4 changed files with 0 additions and 265 deletions

View File

@@ -1,74 +0,0 @@
use anyhow::Result;
use std::io;
use lazy_static::lazy_static;
extern crate enum_map;
use enum_map::enum_map;
use enum_map::{Enum, EnumMap};
pub mod none;
pub mod program;
pub mod sha2;
use crate::digest_engine::none::DigestEngineNone;
use crate::digest_engine::program::DigestEngineProgram;
use crate::digest_engine::sha2::DigestEngineSha256;
use strum::IntoEnumIterator;
#[derive(Debug, Eq, PartialEq, Clone, strum::EnumIter, strum::Display, strum::EnumString, Enum)]
#[strum(ascii_case_insensitive)]
pub enum DigestType {
Sha256,
Md5,
None,
}
pub trait DigestEngine {
fn is_supported(&self) -> bool {
true
}
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]);
}
use std::io::Write;
lazy_static! {
pub static ref DIGEST_PROGRAMS: EnumMap<DigestType, Option<DigestEngineProgram>> = enum_map! {
DigestType::Sha256 => None,
DigestType::Md5 => {
let program = DigestEngineProgram::new("md5sum", vec![]);
if program.supported { Some(program) } else { None }
}
DigestType::None => None
};
}
pub fn get_digest_engine(digest_type: DigestType) -> Box<dyn DigestEngine> {
match digest_type {
DigestType::Sha256 => Box::new(DigestEngineSha256::new()),
DigestType::Md5 => Box::new(DigestEngineProgram::new("md5sum", vec![])),
DigestType::None => Box::new(DigestEngineNone::new()),
}
}
pub fn default_digest_type() -> DigestType {
let mut default = DigestType::None;
for digest_type in DigestType::iter() {
let digest_engine = get_digest_engine(digest_type.clone());
if digest_engine.is_supported() {
default = digest_type;
break;
}
}
default
}
pub fn get_digest_type_meta(digest_type: DigestType) -> String {
format!("digest_{}", digest_type.to_string().to_lowercase())
}

View File

@@ -1,43 +0,0 @@
use crate::digest_engine::DigestEngine;
use anyhow::Result;
use std::io::{self, Write};
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct DigestEngineNone {}
impl DigestEngineNone {
pub fn new() -> DigestEngineNone {
DigestEngineNone {}
}
}
impl DigestEngine for DigestEngineNone {
fn create(&self) -> Result<Box<dyn Write>> {
Ok(Box::new(DummyWriter::new()))
}
fn finalize(&mut self) -> io::Result<String> {
Ok("none".to_string())
}
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

@@ -1,87 +0,0 @@
use crate::plugins::ProgramWriter;
use anyhow::{Context, Result, anyhow};
use log::*;
use std::env;
use std::fs;
use std::io;
use std::io::Write;
use std::os::unix::fs::PermissionsExt;
use std::process::{Command, Stdio};
use crate::digest_engine::DigestEngine;
#[derive(Clone, Debug)]
pub struct DigestEngineProgram {
pub program: String,
pub args: Vec<String>,
pub supported: bool,
}
impl DigestEngineProgram {
pub fn new(program: &str, args: Vec<&str>) -> DigestEngineProgram {
let program_path = get_program_path(program);
let supported = program_path.is_ok();
DigestEngineProgram {
program: program_path.unwrap_or(program.to_string()),
args: args.iter().map(|s| s.to_string()).collect(),
supported,
}
}
}
impl DigestEngine for DigestEngineProgram {
fn is_supported(&self) -> bool {
self.supported
}
fn create(&self) -> Result<Box<dyn Write>> {
debug!("DIGEST: Writting using {:?}", *self);
let program = self.program.clone();
let args = self.args.clone();
debug!("DIGEST: Executing command: {:?} {:?}", program, args);
let mut process = Command::new(program.clone())
.args(args.clone())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.context(anyhow!(
"Problem spawning child process: {:?} {:?}",
program,
args
))?;
Ok(Box::new(ProgramWriter {
stdin: process.stdin.take().unwrap(),
}))
}
fn finalize(&mut self) -> io::Result<String> {
Ok("program".to_string())
}
fn update(&mut self, _data: &[u8]) {
// This is handled by the ProgramWriter implementation
}
}
fn get_program_path(program: &str) -> Result<String> {
debug!("DIGEST: Looking for executable: {}", program);
if let Ok(path) = env::var("PATH") {
for p in path.split(':') {
let p_str = format!("{}/{}", p, program);
let stat = fs::metadata(p_str.clone());
if let Ok(stat) = stat {
let md = stat;
let permissions = md.permissions();
if md.is_file() && permissions.mode() & 0o111 != 0 {
return Ok(p_str);
}
}
}
}
Err(anyhow!("Unable to find binary {} in PATH", program))
}

View File

@@ -1,61 +0,0 @@
use anyhow::Result;
use sha2::{Digest, Sha256};
use std::io;
use std::io::Write;
use crate::digest_engine::DigestEngine;
#[derive(Debug, Clone, Default)]
pub struct DigestEngineSha256 {
hasher: Sha256,
}
impl DigestEngineSha256 {
pub fn new() -> DigestEngineSha256 {
DigestEngineSha256 {
hasher: Sha256::new(),
}
}
// 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 {
fn create(&self) -> Result<Box<dyn Write>> {
Ok(Box::new(self.create_writer()))
}
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);
}
}