Files
keep/src/meta_plugin/digest.rs
Andrew Phillips 2424c543d6 fix: implement debug for meta plugin program and remove unused imports
Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
2025-08-18 08:03:55 -03:00

130 lines
2.9 KiB
Rust

use anyhow::Result;
use sha2::{Digest, Sha256};
use std::io;
use std::time::Instant;
use crate::meta_plugin::MetaPlugin;
#[derive(Debug, Clone, Default)]
pub struct DigestSha256MetaPlugin {
hasher: Sha256,
meta_name: String,
}
impl DigestSha256MetaPlugin {
pub fn new() -> DigestSha256MetaPlugin {
DigestSha256MetaPlugin {
hasher: Sha256::new(),
meta_name: "digest_sha256".to_string(),
}
}
}
impl MetaPlugin for DigestSha256MetaPlugin {
fn is_internal(&self) -> bool {
true
}
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);
}
fn meta_name(&mut self) -> String {
self.meta_name.clone()
}
}
#[derive(Debug, Clone, Default)]
pub struct ReadTimeMetaPlugin {
start_time: Option<Instant>,
meta_name: String,
}
impl ReadTimeMetaPlugin {
pub fn new() -> ReadTimeMetaPlugin {
ReadTimeMetaPlugin {
start_time: None,
meta_name: "read_time".to_string(),
}
}
}
impl MetaPlugin for ReadTimeMetaPlugin {
fn is_internal(&self) -> bool {
true
}
fn finalize(&mut self) -> io::Result<String> {
if let Some(start_time) = self.start_time {
let duration = start_time.elapsed();
Ok(format!("{:.6}s", duration.as_secs_f64()))
} else {
Ok("0.000000s".to_string())
}
}
fn update(&mut self, _data: &[u8]) {
if self.start_time.is_none() {
self.start_time = Some(Instant::now());
}
}
fn meta_name(&mut self) -> String {
self.meta_name.clone()
}
}
#[derive(Debug, Clone, Default)]
pub struct ReadRateMetaPlugin {
start_time: Option<Instant>,
bytes_read: u64,
meta_name: String,
}
impl ReadRateMetaPlugin {
pub fn new() -> ReadRateMetaPlugin {
ReadRateMetaPlugin {
start_time: None,
bytes_read: 0,
meta_name: "read_rate".to_string(),
}
}
}
impl MetaPlugin for ReadRateMetaPlugin {
fn is_internal(&self) -> bool {
true
}
fn finalize(&mut self) -> io::Result<String> {
if let Some(start_time) = self.start_time {
let duration = start_time.elapsed();
if duration.as_secs_f64() > 0.0 {
let rate = self.bytes_read as f64 / duration.as_secs_f64();
Ok(format!("{:.0} B/s", rate))
} else {
Ok("0 B/s".to_string())
}
} else {
Ok("0 B/s".to_string())
}
}
fn update(&mut self, data: &[u8]) {
if self.start_time.is_none() {
self.start_time = Some(Instant::now());
}
self.bytes_read += data.len() as u64;
}
fn meta_name(&mut self) -> String {
self.meta_name.clone()
}
}