| 1 |
use anyhow::Context; |
| 2 |
use anyhow::Result; |
| 3 |
|
| 4 |
use typenum::Unsigned; |
| 5 |
|
| 6 |
use sequoia_openpgp as openpgp; |
| 7 |
use openpgp::Cert; |
| 8 |
use openpgp::cert::ValidCert; |
| 9 |
use openpgp::packet::UserID; |
| 10 |
|
| 11 |
use crate::cli::escape_for_shell; |
| 12 |
|
| 13 |
// Whether to enable the --name parameters. Currently disabled. |
| 14 |
// See https://gitlab.com/sequoia-pgp/sequoia-sq/-/issues/487 . |
| 15 |
const ENABLE_NAME: bool = false; |
| 16 |
|
| 17 |
/// Adds a `--all` argument. |
| 18 |
pub type AllUserIDsArg = typenum::U1; |
| 19 |
|
| 20 |
/// Adds `--userid` `--email`, and `--name` arguments. |
| 21 |
/// |
| 22 |
/// For `UserIDDesignator::resolve`, the value must match on a |
| 23 |
/// self-signed user ID, but returns the value as is. That is, if |
| 24 |
/// there is a self-signed user ID "Alice <[email protected]>", |
| 25 |
/// "--email [email protected]" matches and returns the user ID |
| 26 |
/// "<[email protected]>". |
| 27 |
pub type ExactArgs = typenum::U2; |
| 28 |
|
| 29 |
/// Adds `--userid`, `--userid-by-email`, and `--userid-by-name` |
| 30 |
/// arguments. |
| 31 |
/// |
| 32 |
/// For `UserIDDesignator::resolve`, the value must correspond to a |
| 33 |
/// self-signed user ID. That is, if there is a self-signed user ID |
| 34 |
/// "Alice <[email protected]>", "--email [email protected]" matches |
| 35 |
/// and returns the matching user ID, i.e., "Alice |
| 36 |
/// <[email protected]>". |
| 37 |
pub type ByArgs = typenum::U4; |
| 38 |
|
| 39 |
/// Adds a `--add-userid`, `--add-email`, and `--add-name` argument. |
| 40 |
/// |
| 41 |
/// For `UserIDDesignator::resolve`, acts like `ExactArgs`, but if |
| 42 |
/// there is no matching self-signed user ID, creates one from the |
| 43 |
/// value. |
| 44 |
pub type AddArgs = typenum::U8; |
| 45 |
|
| 46 |
|
| 47 |
/// Requires ByArgs, conflicts with ExactArgs. Renames ByArgs' |
| 48 |
/// arguments to `--userid`, `--email`, and `--name`. |
| 49 |
pub type PlainIsBy = typenum::U16; |
| 50 |
|
| 51 |
/// Requires AddArgs, conflicts with ExactArgs. Renames AddArgs' |
| 52 |
/// arguments to `--userid`, `--email`, and `--name`. |
| 53 |
pub type PlainIsAdd = typenum::U32; |
| 54 |
|
| 55 |
|
| 56 |
pub type PlainByArgs |
| 57 |
= <ByArgs as std::ops::BitOr<PlainIsBy>>::Output; |
| 58 |
|
| 59 |
pub type PlainAddArgs |
| 60 |
= <AddArgs as std::ops::BitOr<PlainIsAdd>>::Output; |
| 61 |
|
| 62 |
pub type ExactAndAddArgs |
| 63 |
= <ExactArgs as std::ops::BitOr<AddArgs>>::Output; |
| 64 |
|
| 65 |
pub type ExactByAndAddArgs |
| 66 |
= <ByArgs as std::ops::BitOr<ExactAndAddArgs>>::Output; |
| 67 |
|
| 68 |
pub type AllExactByAndAddArgs |
| 69 |
= <AllUserIDsArg as std::ops::BitOr<ExactByAndAddArgs>>::Output; |
| 70 |
|
| 71 |
pub type PlainAddAndByArgs |
| 72 |
= <ByArgs as std::ops::BitOr<PlainAddArgs>>::Output; |
| 73 |
|
| 74 |
pub type AllPlainAddAndByArgs |
| 75 |
= <AllUserIDsArg as std::ops::BitOr<PlainAddAndByArgs>>::Output; |
| 76 |
|
| 77 |
/// Argument parser options. |
| 78 |
|
| 79 |
/// Normally it is possible to designate multiple certificates. This |
| 80 |
/// errors out if there is more than one value. |
| 81 |
pub type OneValue = typenum::U1; |
| 82 |
|
| 83 |
/// Normally a certificate designator is required, and errors out if |
| 84 |
/// there isn't at least one value. This makes the cert designator |
| 85 |
/// completely optional. |
| 86 |
pub type OptionalValue = typenum::U2; |
| 87 |
|
| 88 |
/// Doesn't lint new (non-self signed) user IDs. This also suppresses |
| 89 |
/// the `--allow-non-canonical-userid` flag. |
| 90 |
pub type NoLinting = typenum::U4; |
| 91 |
|
| 92 |
/// Makes --all match non-self signed user IDs. |
| 93 |
pub type AllMatchesNonSelfSigned = typenum::U8; |
| 94 |
|
| 95 |
pub type OneValueNoLinting |
| 96 |
= <OneValue as std::ops::BitOr<NoLinting>>::Output; |
| 97 |
|
| 98 |
pub type AllMatchesNonSelfSignedNoLinting |
| 99 |
= <AllMatchesNonSelfSigned as std::ops::BitOr<NoLinting>>::Output; |
| 100 |
|
| 101 |
|
| 102 |
/// The documentation. |
| 103 |
pub trait Documentation { |
| 104 |
/// Returns the help text to display. |
| 105 |
fn help(typ: UserIDDesignatorType, |
| 106 |
plain: bool, |
| 107 |
semantics: UserIDDesignatorSemantics) |
| 108 |
-> (&'static str, Option<&'static str>); |
| 109 |
} |
| 110 |
|
| 111 |
/// Documentation for user ID designators when the designator selects |
| 112 |
/// a self-signed user ID in a certificate. |
| 113 |
/// |
| 114 |
/// This is for commands like `sq key userid revoke`. |
| 115 |
#[derive(Debug, Clone)] |
| 116 |
pub struct SelfSignedDocumentation(()); |
| 117 |
|
| 118 |
impl Documentation for SelfSignedDocumentation { |
| 119 |
fn help(typ: UserIDDesignatorType, |
| 120 |
plain: bool, |
| 121 |
semantics: UserIDDesignatorSemantics) |
| 122 |
-> (&'static str, Option<&'static str>) |
| 123 |
{ |
| 124 |
use UserIDDesignatorType::*; |
| 125 |
use UserIDDesignatorSemantics::*; |
| 126 |
match (typ, semantics) { |
| 127 |
(UserID, Exact | By) => { |
| 128 |
("Use the specified self-signed user ID", |
| 129 |
Some("\ |
| 130 |
Use the specified self-signed user ID |
| 131 |
|
| 132 |
The specified user ID must be self signed.")) |
| 133 |
} |
| 134 |
(UserID, Add) => { |
| 135 |
("Use the specified user ID", |
| 136 |
if plain { |
| 137 |
Some("\ |
| 138 |
Use the specified user ID |
| 139 |
|
| 140 |
The specified user ID does not need to be self signed.") |
| 141 |
} else { |
| 142 |
Some("\ |
| 143 |
Use the specified user ID |
| 144 |
|
| 145 |
The specified user ID does not need to be self signed. |
| 146 |
|
| 147 |
Because using a user ID that is not self-signed is often a mistake, \ |
| 148 |
you need to use this option to explicitly opt in.") |
| 149 |
}) |
| 150 |
} |
| 151 |
(Email, Exact) => { |
| 152 |
("\ |
| 153 |
Use a user ID consisting of just the email address, if the email address \ |
| 154 |
occurs in a self-signed user ID", |
| 155 |
None) |
| 156 |
} |
| 157 |
(Email, By) => { |
| 158 |
("Use the self-signed user ID with the specified email address", |
| 159 |
None) |
| 160 |
} |
| 161 |
(Email, Add) => { |
| 162 |
("Use a user ID with the specified email address", |
| 163 |
Some("\ |
| 164 |
Use a user ID with the specified email address |
| 165 |
|
| 166 |
The user ID consists of just the email address. The email address does not \ |
| 167 |
have to appear in a self-signed user ID.")) |
| 168 |
} |
| 169 |
(Name, Exact) => { |
| 170 |
("\ |
| 171 |
Use a user ID consisting of just the display name, if the display name \ |
| 172 |
occurs in a self-signed user ID", |
| 173 |
None) |
| 174 |
} |
| 175 |
(Name, By) => { |
| 176 |
("Use the self-signed user ID with the specified display name", |
| 177 |
None) |
| 178 |
} |
| 179 |
(Name, Add) => { |
| 180 |
("Use a user ID with the specified display name", |
| 181 |
Some("\ |
| 182 |
Use a user ID with the specified display name |
| 183 |
|
| 184 |
The user ID consists of just the display named. The display name does not \ |
| 185 |
have to appear in a self-signed user ID.")) |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/// The designator type. |
| 192 |
pub enum UserIDDesignatorType { |
| 193 |
/// --userid. |
| 194 |
UserID, |
| 195 |
/// --email. |
| 196 |
Email, |
| 197 |
/// --name. |
| 198 |
Name, |
| 199 |
} |
| 200 |
|
| 201 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 202 |
pub enum UserIDDesignatorSemantics { |
| 203 |
/// For `UserIDDesignator::resolve`, the value must match on a |
| 204 |
/// self-signed user ID, but returns the value. That is, if there |
| 205 |
/// is a self-signed user ID "Alice <[email protected]>", "--email |
| 206 |
/// [email protected]" matches and returns the user ID |
| 207 |
/// "<[email protected]>". |
| 208 |
Exact, |
| 209 |
|
| 210 |
/// For `UserIDDesignator::resolve`, the value must correspond to |
| 211 |
/// a self-signed user ID. That is, if there is a self-signed |
| 212 |
/// user ID "Alice <[email protected]>", "--email |
| 213 |
/// [email protected]" matches and returns the matching user ID, |
| 214 |
/// i.e., "Alice <[email protected]>". |
| 215 |
By, |
| 216 |
|
| 217 |
/// `UserIDDesignator::resolve`, acts like `ExactArgs`, but if |
| 218 |
/// there is no matching self-signed user ID, creates one from the |
| 219 |
/// value. |
| 220 |
Add, |
| 221 |
} |
| 222 |
|
| 223 |
#[allow(unused)] |
| 224 |
impl UserIDDesignatorSemantics { |
| 225 |
/// If `self` is `UserIDDesignatorSemantics::Exact`. |
| 226 |
fn is_exact(&self) -> bool { |
| 227 |
matches!(self, &UserIDDesignatorSemantics::Exact) |
| 228 |
} |
| 229 |
|
| 230 |
/// If `self` is `UserIDDesignatorSemantics::By`. |
| 231 |
fn is_by(&self) -> bool { |
| 232 |
matches!(self, &UserIDDesignatorSemantics::By) |
| 233 |
} |
| 234 |
|
| 235 |
/// If `self` is `UserIDDesignatorSemantics::Add`. |
| 236 |
fn is_add(&self) -> bool { |
| 237 |
matches!(self, &UserIDDesignatorSemantics::Add) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/// A user ID designator. |
| 242 |
#[derive(Debug, Clone)] |
| 243 |
pub enum UserIDDesignator { |
| 244 |
/// A user ID. |
| 245 |
UserID(UserIDDesignatorSemantics, String), |
| 246 |
|
| 247 |
/// An email address. |
| 248 |
Email(UserIDDesignatorSemantics, String), |
| 249 |
|
| 250 |
/// A display name. |
| 251 |
Name(UserIDDesignatorSemantics, String), |
| 252 |
} |
| 253 |
|
| 254 |
#[allow(dead_code)] |
| 255 |
impl UserIDDesignator { |
| 256 |
/// Whether the designator is a user ID. |
| 257 |
pub fn is_userid(&self) -> bool { |
| 258 |
use UserIDDesignator::*; |
| 259 |
match self { |
| 260 |
UserID(_, _) => true, |
| 261 |
Email(_, _) => false, |
| 262 |
Name(_, _) => false, |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/// Whether the designator is an email address. |
| 267 |
pub fn is_email(&self) -> bool { |
| 268 |
use UserIDDesignator::*; |
| 269 |
match self { |
| 270 |
UserID(_, _) => false, |
| 271 |
Email(_, _) => true, |
| 272 |
Name(_, _) => false, |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
/// Whether the designator is a display name. |
| 277 |
pub fn is_name(&self) -> bool { |
| 278 |
use UserIDDesignator::*; |
| 279 |
match self { |
| 280 |
UserID(_, _) => false, |
| 281 |
Email(_, _) => false, |
| 282 |
Name(_, _) => true, |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
/// Returns the semantics. |
| 287 |
fn semantics(&self) -> &UserIDDesignatorSemantics { |
| 288 |
use UserIDDesignator::*; |
| 289 |
match self { |
| 290 |
UserID(s, _) | Email(s, _) | Name(s, _) => { |
| 291 |
s |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/// If `self` is `UserIDDesignatorSemantics::Exact`. |
| 297 |
pub fn is_exact(&self) -> bool { |
| 298 |
self.semantics().is_exact() |
| 299 |
} |
| 300 |
|
| 301 |
/// If `self` is `UserIDDesignatorSemantics::By`. |
| 302 |
fn is_by(&self) -> bool { |
| 303 |
self.semantics().is_by() |
| 304 |
} |
| 305 |
|
| 306 |
/// If `self` is `UserIDDesignatorSemantics::Add`. |
| 307 |
pub fn is_add(&self) -> bool { |
| 308 |
self.semantics().is_add() |
| 309 |
} |
| 310 |
|
| 311 |
/// Returns the argument's value. |
| 312 |
pub fn value(&self) -> &str { |
| 313 |
use UserIDDesignator::*; |
| 314 |
match self { |
| 315 |
UserID(_, s) | Email(_, s) | Name(_, s) => s, |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
/// Returns the argument's name. |
| 320 |
pub fn argument_name<Arguments>(&self) -> String |
| 321 |
where Arguments: Unsigned |
| 322 |
{ |
| 323 |
let arguments = Arguments::to_usize(); |
| 324 |
let _all_arg = (arguments & AllUserIDsArg::to_usize()) > 0; |
| 325 |
let exact_args = (arguments & ExactArgs::to_usize()) > 0; |
| 326 |
let by_args = (arguments & ByArgs::to_usize()) > 0; |
| 327 |
let add_args = (arguments & AddArgs::to_usize()) > 0; |
| 328 |
|
| 329 |
let plain_is_by = (arguments & PlainIsBy::to_usize()) > 0; |
| 330 |
let plain_is_add = (arguments & PlainIsAdd::to_usize()) > 0; |
| 331 |
|
| 332 |
// Can't use PlainIsBy or PlainIsAdd with ExactArgs or with |
| 333 |
// each other. |
| 334 |
assert!(! (exact_args && plain_is_by)); |
| 335 |
assert!(! (exact_args && plain_is_add)); |
| 336 |
assert!(! (plain_is_by && plain_is_add)); |
| 337 |
// If plain_is_xxx is set, then by_xxx must be set. |
| 338 |
if plain_is_by { |
| 339 |
assert!(by_args); |
| 340 |
} |
| 341 |
if plain_is_add { |
| 342 |
assert!(add_args); |
| 343 |
} |
| 344 |
|
| 345 |
use UserIDDesignator::*; |
| 346 |
let thing = match self { |
| 347 |
UserID(_, _) => { |
| 348 |
"userid" |
| 349 |
} |
| 350 |
Email(_, _) => { |
| 351 |
"email" |
| 352 |
} |
| 353 |
Name(_, _) => { |
| 354 |
"name" |
| 355 |
} |
| 356 |
}; |
| 357 |
|
| 358 |
let semantics = match self { |
| 359 |
UserID(semantics, _) | Email(semantics, _) | Name(semantics, _) => { |
| 360 |
semantics |
| 361 |
} |
| 362 |
}; |
| 363 |
|
| 364 |
use UserIDDesignatorSemantics::*; |
| 365 |
match semantics { |
| 366 |
Exact => format!("--{}", thing), |
| 367 |
By if plain_is_by => format!("--{}", thing), |
| 368 |
Add if plain_is_add => format!("--{}", thing), |
| 369 |
By => { |
| 370 |
if self.is_userid() { |
| 371 |
format!("--userid") |
| 372 |
} else { |
| 373 |
format!("--userid-by-{}", thing) |
| 374 |
} |
| 375 |
} |
| 376 |
Add => { |
| 377 |
format!("--add-{}", thing) |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
/// Returns the argument's value. |
| 383 |
/// |
| 384 |
/// The returned string is escaped. |
| 385 |
pub fn argument_value(&self) -> String |
| 386 |
{ |
| 387 |
use UserIDDesignator::*; |
| 388 |
let value = match self { |
| 389 |
UserID(_, userid) => userid, |
| 390 |
Email(_, email) => email, |
| 391 |
Name(_, name) => name, |
| 392 |
}; |
| 393 |
|
| 394 |
escape_for_shell(value).to_string() |
| 395 |
} |
| 396 |
|
| 397 |
/// Returns the argument, e.g., `[email protected]` |
| 398 |
pub fn argument<Arguments>(&self) -> String |
| 399 |
where Arguments: Unsigned |
| 400 |
{ |
| 401 |
format!("{} {}", |
| 402 |
self.argument_name::<Arguments>(), |
| 403 |
self.argument_value()) |
| 404 |
} |
| 405 |
|
| 406 |
/// Resolves to the specified user IDs. |
| 407 |
pub fn resolve_to(&self, userid: UserID) -> ResolvedUserID { |
| 408 |
ResolvedUserID { |
| 409 |
designator: Some(self.clone()), |
| 410 |
userid, |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
/// Resolves to the designated user ID. |
| 415 |
/// |
| 416 |
/// If an email or a name, first converts them to a user ID in the |
| 417 |
/// usual manner. |
| 418 |
pub fn resolve_to_self(&self) -> ResolvedUserID { |
| 419 |
use UserIDDesignator::*; |
| 420 |
let userid = match self { |
| 421 |
UserID(_, userid) => |
| 422 |
openpgp::packet::UserID::from(&userid[..]), |
| 423 |
Email(_, email) => |
| 424 |
openpgp::packet::UserID::from(&format!("<{}>", email)[..]), |
| 425 |
Name(_, name) => |
| 426 |
openpgp::packet::UserID::from(&name[..]), |
| 427 |
}; |
| 428 |
|
| 429 |
ResolvedUserID { |
| 430 |
designator: Some(self.clone()), |
| 431 |
userid, |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
/// A data structure that can be flattened into a clap `Command`, and |
| 437 |
/// adds arguments to address user IDs. |
| 438 |
/// |
| 439 |
/// Depending on `Arguments`, it adds zero or more arguments to the |
| 440 |
/// subcommand. If `ExistingUserIDArg` is selected, for instance, |
| 441 |
/// then a `--userid` argument is added. |
| 442 |
/// |
| 443 |
/// `Options` are the set of options to the argument parser. By |
| 444 |
/// default, at least one user ID designator must be specified. |
| 445 |
pub struct UserIDDesignators<Arguments, |
| 446 |
Options=typenum::U0, |
| 447 |
Documentation=SelfSignedDocumentation> |
| 448 |
{ |
| 449 |
/// The set of certificate designators. |
| 450 |
pub designators: Vec<UserIDDesignator>, |
| 451 |
|
| 452 |
/// Use all self-signed user IDs. |
| 453 |
pub all: Option<bool>, |
| 454 |
|
| 455 |
/// Whether --all should match non-self signed user IDs. |
| 456 |
all_matches_non_self_signed: bool, |
| 457 |
|
| 458 |
/// Whether --allow-non-canonical-userids was passed. |
| 459 |
pub allow_non_canonical_userids: bool, |
| 460 |
|
| 461 |
arguments: std::marker::PhantomData<(Arguments, Options, Documentation)>, |
| 462 |
} |
| 463 |
|
| 464 |
impl<Arguments, Options, Documentation> std::fmt::Debug |
| 465 |
for UserIDDesignators<Arguments, Options, Documentation> |
| 466 |
{ |
| 467 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 468 |
f.debug_struct("UserIDDesignators") |
| 469 |
.field("designators", &self.designators) |
| 470 |
.field("all", &self.all) |
| 471 |
.finish() |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
#[allow(dead_code)] |
| 476 |
impl<Arguments, Options, Documentation> |
| 477 |
UserIDDesignators<Arguments, Options, Documentation> |
| 478 |
{ |
| 479 |
/// Like `Vec::push`. |
| 480 |
pub fn push(&mut self, designator: UserIDDesignator) { |
| 481 |
self.designators.push(designator) |
| 482 |
} |
| 483 |
|
| 484 |
/// Like `Vec::is_empty`. |
| 485 |
pub fn is_empty(&self) -> bool { |
| 486 |
self.designators.is_empty() |
| 487 |
} |
| 488 |
|
| 489 |
/// Like `Vec::len`. |
| 490 |
pub fn len(&self) -> usize { |
| 491 |
self.designators.len() |
| 492 |
} |
| 493 |
|
| 494 |
/// Iterates over the user ID designators. |
| 495 |
pub fn iter(&self) -> impl Iterator<Item=&UserIDDesignator> { |
| 496 |
self.designators.iter() |
| 497 |
} |
| 498 |
|
| 499 |
/// Returns whether the all flag was set. |
| 500 |
/// |
| 501 |
/// If the flag was not enabled, returns `None`. |
| 502 |
pub fn all(&self) -> Option<bool> { |
| 503 |
self.all |
| 504 |
} |
| 505 |
|
| 506 |
/// Returns whether --all should match non-self signed user IDs. |
| 507 |
pub fn all_matches_non_self_signed(&self) -> bool { |
| 508 |
self.all_matches_non_self_signed |
| 509 |
} |
| 510 |
|
| 511 |
/// Returns whether the allow-non-canonical-userids flag was set. |
| 512 |
pub fn allow_non_canonical_userids(&self) -> bool { |
| 513 |
self.allow_non_canonical_userids |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
impl<Arguments, Options, Docs> clap::Args |
| 518 |
for UserIDDesignators<Arguments, Options, Docs> |
| 519 |
where |
| 520 |
Arguments: typenum::Unsigned, |
| 521 |
Options: typenum::Unsigned, |
| 522 |
Docs: Documentation + std::fmt::Debug, |
| 523 |
{ |
| 524 |
fn augment_args(mut cmd: clap::Command) -> clap::Command |
| 525 |
{ |
| 526 |
let arguments = Arguments::to_usize(); |
| 527 |
let all_arg = (arguments & AllUserIDsArg::to_usize()) > 0; |
| 528 |
let exact_args = (arguments & ExactArgs::to_usize()) > 0; |
| 529 |
let by_args = (arguments & ByArgs::to_usize()) > 0; |
| 530 |
let add_args = (arguments & AddArgs::to_usize()) > 0; |
| 531 |
|
| 532 |
let plain_is_by = (arguments & PlainIsBy::to_usize()) > 0; |
| 533 |
let plain_is_add = (arguments & PlainIsAdd::to_usize()) > 0; |
| 534 |
|
| 535 |
// Can't use PlainIsBy or PlainIsAdd with ExactArgs or with |
| 536 |
// each other. |
| 537 |
assert!(! (exact_args && plain_is_by)); |
| 538 |
assert!(! (exact_args && plain_is_add)); |
| 539 |
assert!(! (plain_is_by && plain_is_add)); |
| 540 |
// If plain_is_xxx is set, then by_xxx must be set. |
| 541 |
if plain_is_by { |
| 542 |
assert!(by_args); |
| 543 |
} |
| 544 |
if plain_is_add { |
| 545 |
assert!(add_args); |
| 546 |
} |
| 547 |
|
| 548 |
let options = Options::to_usize(); |
| 549 |
let one_value = (options & OneValue::to_usize()) > 0; |
| 550 |
let optional_value = (options & OptionalValue::to_usize()) > 0; |
| 551 |
let no_linting = (options & NoLinting::to_usize()) > 0; |
| 552 |
|
| 553 |
let group = format!("userid-designator-{:X}-{:X}", |
| 554 |
arguments, |
| 555 |
options); |
| 556 |
let mut arg_group = clap::ArgGroup::new(&group); |
| 557 |
if one_value { |
| 558 |
arg_group = arg_group.multiple(false); |
| 559 |
} else { |
| 560 |
arg_group = arg_group.multiple(true); |
| 561 |
} |
| 562 |
|
| 563 |
if optional_value { |
| 564 |
arg_group = arg_group.required(false); |
| 565 |
} else { |
| 566 |
arg_group = arg_group.required(true); |
| 567 |
} |
| 568 |
|
| 569 |
let action = if one_value { |
| 570 |
clap::ArgAction::Set |
| 571 |
} else { |
| 572 |
clap::ArgAction::Append |
| 573 |
}; |
| 574 |
|
| 575 |
fn parse_as_email(s: &str) -> Result<String> { |
| 576 |
let userid = openpgp::packet::UserID::from(format!("<{}>", s)); |
| 577 |
match userid.email_normalized() { |
| 578 |
Ok(Some(email)) => { |
| 579 |
Ok(email) |
| 580 |
} |
| 581 |
Ok(None) => { |
| 582 |
Err(anyhow::anyhow!( |
| 583 |
"{:?} is not a valid email address", s)) |
| 584 |
} |
| 585 |
Err(err) => { |
| 586 |
Err(err).context(format!( |
| 587 |
"{:?} is not a valid email address", s)) |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
if all_arg { |
| 593 |
let full_name = "all"; |
| 594 |
cmd = cmd.arg( |
| 595 |
clap::Arg::new(&full_name) |
| 596 |
.long(&full_name) |
| 597 |
.action(clap::ArgAction::SetTrue) |
| 598 |
.help("\ |
| 599 |
Use all self-signed user IDs")); |
| 600 |
arg_group = arg_group.arg(full_name); |
| 601 |
} |
| 602 |
|
| 603 |
use UserIDDesignatorType::*; |
| 604 |
use UserIDDesignatorSemantics::*; |
| 605 |
if exact_args || plain_is_by { |
| 606 |
let full_name = "userid"; |
| 607 |
let (help, long_help) = Docs::help( |
| 608 |
UserID, true, if plain_is_by { By } else { Exact }); |
| 609 |
|
| 610 |
let mut arg = clap::Arg::new(&full_name) |
| 611 |
.long(&full_name) |
| 612 |
.value_name("USERID") |
| 613 |
.action(action.clone()) |
| 614 |
.help(help); |
| 615 |
if let Some(long_help) = long_help { |
| 616 |
arg = arg.long_help(long_help); |
| 617 |
} |
| 618 |
if all_arg { |
| 619 |
arg = arg.conflicts_with("all"); |
| 620 |
} |
| 621 |
cmd = cmd.arg(arg); |
| 622 |
|
| 623 |
arg_group = arg_group.arg(full_name); |
| 624 |
} |
| 625 |
|
| 626 |
if add_args { |
| 627 |
let full_name = if plain_is_add { |
| 628 |
"userid" |
| 629 |
} else { |
| 630 |
"add-userid" |
| 631 |
}; |
| 632 |
|
| 633 |
let (help, long_help) = Docs::help(UserID, false, Add); |
| 634 |
let mut arg = clap::Arg::new(&full_name) |
| 635 |
.long(&full_name) |
| 636 |
.value_name("USERID") |
| 637 |
.action(action.clone()) |
| 638 |
.help(help); |
| 639 |
if let Some(long_help) = long_help { |
| 640 |
arg = arg.long_help(long_help); |
| 641 |
} |
| 642 |
if all_arg { |
| 643 |
arg = arg.conflicts_with("all"); |
| 644 |
} |
| 645 |
cmd = cmd.arg(arg); |
| 646 |
|
| 647 |
arg_group = arg_group.arg(full_name); |
| 648 |
} |
| 649 |
|
| 650 |
if by_args { |
| 651 |
let full_name = if plain_is_by { |
| 652 |
"email" |
| 653 |
} else { |
| 654 |
"userid-by-email" |
| 655 |
}; |
| 656 |
|
| 657 |
let (help, long_help) = Docs::help(Email, plain_is_by, By); |
| 658 |
|
| 659 |
let mut arg = clap::Arg::new(&full_name) |
| 660 |
.long(&full_name) |
| 661 |
.value_name("EMAIL") |
| 662 |
.value_parser(parse_as_email) |
| 663 |
.action(action.clone()) |
| 664 |
.help(help); |
| 665 |
if let Some(long_help) = long_help { |
| 666 |
arg = arg.long_help(long_help); |
| 667 |
} |
| 668 |
if all_arg { |
| 669 |
arg = arg.conflicts_with("all"); |
| 670 |
} |
| 671 |
cmd = cmd.arg(arg); |
| 672 |
|
| 673 |
arg_group = arg_group.arg(full_name); |
| 674 |
} |
| 675 |
|
| 676 |
// plain_is_by is handled below. This improves the ordering. |
| 677 |
let render_by_name = |mut cmd: clap::Command, |
| 678 |
mut arg_group: clap::ArgGroup| |
| 679 |
{ |
| 680 |
if ENABLE_NAME { |
| 681 |
let full_name = if plain_is_by { |
| 682 |
"name" |
| 683 |
} else { |
| 684 |
"userid-by-name" |
| 685 |
}; |
| 686 |
|
| 687 |
let (help, long_help) = Docs::help(Name, plain_is_by, By); |
| 688 |
|
| 689 |
let mut arg = clap::Arg::new(&full_name) |
| 690 |
.long(&full_name) |
| 691 |
.value_name("DISPLAY_NAME") |
| 692 |
.action(action.clone()) |
| 693 |
.help(help); |
| 694 |
if let Some(long_help) = long_help { |
| 695 |
arg = arg.long_help(long_help); |
| 696 |
} |
| 697 |
if all_arg { |
| 698 |
arg = arg.conflicts_with("all"); |
| 699 |
} |
| 700 |
cmd = cmd.arg(arg); |
| 701 |
|
| 702 |
arg_group = arg_group.arg(full_name); |
| 703 |
} |
| 704 |
|
| 705 |
(cmd, arg_group) |
| 706 |
}; |
| 707 |
if by_args && ! plain_is_by { |
| 708 |
(cmd, arg_group) = render_by_name(cmd, arg_group); |
| 709 |
} |
| 710 |
|
| 711 |
if exact_args { |
| 712 |
let full_name = "email"; |
| 713 |
let (help, long_help) = Docs::help(Email, true, Exact); |
| 714 |
let mut arg = clap::Arg::new(&full_name) |
| 715 |
.long(&full_name) |
| 716 |
.value_name("EMAIL") |
| 717 |
.value_parser(parse_as_email) |
| 718 |
.action(action.clone()) |
| 719 |
.help(help); |
| 720 |
if let Some(long_help) = long_help { |
| 721 |
arg = arg.long_help(long_help); |
| 722 |
} |
| 723 |
if all_arg { |
| 724 |
arg = arg.conflicts_with("all"); |
| 725 |
} |
| 726 |
cmd = cmd.arg(arg); |
| 727 |
|
| 728 |
arg_group = arg_group.arg(full_name); |
| 729 |
} |
| 730 |
|
| 731 |
if add_args { |
| 732 |
let full_name = if plain_is_add { |
| 733 |
"email" |
| 734 |
} else { |
| 735 |
"add-email" |
| 736 |
}; |
| 737 |
let (help, long_help) = Docs::help(Email, plain_is_add, Add); |
| 738 |
let mut arg = clap::Arg::new(&full_name) |
| 739 |
.long(&full_name) |
| 740 |
.value_name("EMAIL") |
| 741 |
.value_parser(parse_as_email) |
| 742 |
.action(action.clone()) |
| 743 |
.help(help); |
| 744 |
if let Some(long_help) = long_help { |
| 745 |
arg = arg.long_help(long_help); |
| 746 |
} |
| 747 |
if all_arg { |
| 748 |
arg = arg.conflicts_with("all"); |
| 749 |
} |
| 750 |
cmd = cmd.arg(arg); |
| 751 |
|
| 752 |
arg_group = arg_group.arg(full_name); |
| 753 |
} |
| 754 |
|
| 755 |
if exact_args { |
| 756 |
if ENABLE_NAME { |
| 757 |
let full_name = "name"; |
| 758 |
let (help, long_help) = Docs::help(Name, true, Exact); |
| 759 |
let mut arg = clap::Arg::new(&full_name) |
| 760 |
.long(&full_name) |
| 761 |
.value_name("DISPLAY_NAME") |
| 762 |
.action(action.clone()) |
| 763 |
.help(help); |
| 764 |
if let Some(long_help) = long_help { |
| 765 |
arg = arg.long_help(long_help); |
| 766 |
} |
| 767 |
if all_arg { |
| 768 |
arg = arg.conflicts_with("all"); |
| 769 |
} |
| 770 |
cmd = cmd.arg(arg); |
| 771 |
|
| 772 |
arg_group = arg_group.arg(full_name); |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
if by_args && plain_is_by { |
| 777 |
(cmd, arg_group) = render_by_name(cmd, arg_group); |
| 778 |
} |
| 779 |
|
| 780 |
if add_args { |
| 781 |
if ENABLE_NAME { |
| 782 |
let full_name = if plain_is_add { |
| 783 |
"name" |
| 784 |
} else { |
| 785 |
"add-name" |
| 786 |
}; |
| 787 |
let (help, long_help) = Docs::help(Name, plain_is_add, Add); |
| 788 |
let mut arg = clap::Arg::new(&full_name) |
| 789 |
.long(&full_name) |
| 790 |
.value_name("DISPLAY_NAME") |
| 791 |
.action(action.clone()) |
| 792 |
.help(help); |
| 793 |
if let Some(long_help) = long_help { |
| 794 |
arg = arg.long_help(long_help); |
| 795 |
} |
| 796 |
if all_arg { |
| 797 |
arg = arg.conflicts_with("all"); |
| 798 |
} |
| 799 |
cmd = cmd.arg(arg); |
| 800 |
|
| 801 |
arg_group = arg_group.arg(full_name); |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
if ! no_linting && add_args { |
| 806 |
let full_name = "allow-non-canonical-userids"; |
| 807 |
cmd = cmd.arg( |
| 808 |
clap::Arg::new(&full_name) |
| 809 |
.long(&full_name) |
| 810 |
.action(clap::ArgAction::SetTrue) |
| 811 |
.help("\ |
| 812 |
Don't reject new user IDs that are not in canonical form") |
| 813 |
.long_help("\ |
| 814 |
Don't reject new user IDs that are not in canonical form |
| 815 |
|
| 816 |
Canonical user IDs are of the form `Name (Comment) \ |
| 817 |
<[email protected]>`.")); |
| 818 |
} |
| 819 |
|
| 820 |
cmd = cmd.group(arg_group); |
| 821 |
|
| 822 |
cmd |
| 823 |
} |
| 824 |
|
| 825 |
fn augment_args_for_update(cmd: clap::Command) -> clap::Command |
| 826 |
{ |
| 827 |
Self::augment_args(cmd) |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
impl<Arguments, Options, Documentation> clap::FromArgMatches |
| 832 |
for UserIDDesignators<Arguments, Options, Documentation> |
| 833 |
where |
| 834 |
Arguments: typenum::Unsigned, |
| 835 |
Options: typenum::Unsigned, |
| 836 |
Documentation: std::fmt::Debug, |
| 837 |
{ |
| 838 |
fn update_from_arg_matches(&mut self, matches: &clap::ArgMatches) |
| 839 |
-> Result<(), clap::Error> |
| 840 |
{ |
| 841 |
// eprintln!("matches: {:#?}", matches); |
| 842 |
|
| 843 |
let arguments = Arguments::to_usize(); |
| 844 |
let all_arg = (arguments & AllUserIDsArg::to_usize()) > 0; |
| 845 |
let exact_args = (arguments & ExactArgs::to_usize()) > 0; |
| 846 |
let by_args = (arguments & ByArgs::to_usize()) > 0; |
| 847 |
let add_args = (arguments & AddArgs::to_usize()) > 0; |
| 848 |
|
| 849 |
let plain_is_by = (arguments & PlainIsBy::to_usize()) > 0; |
| 850 |
let plain_is_add = (arguments & PlainIsAdd::to_usize()) > 0; |
| 851 |
|
| 852 |
// Can't use PlainIsBy or PlainIsAdd with ExactArgs or with |
| 853 |
// each other. |
| 854 |
assert!(! (exact_args && plain_is_by)); |
| 855 |
assert!(! (exact_args && plain_is_add)); |
| 856 |
assert!(! (plain_is_by && plain_is_add)); |
| 857 |
// If plain_is_xxx is set, then by_xxx must be set. |
| 858 |
if plain_is_by { |
| 859 |
assert!(by_args); |
| 860 |
} |
| 861 |
if plain_is_add { |
| 862 |
assert!(add_args); |
| 863 |
} |
| 864 |
|
| 865 |
let options = Options::to_usize(); |
| 866 |
let no_linting = (options & NoLinting::to_usize()) > 0; |
| 867 |
let all_matches_non_self_signed = |
| 868 |
(options & AllMatchesNonSelfSigned::to_usize()) > 0; |
| 869 |
|
| 870 |
let mut designators = Vec::new(); |
| 871 |
|
| 872 |
self.all = if all_arg { |
| 873 |
if matches.get_flag("all") { |
| 874 |
Some(true) |
| 875 |
} else { |
| 876 |
Some(false) |
| 877 |
} |
| 878 |
} else { |
| 879 |
None |
| 880 |
}; |
| 881 |
self.all_matches_non_self_signed = all_matches_non_self_signed; |
| 882 |
|
| 883 |
use UserIDDesignatorSemantics::*; |
| 884 |
if exact_args { |
| 885 |
if let Ok(Some(userids)) = matches.try_get_many::<String>("userid") { |
| 886 |
for userid in userids.cloned() { |
| 887 |
designators.push( |
| 888 |
UserIDDesignator::UserID(Exact, userid)); |
| 889 |
} |
| 890 |
} |
| 891 |
if let Ok(Some(emails)) = matches.try_get_many::<String>("email") { |
| 892 |
for email in emails.cloned() { |
| 893 |
designators.push( |
| 894 |
UserIDDesignator::Email(Exact, email)); |
| 895 |
} |
| 896 |
} |
| 897 |
if let Ok(Some(names)) = matches.try_get_many::<String>("name") { |
| 898 |
for name in names.cloned() { |
| 899 |
designators.push( |
| 900 |
UserIDDesignator::Name(Exact, name)); |
| 901 |
} |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
if by_args { |
| 906 |
if plain_is_by { |
| 907 |
if let Ok(Some(userids)) = matches.try_get_many::<String>("userid") { |
| 908 |
for userid in userids.cloned() { |
| 909 |
designators.push( |
| 910 |
UserIDDesignator::UserID(By, userid)); |
| 911 |
} |
| 912 |
} |
| 913 |
} |
| 914 |
if let Ok(Some(emails)) |
| 915 |
= matches.try_get_many::<String>( |
| 916 |
if plain_is_by { "email" } else { "userid-by-email" }) |
| 917 |
{ |
| 918 |
for email in emails.cloned() { |
| 919 |
designators.push( |
| 920 |
UserIDDesignator::Email(By ,email)); |
| 921 |
} |
| 922 |
} |
| 923 |
if let Ok(Some(names)) |
| 924 |
= matches.try_get_many::<String>( |
| 925 |
if plain_is_by { "name" } else { "userid-by-name" }) |
| 926 |
{ |
| 927 |
for name in names.cloned() { |
| 928 |
designators.push( |
| 929 |
UserIDDesignator::Name(By, name)); |
| 930 |
} |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
if add_args { |
| 935 |
if let Ok(Some(userids)) |
| 936 |
= matches.try_get_many::<String>( |
| 937 |
if plain_is_add { "userid" } else { "add-userid" }) { |
| 938 |
for userid in userids.cloned() { |
| 939 |
designators.push( |
| 940 |
UserIDDesignator::UserID(Add, userid)); |
| 941 |
} |
| 942 |
} |
| 943 |
if let Ok(Some(emails)) |
| 944 |
= matches.try_get_many::<String>( |
| 945 |
if plain_is_add { "email" } else { "add-email" }) |
| 946 |
{ |
| 947 |
for email in emails.cloned() { |
| 948 |
designators.push( |
| 949 |
UserIDDesignator::Email(Add, email)); |
| 950 |
} |
| 951 |
} |
| 952 |
if let Ok(Some(names)) |
| 953 |
= matches.try_get_many::<String>( |
| 954 |
if plain_is_add { "name" } else { "add-name" }) |
| 955 |
{ |
| 956 |
for name in names.cloned() { |
| 957 |
designators.push( |
| 958 |
UserIDDesignator::Name(Add, name)); |
| 959 |
} |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
if no_linting { |
| 964 |
self.allow_non_canonical_userids = true; |
| 965 |
} else if add_args { |
| 966 |
self.allow_non_canonical_userids |
| 967 |
= matches.get_flag("allow-non-canonical-userids"); |
| 968 |
} |
| 969 |
|
| 970 |
self.designators = designators; |
| 971 |
Ok(()) |
| 972 |
} |
| 973 |
|
| 974 |
fn from_arg_matches(matches: &clap::ArgMatches) |
| 975 |
-> Result<Self, clap::Error> |
| 976 |
{ |
| 977 |
let mut designators = Self { |
| 978 |
designators: Vec::new(), |
| 979 |
arguments: std::marker::PhantomData, |
| 980 |
all: None, |
| 981 |
all_matches_non_self_signed: false, |
| 982 |
allow_non_canonical_userids: false, |
| 983 |
}; |
| 984 |
|
| 985 |
// The way we use clap, this is never called. |
| 986 |
designators.update_from_arg_matches(matches)?; |
| 987 |
Ok(designators) |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
#[derive(Debug, Clone)] |
| 992 |
pub struct ResolvedUserID { |
| 993 |
designator: Option<UserIDDesignator>, |
| 994 |
userid: UserID, |
| 995 |
} |
| 996 |
|
| 997 |
impl From<UserID> for ResolvedUserID { |
| 998 |
fn from(userid: UserID) -> Self { |
| 999 |
ResolvedUserID { |
| 1000 |
designator: None, |
| 1001 |
userid, |
| 1002 |
} |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
use crate::cli::types::MyAsRef; |
| 1007 |
impl MyAsRef<UserID> for &ResolvedUserID { |
| 1008 |
fn as_ref(&self) -> &UserID { |
| 1009 |
&self.userid |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
impl ResolvedUserID { |
| 1014 |
/// Return a new implicitly resolved user ID (i.e., it was |
| 1015 |
/// resolved via `--add`). |
| 1016 |
pub fn implicit(userid: UserID) |
| 1017 |
-> Self |
| 1018 |
{ |
| 1019 |
Self { |
| 1020 |
designator: None, |
| 1021 |
userid, |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
/// Return implicitly resolved user IDs for all user IDs |
| 1026 |
/// associated with a certificate. |
| 1027 |
#[allow(dead_code)] |
| 1028 |
pub fn implicit_for_cert(cert: &Cert) -> Vec<Self> { |
| 1029 |
cert.userids() |
| 1030 |
.map(|ua| Self::implicit(ua.userid().clone())) |
| 1031 |
.collect::<Vec<_>>() |
| 1032 |
} |
| 1033 |
|
| 1034 |
/// Return implicitly resolved user IDs for of a certificate's |
| 1035 |
/// self-signed user IDs. |
| 1036 |
#[allow(dead_code)] |
| 1037 |
pub fn implicit_for_valid_cert(vc: &ValidCert) -> Vec<Self> { |
| 1038 |
vc.userids() |
| 1039 |
.map(|ua| Self::implicit(ua.userid().clone())) |
| 1040 |
.collect::<Vec<_>>() |
| 1041 |
} |
| 1042 |
|
| 1043 |
/// The user ID designator. |
| 1044 |
/// |
| 1045 |
/// The designator is what the user provided. If the user ID was |
| 1046 |
/// not explicitly designator (i.e., it was resolved via `--all`), |
| 1047 |
/// this is `None`. |
| 1048 |
#[allow(unused)] |
| 1049 |
pub fn designator(&self) -> Option<&UserIDDesignator> { |
| 1050 |
self.designator.as_ref() |
| 1051 |
} |
| 1052 |
|
| 1053 |
/// The resolved user ID. |
| 1054 |
pub fn userid(&self) -> &UserID { |
| 1055 |
&self.userid |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
#[cfg(test)] |
| 1060 |
mod test { |
| 1061 |
use super::*; |
| 1062 |
|
| 1063 |
// Check that flattening UserIDDesignators works as expected. |
| 1064 |
#[test] |
| 1065 |
fn userid_designators() { |
| 1066 |
use clap::Parser; |
| 1067 |
use clap::CommandFactory; |
| 1068 |
use clap::FromArgMatches; |
| 1069 |
|
| 1070 |
macro_rules! check { |
| 1071 |
($t:ty, |
| 1072 |
$plain:expr, $by:expr, $add:expr) => |
| 1073 |
{{ |
| 1074 |
#[derive(Parser, Debug)] |
| 1075 |
#[clap(name = "prog")] |
| 1076 |
struct CLI { |
| 1077 |
#[command(flatten)] |
| 1078 |
pub userids: UserIDDesignators<$t>, |
| 1079 |
} |
| 1080 |
|
| 1081 |
let command = CLI::command(); |
| 1082 |
|
| 1083 |
let plain: Option<UserIDDesignatorSemantics> = |
| 1084 |
$plain.into(); |
| 1085 |
|
| 1086 |
// Check if --userid is recognized. |
| 1087 |
let m = command.clone().try_get_matches_from(vec![ |
| 1088 |
"prog", "--userid", "alice", "--userid", "bob", |
| 1089 |
]); |
| 1090 |
if let Some(plain) = plain.as_ref() { |
| 1091 |
let m = m.expect("valid arguments"); |
| 1092 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1093 |
assert_eq!(c.userids.designators.len(), 2); |
| 1094 |
assert!(c.userids.designators.iter().all(|d| { |
| 1095 |
d.semantics() == plain |
| 1096 |
})); |
| 1097 |
} else { |
| 1098 |
assert!(m.is_err()); |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
// Check if --email is recognized. |
| 1103 |
let m = command.clone().try_get_matches_from(vec![ |
| 1104 |
"prog", |
| 1105 |
"--email", "[email protected]", |
| 1106 |
"--email", "[email protected]", |
| 1107 |
]); |
| 1108 |
if let Some(plain) = plain.as_ref() { |
| 1109 |
let m = m.expect("valid arguments"); |
| 1110 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1111 |
assert_eq!(c.userids.designators.len(), 2); |
| 1112 |
assert!(c.userids.designators.iter().all(|d| { |
| 1113 |
d.semantics() == plain |
| 1114 |
})); |
| 1115 |
} else { |
| 1116 |
assert!(m.is_err()); |
| 1117 |
} |
| 1118 |
|
| 1119 |
// Check if --name is recognized. |
| 1120 |
let m = command.clone().try_get_matches_from(vec![ |
| 1121 |
"prog", |
| 1122 |
"--name", "alice", |
| 1123 |
"--name", "bob", |
| 1124 |
]); |
| 1125 |
if let (Some(plain), true) = (plain.as_ref(), ENABLE_NAME) { |
| 1126 |
let m = m.expect("valid arguments"); |
| 1127 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1128 |
assert_eq!(c.userids.designators.len(), 2); |
| 1129 |
assert!(c.userids.designators.iter().all(|d| { |
| 1130 |
d.semantics() == plain |
| 1131 |
})); |
| 1132 |
} else { |
| 1133 |
assert!(m.is_err()); |
| 1134 |
} |
| 1135 |
|
| 1136 |
// Check if --userid-by-email is recognized. |
| 1137 |
let m = command.clone().try_get_matches_from(vec![ |
| 1138 |
"prog", |
| 1139 |
"--userid-by-email", "[email protected]", |
| 1140 |
"--userid-by-email", "[email protected]", |
| 1141 |
]); |
| 1142 |
if $by { |
| 1143 |
let m = m.expect("valid arguments"); |
| 1144 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1145 |
assert_eq!(c.userids.designators.len(), 2); |
| 1146 |
assert!(c.userids.designators.iter().all(|d| d.is_by())); |
| 1147 |
} else { |
| 1148 |
assert!(m.is_err()); |
| 1149 |
} |
| 1150 |
|
| 1151 |
// Check if --userid-by-name is recognized. |
| 1152 |
let m = command.clone().try_get_matches_from(vec![ |
| 1153 |
"prog", |
| 1154 |
"--userid-by-name", "alice", |
| 1155 |
"--userid-by-name", "bob", |
| 1156 |
]); |
| 1157 |
if $by && ENABLE_NAME { |
| 1158 |
let m = m.expect("valid arguments"); |
| 1159 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1160 |
assert_eq!(c.userids.designators.len(), 2); |
| 1161 |
assert!(c.userids.designators.iter().all(|d| d.is_by())); |
| 1162 |
} else { |
| 1163 |
assert!(m.is_err()); |
| 1164 |
} |
| 1165 |
|
| 1166 |
// Either --email is unknown, or the --email's value |
| 1167 |
// is invalid. |
| 1168 |
let m = command.clone().try_get_matches_from(vec![ |
| 1169 |
"prog", |
| 1170 |
"--email", "alice@[email protected]", |
| 1171 |
]); |
| 1172 |
assert!(m.is_err()); |
| 1173 |
|
| 1174 |
// Check if --add-userid is recognized. |
| 1175 |
let m = command.clone().try_get_matches_from(vec![ |
| 1176 |
"prog", |
| 1177 |
"--add-userid", "alice", |
| 1178 |
"--add-userid", "bob", |
| 1179 |
]); |
| 1180 |
if $add { |
| 1181 |
let m = m.expect("valid arguments"); |
| 1182 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1183 |
assert_eq!(c.userids.designators.len(), 2); |
| 1184 |
assert!(c.userids.designators.iter().all(|d| d.is_add())); |
| 1185 |
} else { |
| 1186 |
assert!(m.is_err()); |
| 1187 |
} |
| 1188 |
|
| 1189 |
// Check if --add-email is recognized. |
| 1190 |
let m = command.clone().try_get_matches_from(vec![ |
| 1191 |
"prog", |
| 1192 |
"--add-email", "[email protected]", |
| 1193 |
"--add-email", "[email protected]", |
| 1194 |
]); |
| 1195 |
if $add { |
| 1196 |
let m = m.expect("valid arguments"); |
| 1197 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1198 |
assert_eq!(c.userids.designators.len(), 2); |
| 1199 |
assert!(c.userids.designators.iter().all(|d| d.is_add())); |
| 1200 |
} else { |
| 1201 |
assert!(m.is_err()); |
| 1202 |
} |
| 1203 |
|
| 1204 |
// Check if --add-name is recognized. |
| 1205 |
let m = command.clone().try_get_matches_from(vec![ |
| 1206 |
"prog", |
| 1207 |
"--add-name", "alice", |
| 1208 |
"--add-name", "bob", |
| 1209 |
]); |
| 1210 |
if $add && ENABLE_NAME { |
| 1211 |
let m = m.expect("valid arguments"); |
| 1212 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1213 |
assert_eq!(c.userids.designators.len(), 2); |
| 1214 |
assert!(c.userids.designators.iter().all(|d| d.is_add())); |
| 1215 |
} else { |
| 1216 |
assert!(m.is_err()); |
| 1217 |
} |
| 1218 |
}} |
| 1219 |
} |
| 1220 |
|
| 1221 |
use UserIDDesignatorSemantics::*; |
| 1222 |
// plain, by, add |
| 1223 |
check!(typenum::U0, None, false, false); |
| 1224 |
check!(ExactArgs, Exact, false, false); |
| 1225 |
check!(ByArgs, None, true, false); |
| 1226 |
check!(AddArgs, None, false, true); |
| 1227 |
check!(PlainByArgs, By, false, false); |
| 1228 |
check!(PlainAddArgs, Add, false, false); |
| 1229 |
check!(ExactAndAddArgs, Exact, false, true); |
| 1230 |
check!(ExactByAndAddArgs, Exact, true, true); |
| 1231 |
check!(AllExactByAndAddArgs, Exact, true, true); |
| 1232 |
check!(PlainAddAndByArgs, Add, true, false); |
| 1233 |
check!(AllPlainAddAndByArgs, Add, true, false); |
| 1234 |
} |
| 1235 |
|
| 1236 |
#[test] |
| 1237 |
fn userid_designators_one() { |
| 1238 |
use clap::Parser; |
| 1239 |
use clap::CommandFactory; |
| 1240 |
use clap::FromArgMatches; |
| 1241 |
|
| 1242 |
#[derive(Parser, Debug)] |
| 1243 |
#[clap(name = "prog")] |
| 1244 |
struct CLI { |
| 1245 |
#[command(flatten)] |
| 1246 |
pub userids: UserIDDesignators<ExactArgs, OneValue>, |
| 1247 |
} |
| 1248 |
|
| 1249 |
let command = CLI::command(); |
| 1250 |
|
| 1251 |
// Check if --userid is recognized. |
| 1252 |
let m = command.clone().try_get_matches_from(vec![ |
| 1253 |
"prog", |
| 1254 |
"--userid", "userid", |
| 1255 |
]); |
| 1256 |
let m = m.expect("valid arguments"); |
| 1257 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1258 |
assert_eq!(c.userids.designators.len(), 1); |
| 1259 |
|
| 1260 |
// Make sure that we can't give it twice. |
| 1261 |
let m = command.clone().try_get_matches_from(vec![ |
| 1262 |
"prog", |
| 1263 |
"--userid", "alice", |
| 1264 |
"--userid", "bob", |
| 1265 |
]); |
| 1266 |
assert!(m.is_err()); |
| 1267 |
|
| 1268 |
// Make sure that we can't give it zero times. |
| 1269 |
let m = command.clone().try_get_matches_from(vec![ |
| 1270 |
"prog", |
| 1271 |
]); |
| 1272 |
assert!(m.is_err()); |
| 1273 |
|
| 1274 |
// Mixing is also not allowed. |
| 1275 |
let m = command.clone().try_get_matches_from(vec![ |
| 1276 |
"prog", |
| 1277 |
"--userid", "carol", |
| 1278 |
"--email", "[email protected]", |
| 1279 |
]); |
| 1280 |
assert!(m.is_err()); |
| 1281 |
} |
| 1282 |
|
| 1283 |
#[test] |
| 1284 |
fn userid_designators_optional() { |
| 1285 |
use clap::Parser; |
| 1286 |
use clap::CommandFactory; |
| 1287 |
use clap::FromArgMatches; |
| 1288 |
|
| 1289 |
#[derive(Parser, Debug)] |
| 1290 |
#[clap(name = "prog")] |
| 1291 |
struct CLI { |
| 1292 |
#[command(flatten)] |
| 1293 |
pub userids: UserIDDesignators<ExactArgs, |
| 1294 |
OptionalValue>, |
| 1295 |
} |
| 1296 |
|
| 1297 |
let command = CLI::command(); |
| 1298 |
|
| 1299 |
// Check if --userid is recognized. |
| 1300 |
let m = command.clone().try_get_matches_from(vec![ |
| 1301 |
"prog", |
| 1302 |
"--userid", "alice", |
| 1303 |
]); |
| 1304 |
let m = m.expect("valid arguments"); |
| 1305 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1306 |
assert_eq!(c.userids.designators.len(), 1); |
| 1307 |
|
| 1308 |
// Make sure that we can give it twice. |
| 1309 |
let m = command.clone().try_get_matches_from(vec![ |
| 1310 |
"prog", |
| 1311 |
"--userid", "alice", |
| 1312 |
"--userid", "bob", |
| 1313 |
]); |
| 1314 |
let m = m.expect("valid arguments"); |
| 1315 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1316 |
assert_eq!(c.userids.designators.len(), 2); |
| 1317 |
|
| 1318 |
// Make sure that we can give it zero times. |
| 1319 |
let m = command.clone().try_get_matches_from(vec![ |
| 1320 |
"prog", |
| 1321 |
]); |
| 1322 |
let m = m.expect("valid arguments"); |
| 1323 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1324 |
assert_eq!(c.userids.designators.len(), 0); |
| 1325 |
|
| 1326 |
// Make sure mixing is allowed. |
| 1327 |
let m = command.clone().try_get_matches_from(vec![ |
| 1328 |
"prog", |
| 1329 |
"--userid", "alice", |
| 1330 |
"--email", "[email protected]", |
| 1331 |
]); |
| 1332 |
let m = m.expect("valid arguments"); |
| 1333 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1334 |
assert_eq!(c.userids.designators.len(), 2); |
| 1335 |
} |
| 1336 |
|
| 1337 |
#[test] |
| 1338 |
fn userid_designators_all() { |
| 1339 |
use clap::Parser; |
| 1340 |
use clap::CommandFactory; |
| 1341 |
use clap::FromArgMatches; |
| 1342 |
|
| 1343 |
#[derive(Parser, Debug)] |
| 1344 |
#[clap(name = "prog")] |
| 1345 |
struct CLI { |
| 1346 |
#[command(flatten)] |
| 1347 |
pub userids: UserIDDesignators<AllExactByAndAddArgs>, |
| 1348 |
} |
| 1349 |
|
| 1350 |
let command = CLI::command(); |
| 1351 |
|
| 1352 |
// Sanity check. |
| 1353 |
let m = command.clone().try_get_matches_from(vec![ |
| 1354 |
"prog", |
| 1355 |
"--userid", "alice", |
| 1356 |
]); |
| 1357 |
let m = m.expect("valid arguments"); |
| 1358 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1359 |
assert_eq!(c.userids.designators.len(), 1); |
| 1360 |
assert_eq!(c.userids.all(), Some(false)); |
| 1361 |
|
| 1362 |
// Make sure --all by itself is accepted. |
| 1363 |
let m = command.clone().try_get_matches_from(vec![ |
| 1364 |
"prog", |
| 1365 |
"--all" |
| 1366 |
]); |
| 1367 |
let m = m.expect("valid arguments"); |
| 1368 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1369 |
assert_eq!(c.userids.designators.len(), 0); |
| 1370 |
assert_eq!(c.userids.all(), Some(true)); |
| 1371 |
|
| 1372 |
// Can't combine --all with any other designator. |
| 1373 |
for (arg, value) in &[ |
| 1374 |
("--userid", "foo"), |
| 1375 |
("--userid-by-email", "[email protected]"), |
| 1376 |
("--userid-by-name", "foo"), |
| 1377 |
("--add-userid", "foo"), |
| 1378 |
("--email", "[email protected]"), |
| 1379 |
("--add-email", "[email protected]"), |
| 1380 |
("--name", "foo"), |
| 1381 |
("--add-name", "foo"), |
| 1382 |
] |
| 1383 |
{ |
| 1384 |
if ! ENABLE_NAME && arg.contains("-name") { |
| 1385 |
continue; |
| 1386 |
} |
| 1387 |
|
| 1388 |
// Make sure the arg/value are recognized. |
| 1389 |
eprintln!("Testing {} {}", arg, value); |
| 1390 |
let m = command.clone().try_get_matches_from(vec![ |
| 1391 |
"prog", |
| 1392 |
arg, value, |
| 1393 |
]); |
| 1394 |
let m = m.expect("valid arguments"); |
| 1395 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1396 |
assert_eq!(c.userids.designators.len(), 1); |
| 1397 |
assert_eq!(c.userids.all(), Some(false)); |
| 1398 |
|
| 1399 |
// Make sure adding --all causes it to fail. |
| 1400 |
eprintln!("Testing {} {} --all", arg, value); |
| 1401 |
let m = command.clone().try_get_matches_from(vec![ |
| 1402 |
"prog", |
| 1403 |
arg, value, |
| 1404 |
"--all", |
| 1405 |
]); |
| 1406 |
assert!(m.is_err()); |
| 1407 |
} |
| 1408 |
} |
| 1409 |
} |