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)] #[arg(short, long, default_value_t = 10)]
top_n: usize, top_n: usize,
/// Include subpart params in fingerprint /// Include subpart headers keys in fingerprint
#[arg(short, long)] #[arg(short, long)]
include_header_keys: bool,
/// Include subpart params in fingerprint
#[arg(long)]
include_params: bool, include_params: bool,
/// List of input directories to recursively search /// List of input directories to recursively search
@ -72,7 +76,12 @@ fn main() -> anyhow::Result<()> {
if should_skip(&arg) { if should_skip(&arg) {
return None; 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)), Ok(Some(h)) => Some((h, arg)),
// Skip old emails // Skip old emails
Ok(None) => return None, Ok(None) => return None,
@ -123,6 +132,7 @@ fn main() -> anyhow::Result<()> {
fn parse<P: AsRef<Path>>( fn parse<P: AsRef<Path>>(
path: P, path: P,
youngest: i64, youngest: i64,
include_header_keys: bool,
include_params: bool, include_params: bool,
) -> Result<Option<String>, EmailError> { ) -> Result<Option<String>, EmailError> {
let file = File::open(&path)?; let file = File::open(&path)?;
@ -139,5 +149,7 @@ fn parse<P: AsRef<Path>>(
return Ok(None); return Ok(None);
} }
//println!("{}: {:#?}", path.as_ref().display(), m.ctype); //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)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
struct Args { struct Args {
/// Include subpart params in fingerprint /// Include subpart headers keys in fingerprint
#[arg(short, long)] #[arg(short, long)]
include_header_keys: bool,
/// Include subpart params in fingerprint
#[arg(long)]
include_params: bool, include_params: bool,
/// List of files to summarize /// List of files to summarize
@ -29,7 +33,7 @@ fn main() -> anyhow::Result<()> {
.unwrap_or("(NO SUBJECT)".to_owned()); .unwrap_or("(NO SUBJECT)".to_owned());
println!( println!(
"{subject}: {path}\n{}", "{subject}: {path}\n{}",
fingerprint(&m, args.include_params).join("\n") fingerprint(&m, args.include_header_keys, args.include_params).join("\n")
); );
} }
Ok(()) Ok(())

View File

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