From de0a0860ed68a75e5b143e13698a2ce2fc9cce21 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 21 Feb 2019 23:09:07 +0100 Subject: [PATCH] Drop 0x prefixes from KeyID.to_string(). - Likewise for Fingerprint. - Make the .from_str() implementations cope with non-prefixed strings. --- src/types.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/types.rs b/src/types.rs index ea16856..953aa0c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -57,7 +57,7 @@ impl TryFrom for Fingerprint { impl ToString for Fingerprint { fn to_string(&self) -> String { - format!("0x{}", hex::encode(&self.0[..])) + hex::encode(&self.0[..]) } } @@ -86,12 +86,16 @@ impl<'de> Deserialize<'de> for Fingerprint { impl FromStr for Fingerprint { type Err = Error; - fn from_str(s: &str) -> Result { - if !s.starts_with("0x") || s.len() != 40 + 2 { + fn from_str(mut s: &str) -> Result { + if s.starts_with("0x") { + s = &s[2..]; + } + + if s.len() != 40 { return Err(format!("'{}' is not a valid fingerprint", s).into()); } - let vec = hex::decode(&s[2..])?; + let vec = hex::decode(s)?; if vec.len() == 20 { let mut arr = [0u8; 20]; @@ -130,19 +134,23 @@ impl From for KeyID { impl ToString for KeyID { fn to_string(&self) -> String { - format!("0x{}", hex::encode(&self.0[..])) + hex::encode(&self.0[..]) } } impl FromStr for KeyID { type Err = Error; - fn from_str(s: &str) -> Result { - if !s.starts_with("0x") || s.len() != 16 + 2 { + fn from_str(mut s: &str) -> Result { + if s.starts_with("0x") { + s = &s[2..]; + } + + if s.len() != 16 { return Err(format!("'{}' is not a valid long key ID", s).into()); } - let vec = hex::decode(&s[2..])?; + let vec = hex::decode(s)?; if vec.len() == 8 { let mut arr = [0u8; 8];