Drop 0x prefixes from KeyID.to_string().

- Likewise for Fingerprint.

  - Make the .from_str() implementations cope with non-prefixed
    strings.
This commit is contained in:
Justus Winter 2019-02-21 23:09:07 +01:00
parent b3fa38f069
commit de0a0860ed
No known key found for this signature in database
GPG Key ID: 686F55B4AB2B3386
1 changed files with 16 additions and 8 deletions

View File

@ -57,7 +57,7 @@ impl TryFrom<sequoia_openpgp::Fingerprint> 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<Fingerprint> {
if !s.starts_with("0x") || s.len() != 40 + 2 {
fn from_str(mut s: &str) -> Result<Fingerprint> {
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<Fingerprint> 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<KeyID> {
if !s.starts_with("0x") || s.len() != 16 + 2 {
fn from_str(mut s: &str) -> Result<KeyID> {
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];