Simplify the types used for the CLI arguments

- An `Option<Vec<_>>` is redundant, and slightly more complex to
    handle, than just using a `Vec<_>` and checking if it is empty.
This commit is contained in:
Neal H. Walfield 2023-03-17 08:33:53 +01:00
parent 9eb1e0fa7d
commit f91c21da12
No known key found for this signature in database
GPG Key ID: 6863C9AD5B4D22D3
12 changed files with 63 additions and 77 deletions

View File

@ -147,7 +147,7 @@ pub fn certify(config: Config, c: certify::Command)
(Some(_), Some(_)) => unreachable!("conflicting args"),
}
let notations = parse_notations(c.notation.unwrap_or_default())?;
let notations = parse_notations(c.notation)?;
for (critical, n) in notations {
builder = builder.add_notation(
n.name(),

View File

@ -188,7 +188,7 @@ impl<'a, 'certdb> VerificationHelper for Helper<'a, 'certdb> {
dumper.packet(&mut io::stderr(),
pp.recursion_depth() as usize,
pp.header().clone(), pp.packet.clone(),
pp.map().cloned(), None)?;
pp.map().cloned(), Vec::new())?;
}
Ok(())
}

View File

@ -75,15 +75,15 @@ pub fn dump<W>(input: &mut (dyn io::Read + Sync + Send),
Packet::Literal(_) => {
let mut prefix = vec![0; 40];
let n = pp.read(&mut prefix)?;
Some(vec![
vec![
format!("Content: {:?}{}",
String::from_utf8_lossy(&prefix[..n]),
if n == prefix.len() { "..." } else { "" }),
])
]
},
Packet::SEIP(_) if sk.is_none() => {
message_encrypted = true;
Some(vec!["No session key supplied".into()])
vec!["No session key supplied".into()]
}
Packet::SEIP(_) if sk.is_some() => {
message_encrypted = true;
@ -113,11 +113,11 @@ pub fn dump<W>(input: &mut (dyn io::Read + Sync + Send),
};
fields.push("Decryption failed".into());
}
Some(fields)
fields
},
Packet::AED(_) if sk.is_none() => {
message_encrypted = true;
Some(vec!["No session key supplied".into()])
vec!["No session key supplied".into()]
}
Packet::AED(_) if sk.is_some() => {
message_encrypted = true;
@ -137,9 +137,9 @@ pub fn dump<W>(input: &mut (dyn io::Read + Sync + Send),
} else {
fields.push("Decryption failed".into());
}
Some(fields)
fields
},
_ => None,
_ => Vec::new(),
};
let header = pp.header().clone();
@ -184,13 +184,13 @@ struct Node {
header: Header,
packet: Packet,
map: Option<Map>,
additional_fields: Option<Vec<String>>,
additional_fields: Vec<String>,
children: Vec<Node>,
}
impl Node {
fn new(header: Header, packet: Packet, map: Option<Map>,
additional_fields: Option<Vec<String>>) -> Self {
additional_fields: Vec<String>) -> Self {
Node {
header,
packet,
@ -226,7 +226,7 @@ impl PacketDumper {
pub fn packet(&mut self, output: &mut dyn io::Write, depth: usize,
header: Header, p: Packet, map: Option<Map>,
additional_fields: Option<Vec<String>>)
additional_fields: Vec<String>)
-> Result<()> {
let node = Node::new(header, p, map, additional_fields);
if self.root.is_none() {
@ -255,7 +255,7 @@ impl PacketDumper {
format!("{}{} ", indent,
if node.children.is_empty() { " " } else { "" });
self.dump_packet(output, &indent_node, Some(&node.header), &node.packet,
node.map.as_ref(), node.additional_fields.as_ref())?;
node.map.as_ref(), &node.additional_fields)?;
if node.children.is_empty() {
return Ok(());
}
@ -275,7 +275,7 @@ impl PacketDumper {
fn dump_packet(&self, mut output: &mut dyn io::Write, i: &str,
header: Option<&Header>, p: &Packet, map: Option<&Map>,
additional_fields: Option<&Vec<String>>)
additional_fields: &Vec<String>)
-> Result<()> {
use self::openpgp::Packet::*;
@ -723,10 +723,8 @@ impl PacketDumper {
u => writeln!(output, "{} Unknown variant: {:?}", i, u)?,
}
if let Some(fields) = additional_fields {
for field in fields {
writeln!(output, "{} {}", i, field)?;
}
for field in additional_fields {
writeln!(output, "{} {}", i, field)?;
}
writeln!(output, "{}", i)?;
@ -899,7 +897,7 @@ impl PacketDumper {
let indent = format!("{} ", i);
write!(output, "{}", indent)?;
self.dump_packet(output, &indent, None, &sig.clone().into(),
None, None)?;
None, &Vec::new())?;
},
_ => {
if s.critical() {

View File

@ -47,12 +47,11 @@ fn generate(
let mut builder = CertBuilder::new();
// User ID
match command.userid {
Some(uids) => for uid in uids {
if command.userid.is_empty() {
eprintln!("No user ID given, using direct key signature");
} else {
for uid in command.userid {
builder = builder.add_userid(uid);
},
None => {
eprintln!("No user ID given, using direct key signature");
}
}

View File

@ -40,44 +40,36 @@ pub fn dispatch(config: Config, c: keyring::Command) -> Result<()> {
match c.subcommand {
Filter(command) => {
let any_uid_predicates =
command.userid.is_some()
|| command.name.is_some()
|| command.email.is_some()
|| command.domain.is_some();
! command.userid.is_empty()
|| !command.name.is_empty()
|| !command.email.is_empty()
|| !command.domain.is_empty();
let uid_predicate = |uid: &UserID| {
let mut keep = false;
if let Some(userids) = &command.userid {
for userid in userids {
keep |= uid.value() == userid.as_bytes();
}
for userid in &command.userid {
keep |= uid.value() == userid.as_bytes();
}
if let Some(names) = &command.name {
for name in names {
keep |= uid
.name().unwrap_or(None)
.map(|n| &n == name)
.unwrap_or(false);
}
for name in &command.name {
keep |= uid
.name().unwrap_or(None)
.map(|n| &n == name)
.unwrap_or(false);
}
if let Some(emails) = &command.email {
for email in emails {
keep |= uid
.email().unwrap_or(None)
.map(|n| &n == email)
.unwrap_or(false);
}
for email in &command.email {
keep |= uid
.email().unwrap_or(None)
.map(|n| &n == email)
.unwrap_or(false);
}
if let Some(domains) = &command.domain {
for domain in domains {
keep |= uid
.email().unwrap_or(None)
.map(|n| n.ends_with(&format!("@{}", domain)))
.unwrap_or(false);
}
for domain in &command.domain {
keep |= uid
.email().unwrap_or(None)
.map(|n| n.ends_with(&format!("@{}", domain)))
.unwrap_or(false);
}
keep
@ -86,15 +78,12 @@ pub fn dispatch(config: Config, c: keyring::Command) -> Result<()> {
let any_ua_predicates = false;
let ua_predicate = |_ua: &UserAttribute| false;
let any_key_predicates = command.handle.is_some();
let handles: Vec<KeyHandle> =
if let Some(handles) = &command.handle {
use std::str::FromStr;
handles.iter().map(|h| KeyHandle::from_str(h))
.collect::<Result<_>>()?
} else {
Vec::with_capacity(0)
};
let any_key_predicates = ! command.handle.is_empty();
let handles: Vec<KeyHandle> = {
use std::str::FromStr;
command.handle.iter().map(|h| KeyHandle::from_str(h))
.collect::<Result<_>>()?
};
let key_predicate = |key: &Key<_, _>| {
let mut keep = false;

View File

@ -64,7 +64,7 @@ pub fn revoke_certificate(config: Config, c: revoke::CertificateCommand) -> Resu
let time = c.time.map(|t| t.time.into());
let notations = parse_notations(c.notation.unwrap_or_default())?;
let notations = parse_notations(c.notation)?;
revoke(
config,
@ -99,7 +99,7 @@ pub fn revoke_subkey(config: Config, c: revoke::SubkeyCommand) -> Result<()> {
let time = c.time.map(|t| t.time.into());
let notations = parse_notations(c.notation.unwrap_or_default())?;
let notations = parse_notations(c.notation)?;
revoke(
config,
@ -126,7 +126,7 @@ pub fn revoke_userid(config: Config, c: revoke::UseridCommand) -> Result<()> {
let time = c.time.map(|t| t.time.into());
let notations = parse_notations(c.notation.unwrap_or_default())?;
let notations = parse_notations(c.notation)?;
revoke(
config,

View File

@ -791,7 +791,7 @@ fn main() -> Result<()> {
load_certs(command.secret_key_file.iter().map(|s| s.as_ref()))?;
let time = command.time.map(|t| t.time.into());
let notations = parse_notations(command.notation.unwrap_or_default())?;
let notations = parse_notations(command.notation)?;
if let Some(merge) = command.merge {
let output = config.create_or_stdout_pgp(output, binary,

View File

@ -134,7 +134,7 @@ $ sq certify --time 20130721T0550+0200 neal.pgp ada.pgp ada
then it will ignore the signature. The notation is marked as \
being human readable."
)]
pub notation: Option<Vec<String>>,
pub notation: Vec<String>,
#[clap(
long = "expires",
value_name = "TIME",

View File

@ -83,7 +83,7 @@ pub struct GenerateCommand {
value_name = "EMAIL",
help = "Adds a userid to the key"
)]
pub userid: Option<Vec<String>>,
pub userid: Vec<String>,
#[clap(
short = 'c',
long = "cipher-suite",

View File

@ -92,7 +92,7 @@ pub struct FilterCommand {
long_help = "Case-sensitively matches on the \
user id, requiring an exact match.",
)]
pub userid: Option<Vec<String>>,
pub userid: Vec<String>,
#[clap(
long = "name",
value_name = "NAME",
@ -102,7 +102,7 @@ pub struct FilterCommand {
and case-sensitively matches on the \
name, requiring an exact match.",
)]
pub name: Option<Vec<String>>,
pub name: Vec<String>,
#[clap(
long = "email",
value_name = "ADDRESS",
@ -112,7 +112,7 @@ pub struct FilterCommand {
address and case-sensitively matches \
on the email address, requiring an exact match.",
)]
pub email: Option<Vec<String>>,
pub email: Vec<String>,
#[clap(
long = "domain",
value_name = "FQDN",
@ -123,7 +123,7 @@ pub struct FilterCommand {
on the domain of the email address, \
requiring an exact match.",
)]
pub domain: Option<Vec<String>>,
pub domain: Vec<String>,
#[clap(
long = "handle",
value_name = "FINGERPRINT|KEYID",
@ -133,7 +133,7 @@ pub struct FilterCommand {
including those certificates that match the \
given fingerprint or key id.",
)]
pub handle: Option<Vec<String>>,
pub handle: Vec<String>,
#[clap(
short = 'P',
long = "prune-certs",

View File

@ -176,7 +176,7 @@ certificate's creation time",
then it will ignore the signature. The notation is marked as \
being human readable."
)]
pub notation: Option<Vec<String>>,
pub notation: Vec<String>,
#[clap(
short = 'B',
long,
@ -330,7 +330,7 @@ certificate's creation time",
then it will ignore the signature. The notation is marked as \
being human readable."
)]
pub notation: Option<Vec<String>>,
pub notation: Vec<String>,
#[clap(
short = 'B',
long,
@ -451,7 +451,7 @@ certificate's creation time",
then it will ignore the signature. The notation is marked as \
being human readable."
)]
pub notation: Option<Vec<String>>,
pub notation: Vec<String>,
#[clap(
short = 'B',
long,

View File

@ -115,5 +115,5 @@ pub struct Command {
// TODO: Is there a better way to express that one notation consists of two arguments, and
// there may be multiple notations? Like something like Vec<(String, String)>.
// TODO: Also, no need for the Option
pub notation: Option<Vec<String>>,
pub notation: Vec<String>,
}