diff options
author | Neal H. Walfield <neal@pep.foundation> | 2019-08-30 16:46:22 +0200 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2019-09-02 13:31:54 +0200 |
commit | aa33bd8278cd7f7658bcd60ceeb38ca3d1bd5d7d (patch) | |
tree | 81c4b97ee82d3324e49b636fbdf843ae79f40b22 /openpgp | |
parent | 867486afcd952ea0b240a7c146d915418d0764bd (diff) |
openpgp: Wrap Vec<ComponentBinding<C>>.
- This is needed to be able to impl methods on a
Vec<ComponentBinding<C>>.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/tpk/mod.rs | 83 | ||||
-rw-r--r-- | openpgp/src/tpk/parser/low_level/grammar.lalrpop | 9 |
2 files changed, 81 insertions, 11 deletions
diff --git a/openpgp/src/tpk/mod.rs b/openpgp/src/tpk/mod.rs index fe33fee0..2f1d2906 100644 --- a/openpgp/src/tpk/mod.rs +++ b/openpgp/src/tpk/mod.rs @@ -6,6 +6,8 @@ use std::path::Path; use std::slice; use std::mem; use std::fmt; +use std::ops::{Deref, DerefMut}; + use time; use crate::{ @@ -357,6 +359,73 @@ impl<'a, C> ExactSizeIterator for ComponentBindingIter<'a, C> } } +/// A collection of `ComponentBindings`. +/// +/// Note: we need this, because we can't `impl Vec<ComponentBindings>`. +#[derive(Debug, Clone, PartialEq)] +pub struct ComponentBindings<C> { + bindings: Vec<ComponentBinding<C>>, +} + +impl<C> Deref for ComponentBindings<C> { + type Target = Vec<ComponentBinding<C>>; + + fn deref(&self) -> &Self::Target { + &self.bindings + } +} + +impl<C> DerefMut for ComponentBindings<C> { + fn deref_mut(&mut self) -> &mut Vec<ComponentBinding<C>> { + &mut self.bindings + } +} + +impl<C> Into<Vec<ComponentBinding<C>>> for ComponentBindings<C> { + fn into(self) -> Vec<ComponentBinding<C>> { + self.bindings + } +} + +impl<C> IntoIterator for ComponentBindings<C> { + type Item = ComponentBinding<C>; + type IntoIter = std::vec::IntoIter<Self::Item>; + + fn into_iter(self) -> Self::IntoIter { + self.bindings.into_iter() + } +} + +impl<C> ComponentBindings<C> { + fn new() -> Self { + Self { bindings: vec![] } + } +} + +/// A vecor of key (primary or subkey, public or private) and any +/// associated signatures. +pub type KeyBindings<KeyPart, KeyRole> = ComponentBindings<Key<KeyPart, KeyRole>>; + +/// A vector of subkeys and any associated signatures. +pub type SubkeyBindings<KeyPart> = KeyBindings<KeyPart, key::SubordinateRole>; + +/// A vector of key (primary or subkey, public or private) and any +/// associated signatures. +pub type GenericKeyBindings + = ComponentBindings<Key<key::UnspecifiedParts, key::UnspecifiedRole>>; + +/// A vector of User ID bindings and any associated signatures. +pub type UserIDBindings = ComponentBindings<UserID>; + +/// A vector of User Attribute bindings and any associated signatures. +pub type UserAttributeBindings = ComponentBindings<UserAttribute>; + +/// A vector of unknown components and any associated signatures. +/// +/// Note: all signatures are stored as certifications. +pub type UnknownBindings = ComponentBindings<Unknown>; + + /// A transferable public key (TPK). /// /// A TPK (see [RFC 4880, section 11.1]) can be used to verify @@ -413,13 +482,13 @@ impl<'a, C> ExactSizeIterator for ComponentBindingIter<'a, C> pub struct TPK { primary: PrimaryKeyBinding<key::PublicParts>, - userids: Vec<UserIDBinding>, - user_attributes: Vec<UserAttributeBinding>, - subkeys: Vec<SubkeyBinding<key::PublicParts>>, + userids: UserIDBindings, + user_attributes: UserAttributeBindings, + subkeys: SubkeyBindings<key::PublicParts>, // Unknown components, e.g., some UserAttribute++ packet from the // future. - unknowns: Vec<UnknownBinding>, + unknowns: UnknownBindings, // Signatures that we couldn't find a place for. bad: Vec<packet::Signature>, } @@ -1027,13 +1096,13 @@ impl TPK { self.bad.sort_by(sig_cmp); self.bad.dedup(); - for binding in &mut self.userids { + for binding in &mut self.userids.iter_mut() { binding.sort_and_dedup(); } - for binding in &mut self.user_attributes { + for binding in &mut self.user_attributes.iter_mut() { binding.sort_and_dedup(); } - for binding in &mut self.subkeys { + for binding in &mut self.subkeys.iter_mut() { binding.sort_and_dedup(); } diff --git a/openpgp/src/tpk/parser/low_level/grammar.lalrpop b/openpgp/src/tpk/parser/low_level/grammar.lalrpop index 3a73a688..88937b09 100644 --- a/openpgp/src/tpk/parser/low_level/grammar.lalrpop +++ b/openpgp/src/tpk/parser/low_level/grammar.lalrpop @@ -12,6 +12,7 @@ use crate::TPK; use crate::tpk::parser::low_level::lexer; use crate::tpk::parser::low_level::lexer::{Token, Component}; use crate::tpk::{ + ComponentBindings, PrimaryKeyBinding, SubkeyBinding, UserIDBinding, @@ -52,10 +53,10 @@ pub TPK: Option<TPK> = { self_revocations: vec![], other_revocations: vec![], }, - subkeys: vec![], - userids: vec![], - user_attributes: vec![], - unknowns: vec![], + subkeys: ComponentBindings::new(), + userids: ComponentBindings::new(), + user_attributes: ComponentBindings::new(), + unknowns: ComponentBindings::new(), bad: vec![], }; |