diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-11-26 14:48:57 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-11-26 14:48:57 +0100 |
commit | f5f8a7b1b47d29c16083ba9d3dc5f88441376ea7 (patch) | |
tree | d5925f3513f4840da31057ad5c1ad78725d44cee /openpgp | |
parent | b24d1c5a099e2c741cf724816d49b8dd7977e049 (diff) |
openpgp: New type ID.
- ID is either a KeyID or a Fingerprint.
- Fixes #282.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/id.rs | 60 | ||||
-rw-r--r-- | openpgp/src/lib.rs | 2 |
2 files changed, 62 insertions, 0 deletions
diff --git a/openpgp/src/id.rs b/openpgp/src/id.rs new file mode 100644 index 00000000..eafc3b20 --- /dev/null +++ b/openpgp/src/id.rs @@ -0,0 +1,60 @@ +use std::convert::TryFrom; + +use crate::{ + Error, + Fingerprint, + KeyID, + Result, +}; + +/// Identifies OpenPGP keys. +/// +/// An `ID` is either a `Fingerprint` or a `KeyID`. +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)] +pub enum ID { + /// A Fingerprint. + Fingerprint(Fingerprint), + /// A KeyID. + KeyID(KeyID), +} + +impl std::fmt::Display for ID { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + ID::Fingerprint(v) => v.fmt(f), + ID::KeyID(v) => v.fmt(f), + } + } +} + +impl From<KeyID> for ID { + fn from(i: KeyID) -> Self { + ID::KeyID(i) + } +} + +impl From<ID> for KeyID { + fn from(i: ID) -> Self { + match i { + ID::Fingerprint(i) => i.into(), + ID::KeyID(i) => i, + } + } +} + +impl From<Fingerprint> for ID { + fn from(i: Fingerprint) -> Self { + ID::Fingerprint(i) + } +} + +impl TryFrom<ID> for Fingerprint { + type Error = failure::Error; + fn try_from(i: ID) -> Result<Self> { + match i { + ID::Fingerprint(i) => Ok(i), + ID::KeyID(i) => Err(Error::InvalidOperation( + format!("Cannot convert keyid {} to fingerprint", i)).into()), + } + } +} diff --git a/openpgp/src/lib.rs b/openpgp/src/lib.rs index 603d0744..058af071 100644 --- a/openpgp/src/lib.rs +++ b/openpgp/src/lib.rs @@ -145,6 +145,8 @@ use crate::types::{ mod fingerprint; mod keyid; +mod id; +pub use id::ID; #[cfg(test)] mod tests; |