summaryrefslogtreecommitdiffstats
path: root/openpgp/src/keyhandle.rs
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-11-27 12:55:34 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-11-27 13:01:31 +0100
commitc64eb5733fa217f10e51f24dd1d6614703f0d828 (patch)
tree106a873a4e80161093cbd2f2a04aa25f9c4a6ddc /openpgp/src/keyhandle.rs
parentf5f8a7b1b47d29c16083ba9d3dc5f88441376ea7 (diff)
openpgp: Rename ID to KeyHandle.
Diffstat (limited to 'openpgp/src/keyhandle.rs')
-rw-r--r--openpgp/src/keyhandle.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/openpgp/src/keyhandle.rs b/openpgp/src/keyhandle.rs
new file mode 100644
index 00000000..a9433f84
--- /dev/null
+++ b/openpgp/src/keyhandle.rs
@@ -0,0 +1,60 @@
+use std::convert::TryFrom;
+
+use crate::{
+ Error,
+ Fingerprint,
+ KeyID,
+ Result,
+};
+
+/// Identifies OpenPGP keys.
+///
+/// An `KeyHandle` is either a `Fingerprint` or a `KeyID`.
+#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
+pub enum KeyHandle {
+ /// A Fingerprint.
+ Fingerprint(Fingerprint),
+ /// A KeyID.
+ KeyID(KeyID),
+}
+
+impl std::fmt::Display for KeyHandle {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ match self {
+ KeyHandle::Fingerprint(v) => v.fmt(f),
+ KeyHandle::KeyID(v) => v.fmt(f),
+ }
+ }
+}
+
+impl From<KeyID> for KeyHandle {
+ fn from(i: KeyID) -> Self {
+ KeyHandle::KeyID(i)
+ }
+}
+
+impl From<KeyHandle> for KeyID {
+ fn from(i: KeyHandle) -> Self {
+ match i {
+ KeyHandle::Fingerprint(i) => i.into(),
+ KeyHandle::KeyID(i) => i,
+ }
+ }
+}
+
+impl From<Fingerprint> for KeyHandle {
+ fn from(i: Fingerprint) -> Self {
+ KeyHandle::Fingerprint(i)
+ }
+}
+
+impl TryFrom<KeyHandle> for Fingerprint {
+ type Error = failure::Error;
+ fn try_from(i: KeyHandle) -> Result<Self> {
+ match i {
+ KeyHandle::Fingerprint(i) => Ok(i),
+ KeyHandle::KeyID(i) => Err(Error::InvalidOperation(
+ format!("Cannot convert keyid {} to fingerprint", i)).into()),
+ }
+ }
+}