| 1 |
//! Common argument for signature notations. |
| 2 |
|
| 3 |
use anyhow::Result; |
| 4 |
|
| 5 |
use sequoia_openpgp::packet::signature::subpacket::{ |
| 6 |
NotationData, |
| 7 |
NotationDataFlags, |
| 8 |
}; |
| 9 |
|
| 10 |
#[derive(Debug, clap::Args)] |
| 11 |
pub struct SignatureNotationsArg { |
| 12 |
#[clap( |
| 13 |
long = "signature-notation", |
| 14 |
value_names = &["NAME", "VALUE"], |
| 15 |
number_of_values = 2, |
| 16 |
help = "Add a notation to the signature", |
| 17 |
long_help = "Add a notation to the signature |
| 18 |
|
| 19 |
A user-defined notation's name must be of the form \ |
| 20 |
`[email protected]`. If the notation's name starts \ |
| 21 |
with a `!`, then the notation is marked as being critical. If a \ |
| 22 |
consumer of a signature doesn't understand a critical notation, \ |
| 23 |
then it will ignore the signature. The notation is marked as \ |
| 24 |
being human readable.", |
| 25 |
)] |
| 26 |
pub signature_notations: Vec<String>, |
| 27 |
} |
| 28 |
|
| 29 |
impl SignatureNotationsArg { |
| 30 |
/// Parses the notations. |
| 31 |
pub fn parse(&self) -> Result<Vec<(bool, NotationData)>> { |
| 32 |
let n = &self.signature_notations; |
| 33 |
assert_eq!(n.len() % 2, 0, "notations must be pairs of key and value"); |
| 34 |
|
| 35 |
// Each --notation takes two values. Iterate over them in chunks of 2. |
| 36 |
let notations: Vec<(bool, NotationData)> = n |
| 37 |
.chunks(2) |
| 38 |
.map(|arg_pair| { |
| 39 |
let name = &arg_pair[0]; |
| 40 |
let value = &arg_pair[1]; |
| 41 |
|
| 42 |
let (critical, name) = match name.strip_prefix('!') { |
| 43 |
Some(name) => (true, name), |
| 44 |
None => (false, name.as_str()), |
| 45 |
}; |
| 46 |
|
| 47 |
let notation_data = NotationData::new( |
| 48 |
name, |
| 49 |
value, |
| 50 |
NotationDataFlags::empty().set_human_readable(), |
| 51 |
); |
| 52 |
(critical, notation_data) |
| 53 |
}) |
| 54 |
.collect(); |
| 55 |
|
| 56 |
Ok(notations) |
| 57 |
} |
| 58 |
} |