From 86e2cb5263a23fe78a49163f0e798efc349ca89e Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Mon, 26 Aug 2019 14:48:12 +0200 Subject: openpgp: Implement FromStr for some types. - This implements std::str::FromStr for types that have string-representations and are reasonably likely to be encountered by downstream users, e.g. fingerprints or messages. This allows us to do `"xxx".parse()?`. - Fixes #320. --- openpgp/src/crypto/keygrip.rs | 8 ++++++++ openpgp/src/fingerprint.rs | 8 ++++++++ openpgp/src/keyid.rs | 8 ++++++++ openpgp/src/message/mod.rs | 8 ++++++++ openpgp/src/packet_pile.rs | 8 ++++++++ openpgp/src/tpk/mod.rs | 8 ++++++++ 6 files changed, 48 insertions(+) (limited to 'openpgp') diff --git a/openpgp/src/crypto/keygrip.rs b/openpgp/src/crypto/keygrip.rs index 9bf4bf5b..66240b92 100644 --- a/openpgp/src/crypto/keygrip.rs +++ b/openpgp/src/crypto/keygrip.rs @@ -29,6 +29,14 @@ impl fmt::Display for Keygrip { } } +impl std::str::FromStr for Keygrip { + type Err = failure::Error; + + fn from_str(s: &str) -> std::result::Result { + Self::from_hex(s) + } +} + impl Keygrip { /// Parses a keygrip. pub fn from_hex(hex: &str) -> Result { diff --git a/openpgp/src/fingerprint.rs b/openpgp/src/fingerprint.rs index 9c4a57cf..a12ec118 100644 --- a/openpgp/src/fingerprint.rs +++ b/openpgp/src/fingerprint.rs @@ -18,6 +18,14 @@ impl fmt::Debug for Fingerprint { } } +impl std::str::FromStr for Fingerprint { + type Err = failure::Error; + + fn from_str(s: &str) -> std::result::Result { + Self::from_hex(s) + } +} + impl Fingerprint { /// Reads a binary fingerprint. pub fn from_bytes(raw: &[u8]) -> Fingerprint { diff --git a/openpgp/src/keyid.rs b/openpgp/src/keyid.rs index 799bf4fd..2e77c962 100644 --- a/openpgp/src/keyid.rs +++ b/openpgp/src/keyid.rs @@ -20,6 +20,14 @@ impl fmt::Debug for KeyID { } } +impl std::str::FromStr for KeyID { + type Err = failure::Error; + + fn from_str(s: &str) -> std::result::Result { + Self::from_hex(s) + } +} + impl From for Vec { fn from(id: KeyID) -> Self { let mut r = Vec::with_capacity(8); diff --git a/openpgp/src/message/mod.rs b/openpgp/src/message/mod.rs index d36ddb3c..30f8268d 100644 --- a/openpgp/src/message/mod.rs +++ b/openpgp/src/message/mod.rs @@ -338,6 +338,14 @@ impl<'a> Parse<'a, Message> for Message { } } +impl std::str::FromStr for Message { + type Err = failure::Error; + + fn from_str(s: &str) -> std::result::Result { + Self::from_bytes(s.as_bytes()) + } +} + impl Message { /// Converts the `PacketPile` to a `Message`. /// diff --git a/openpgp/src/packet_pile.rs b/openpgp/src/packet_pile.rs index e14a541d..275c0dba 100644 --- a/openpgp/src/packet_pile.rs +++ b/openpgp/src/packet_pile.rs @@ -64,6 +64,14 @@ impl<'a> Parse<'a, PacketPile> for PacketPile { } } +impl std::str::FromStr for PacketPile { + type Err = failure::Error; + + fn from_str(s: &str) -> std::result::Result { + Self::from_bytes(s.as_bytes()) + } +} + impl From> for PacketPile { fn from(p: Vec) -> Self { PacketPile { top_level: Container { packets: p } } diff --git a/openpgp/src/tpk/mod.rs b/openpgp/src/tpk/mod.rs index fb1ae43e..29f59da2 100644 --- a/openpgp/src/tpk/mod.rs +++ b/openpgp/src/tpk/mod.rs @@ -368,6 +368,14 @@ impl<'a> Parse<'a, TPKParser<'a, vec::IntoIter>> } } +impl std::str::FromStr for TPK { + type Err = failure::Error; + + fn from_str(s: &str) -> std::result::Result { + Self::from_bytes(s.as_bytes()) + } +} + impl<'a, I: Iterator> TPKParser<'a, I> { /// Initializes a TPKParser from an iterator over Packets. pub fn from_iter(iter: I) -> Self { -- cgit v1.2.3