diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-11-25 11:29:37 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-11-25 11:35:06 +0100 |
commit | 9c6174635ac40a32b273bf78493c1d6db3cc2d34 (patch) | |
tree | 6cb90209caa44931abe6061144af3cfbd61711e5 /openpgp/src/types/server_preferences.rs | |
parent | 6b3574906ed2cd647a6d4f8dcb3243e3dd0d5616 (diff) |
openpgp: Rename openpgp::constants to openpgp::types.
- Fixes #381.
Diffstat (limited to 'openpgp/src/types/server_preferences.rs')
-rw-r--r-- | openpgp/src/types/server_preferences.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/openpgp/src/types/server_preferences.rs b/openpgp/src/types/server_preferences.rs new file mode 100644 index 00000000..5567f4a9 --- /dev/null +++ b/openpgp/src/types/server_preferences.rs @@ -0,0 +1,74 @@ +use std::fmt; + +/// Describes preferences regarding key servers. +#[derive(Clone, PartialEq, Eq)] +pub struct KeyServerPreferences{ + no_modify: bool, + unknown: Box<[u8]>, +} + +impl Default for KeyServerPreferences { + fn default() -> Self { + KeyServerPreferences::new(&[0]) + } +} + +impl fmt::Debug for KeyServerPreferences { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.no_modify() { + f.write_str("no modify")?; + } + + Ok(()) + } +} + +impl KeyServerPreferences { + /// Creates a new instance from `bits`. + pub fn new(bits: &[u8]) -> Self { + let no_mod = bits.get(0) + .map(|x| x & KEYSERVER_PREFERENCE_NO_MODIFY != 0).unwrap_or(false); + let unk = if bits.is_empty() { + Box::default() + } else { + let mut cpy = Vec::from(bits); + + cpy[0] &= KEYSERVER_PREFERENCE_NO_MODIFY ^ 0xff; + + while cpy.last().cloned() == Some(0) { cpy.pop(); } + cpy.into_boxed_slice() + }; + + KeyServerPreferences{ + no_modify: no_mod, unknown: unk + } + } + + /// Returns a slice referencing the raw values. + pub(crate) fn as_vec(&self) -> Vec<u8> { + let mut ret = if self.unknown.is_empty() { + vec![0] + } else { + self.unknown.clone().into() + }; + + if self.no_modify { ret[0] |= KEYSERVER_PREFERENCE_NO_MODIFY; } + + ret + } + + /// Whether or not keyservers are allowed to modify this key. + pub fn no_modify(&self) -> bool { + !self.no_modify + } + + /// Sets whether or not keyservers are allowed to modify this key. + pub fn set_no_modify(mut self, v: bool) -> Self { + self.no_modify = v; + self + } +} + +/// The private component of this key may be in the possession of more +/// than one person. +const KEYSERVER_PREFERENCE_NO_MODIFY: u8 = 0x80; |