Add options to print header keys for summary and fingerprinting

This commit is contained in:
Bill Thiede 2023-12-02 15:12:15 -08:00
parent d78852976f
commit 2307aa144f
3 changed files with 49 additions and 11 deletions

View File

@ -32,8 +32,12 @@ struct Args {
#[arg(short, long, default_value_t = 10)]
top_n: usize,
/// Include subpart params in fingerprint
/// Include subpart headers keys in fingerprint
#[arg(short, long)]
include_header_keys: bool,
/// Include subpart params in fingerprint
#[arg(long)]
include_params: bool,
/// List of input directories to recursively search
@ -72,7 +76,12 @@ fn main() -> anyhow::Result<()> {
if should_skip(&arg) {
return None;
}
match parse(&arg, youngest, args.include_params) {
match parse(
&arg,
youngest,
args.include_header_keys,
args.include_params,
) {
Ok(Some(h)) => Some((h, arg)),
// Skip old emails
Ok(None) => return None,
@ -123,6 +132,7 @@ fn main() -> anyhow::Result<()> {
fn parse<P: AsRef<Path>>(
path: P,
youngest: i64,
include_header_keys: bool,
include_params: bool,
) -> Result<Option<String>, EmailError> {
let file = File::open(&path)?;
@ -139,5 +149,7 @@ fn parse<P: AsRef<Path>>(
return Ok(None);
}
//println!("{}: {:#?}", path.as_ref().display(), m.ctype);
Ok(Some(fingerprint(&m, include_params).join("\n")))
Ok(Some(
fingerprint(&m, include_header_keys, include_params).join("\n"),
))
}

View File

@ -9,8 +9,12 @@ use memmap::MmapOptions;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Include subpart params in fingerprint
/// Include subpart headers keys in fingerprint
#[arg(short, long)]
include_header_keys: bool,
/// Include subpart params in fingerprint
#[arg(long)]
include_params: bool,
/// List of files to summarize
@ -29,7 +33,7 @@ fn main() -> anyhow::Result<()> {
.unwrap_or("(NO SUBJECT)".to_owned());
println!(
"{subject}: {path}\n{}",
fingerprint(&m, args.include_params).join("\n")
fingerprint(&m, args.include_header_keys, args.include_params).join("\n")
);
}
Ok(())

View File

@ -65,17 +65,39 @@ pub fn should_skip(path: &str) -> bool {
SKIP_FILES.contains(&filename)
}
pub fn fingerprint(pm: &ParsedMail<'_>, include_params: bool) -> Vec<String> {
fingerprint_rec(pm, include_params, 0)
pub fn fingerprint(
pm: &ParsedMail<'_>,
include_header_keys: bool,
include_params: bool,
) -> Vec<String> {
fingerprint_rec(pm, include_header_keys, include_params, 0)
}
fn fingerprint_rec(pm: &ParsedMail<'_>, include_params: bool, depth: usize) -> Vec<String> {
fn fingerprint_rec(
pm: &ParsedMail<'_>,
include_header_keys: bool,
include_params: bool,
depth: usize,
) -> Vec<String> {
let indent = " ".repeat(depth * 2);
let mut parts = vec![format!("{}{}", indent, pm.ctype.mimetype)];
for (k, v) in &pm.ctype.params {
parts.push(format!("{indent} {k}: {v}"));
if include_header_keys {
parts.push(format!("{indent}Headers:"));
for k in &pm.headers {
parts.push(format!("{indent} {}", k.get_key()));
}
}
if include_params {
for (k, v) in &pm.ctype.params {
parts.push(format!("{indent} {k}: {v}"));
}
}
for c in &pm.subparts {
parts.extend(fingerprint_rec(&c, include_params, depth + 1));
parts.extend(fingerprint_rec(
&c,
include_header_keys,
include_params,
depth + 1,
));
}
parts
}