summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2020-01-17 17:35:53 +0100
committerJustus Winter <justus@sequoia-pgp.org>2020-01-18 14:26:31 +0100
commitebb6aadc1a1179968dbc4d9f5f2807024192d1bd (patch)
treefcdfcb334d1f38de27db95e9cfdff5459dd6d9e2
parent768ff8473433de264ed8e9e0cb7b246ac666d182 (diff)
openpgp: Add KeyIter::primary.
- See #414.
-rw-r--r--openpgp/src/cert/keyiter.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/openpgp/src/cert/keyiter.rs b/openpgp/src/cert/keyiter.rs
index 29ce5485..60071d46 100644
--- a/openpgp/src/cert/keyiter.rs
+++ b/openpgp/src/cert/keyiter.rs
@@ -16,6 +16,8 @@ use crate::{
KeyBindingIter,
KeyAmalgamation,
},
+ Error,
+ Result,
};
/// An iterator over all `Key`s (both the primary key and the subkeys)
@@ -287,6 +289,28 @@ impl<'a, P: 'a + key::KeyParts, R: 'a + key::KeyRole> KeyIter<'a, P, R>
}
}
+ /// Returns the amalgamated primary key at time `time`
+ ///
+ /// If `time` is None, then the current time is used.
+ ///
+ /// See `ValidKeyIter` for the definition of a valid key.
+ pub fn primary<T>(self, time: T) -> Result<KeyAmalgamation<'a, P>>
+ where T: Into<Option<SystemTime>>,
+ &'a KeyBinding<P, key::PrimaryRole>:
+ From<&'a KeyBinding<key::PublicParts, key::PrimaryRole>>
+ {
+ if let Some(cert) = self.cert.as_ref() {
+ let time = time.into().unwrap_or_else(std::time::SystemTime::now);
+ let primary: &KeyBinding<P, key::PrimaryRole> =
+ (&cert.primary).into();
+ let ka: KeyAmalgamation<'a, P> = (*cert, primary, time).into();
+ ka.alive()?;
+ Ok(ka)
+ } else {
+ Err(Error::InvalidOperation("empty iterator".into()).into())
+ }
+ }
+
/// Changes the iterator to return key components.
///
/// A key component is similar to a key amalgamation, but is not