aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-06-01 00:02:27 +0200
committerAlex Auvolat <alex@adnab.me>2022-06-01 00:02:27 +0200
commitdd62efa24c66eaec87b0cbb99fc48d6bf8441801 (patch)
treee2f210f5a916328d5f410ffe3223d4fd6d36b983
parent553a15a82a700792986b23cb89e2a8ec070cc27d (diff)
downloadaerogramme-dd62efa24c66eaec87b0cbb99fc48d6bf8441801.tar.gz
aerogramme-dd62efa24c66eaec87b0cbb99fc48d6bf8441801.zip
Implement FromStr for MailUuid
-rw-r--r--src/mail_uuid.rs27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/mail_uuid.rs b/src/mail_uuid.rs
index b784e78..647238f 100644
--- a/src/mail_uuid.rs
+++ b/src/mail_uuid.rs
@@ -1,4 +1,5 @@
use std::sync::atomic::{AtomicU64, Ordering};
+use std::str::FromStr;
use lazy_static::lazy_static;
use rand::prelude::*;
@@ -54,15 +55,7 @@ impl<'de> Deserialize<'de> for MailUuid {
D: Deserializer<'de>,
{
let v = String::deserialize(d)?;
- let bytes = hex::decode(v).map_err(|_| D::Error::custom("invalid hex"))?;
-
- if bytes.len() != 24 {
- return Err(D::Error::custom("bad length"));
- }
-
- let mut tmp = [0u8; 24];
- tmp[..].copy_from_slice(&bytes);
- Ok(Self(tmp))
+ MailUuid::from_str(&v).map_err(D::Error::custom)
}
}
@@ -80,3 +73,19 @@ impl ToString for MailUuid {
hex::encode(self.0)
}
}
+
+impl FromStr for MailUuid {
+ type Err = &'static str;
+
+ fn from_str(s: &str) -> Result<MailUuid, &'static str> {
+ let bytes = hex::decode(s).map_err(|_| "invalid hex")?;
+
+ if bytes.len() != 24 {
+ return Err("bad length");
+ }
+
+ let mut tmp = [0u8; 24];
+ tmp[..].copy_from_slice(&bytes);
+ Ok(MailUuid(tmp))
+ }
+}