From 623b6a783184e0e8ec0f27f7cabfe8db62abb1bb Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Wed, 6 Jan 2021 12:53:44 +0100 Subject: sq: Generalize join to filter. --- sq/src/commands/certring.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/sq/src/commands/certring.rs b/sq/src/commands/certring.rs index 094861f4..351aad7c 100644 --- a/sq/src/commands/certring.rs +++ b/sq/src/commands/certring.rs @@ -9,7 +9,10 @@ use sequoia_openpgp as openpgp; use openpgp::{ Result, armor, - cert::CertParser, + cert::{ + Cert, + CertParser, + }, parse::Parse, serialize::Serialize, }; @@ -31,7 +34,7 @@ pub fn dispatch(m: &clap::ArgMatches, force: bool) -> Result<()> { force, m.is_present("binary"), armor::Kind::PublicKey)?; - join(m.values_of("input"), &mut output)?; + filter(m.values_of("input"), &mut output, |c| Some(c))?; output.finalize() }, ("list", Some(m)) => { @@ -61,21 +64,28 @@ pub fn dispatch(m: &clap::ArgMatches, force: bool) -> Result<()> { } } -/// Joins cert(ring)s into a certring. -fn join(inputs: Option, output: &mut dyn io::Write) - -> Result<()> { +/// Joins cert(ring)s into a certring, applying a filter. +fn filter(inputs: Option, output: &mut dyn io::Write, + mut filter: F) + -> Result<()> + where F: FnMut(Cert) -> Option, +{ if let Some(inputs) = inputs { for name in inputs { for cert in CertParser::from_file(name)? { let cert = cert.context( format!("Malformed certificate in certring {:?}", name))?; - cert.serialize(output)?; + if let Some(cert) = filter(cert) { + cert.serialize(output)?; + } } } } else { for cert in CertParser::from_reader(io::stdin())? { let cert = cert.context("Malformed certificate in certring")?; - cert.serialize(output)?; + if let Some(cert) = filter(cert) { + cert.serialize(output)?; + } } } Ok(()) -- cgit v1.2.3