summaryrefslogtreecommitdiffstats
path: root/openpgp
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-08-26 14:48:12 +0200
committerJustus Winter <justus@sequoia-pgp.org>2019-08-26 16:00:49 +0200
commit86e2cb5263a23fe78a49163f0e798efc349ca89e (patch)
tree070167129b99a3b7f5642ccbe84ee36b7a7d31d2 /openpgp
parent86806951896d44a40b30ad4b10d2ea87c6140e45 (diff)
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.
Diffstat (limited to 'openpgp')
-rw-r--r--openpgp/src/crypto/keygrip.rs8
-rw-r--r--openpgp/src/fingerprint.rs8
-rw-r--r--openpgp/src/keyid.rs8
-rw-r--r--openpgp/src/message/mod.rs8
-rw-r--r--openpgp/src/packet_pile.rs8
-rw-r--r--openpgp/src/tpk/mod.rs8
6 files changed, 48 insertions, 0 deletions
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, Self::Err> {
+ Self::from_hex(s)
+ }
+}
+
impl Keygrip {
/// Parses a keygrip.
pub fn from_hex(hex: &str) -> Result<Self> {
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, Self::Err> {
+ 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, Self::Err> {
+ Self::from_hex(s)
+ }
+}
+
impl From<KeyID> for Vec<u8> {
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, Self::Err> {
+ 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, Self::Err> {
+ Self::from_bytes(s.as_bytes())
+ }
+}
+
impl From<Vec<Packet>> for PacketPile {
fn from(p: Vec<Packet>) -> 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<Packet>>>
}
}
+impl std::str::FromStr for TPK {
+ type Err = failure::Error;
+
+ fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
+ Self::from_bytes(s.as_bytes())
+ }
+}
+
impl<'a, I: Iterator<Item=Packet>> TPKParser<'a, I> {
/// Initializes a TPKParser from an iterator over Packets.
pub fn from_iter(iter: I) -> Self {