68 lines
1.6 KiB
Rust
68 lines
1.6 KiB
Rust
use super::{PrivateID, PublicID};
|
|
use serde::{de, Serialize, Serializer};
|
|
use std::fmt;
|
|
|
|
impl Serialize for PrivateID {
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: Serializer,
|
|
{
|
|
serializer.serialize_str(&self.as_hex())
|
|
}
|
|
}
|
|
|
|
struct PrivateIDVisitor;
|
|
|
|
impl<'de> de::Visitor<'de> for PrivateIDVisitor {
|
|
type Value = PrivateID;
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
formatter.write_str("a base-16 formatted ID as a string")
|
|
}
|
|
|
|
fn visit_str<E>(self, id: &str) -> Result<Self::Value, E>
|
|
where
|
|
E: de::Error,
|
|
{
|
|
match PrivateID::from_hex(id.to_string()) {
|
|
Ok(id) => Ok(id),
|
|
Err(_) => Err(de::Error::invalid_value(
|
|
de::Unexpected::Str(id),
|
|
&"a base-16 32 byte ID",
|
|
)),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Serialize for PublicID {
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: Serializer,
|
|
{
|
|
serializer.serialize_str(&self.as_hex())
|
|
}
|
|
}
|
|
|
|
struct PublicIDVisitor;
|
|
|
|
impl<'de> de::Visitor<'de> for PublicIDVisitor {
|
|
type Value = PublicID;
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
formatter.write_str("a base-16 formatted ID as a string")
|
|
}
|
|
|
|
fn visit_str<E>(self, id: &str) -> Result<Self::Value, E>
|
|
where
|
|
E: de::Error,
|
|
{
|
|
match PublicID::from_hex(id.to_string()) {
|
|
Ok(id) => Ok(id),
|
|
Err(_) => Err(de::Error::invalid_value(
|
|
de::Unexpected::Str(id),
|
|
&"a base-16 32 byte ID",
|
|
)),
|
|
}
|
|
}
|
|
}
|