Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use crate::XorName; | ||
use serde::{ | ||
de::{self, Visitor}, | ||
Deserialize, Deserializer, Serialize, Serializer, | ||
}; | ||
use std::fmt; | ||
|
||
impl Serialize for XorName { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
// Return string with hexadecimal representation | ||
if serializer.is_human_readable() { | ||
return serializer.serialize_str(&hex::encode(self.0)); | ||
} | ||
|
||
// Default serialization. | ||
serializer.serialize_newtype_struct("XorName", &self.0) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for XorName { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
if deserializer.is_human_readable() { | ||
return deserializer.deserialize_str(HexStrOrNewtype); | ||
} | ||
|
||
deserializer.deserialize_newtype_struct("XorName", HexStrOrNewtype) | ||
} | ||
} | ||
|
||
struct HexStrOrNewtype; | ||
impl<'de> Visitor<'de> for HexStrOrNewtype { | ||
type Value = XorName; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
write!(formatter, "tuple struct XorName") | ||
} | ||
|
||
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> | ||
where | ||
E: de::Error, | ||
{ | ||
let buffer = <[u8; 32] as hex::FromHex>::from_hex(s) | ||
.map_err(|e| E::custom(std::format!("hex decoding ({})", e)))?; | ||
Ok(XorName(buffer)) | ||
} | ||
|
||
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
Ok(XorName(<[u8; 32] as Deserialize>::deserialize( | ||
deserializer, | ||
)?)) | ||
} | ||
|
||
// A method like this was generated (August 2022) by #[derive(Deserialize)], so assumed to be of use | ||
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> | ||
where | ||
A: de::SeqAccess<'de>, | ||
{ | ||
match seq.next_element::<[u8; 32]>()? { | ||
Some(a) => Ok(XorName(a)), | ||
None => Err(de::Error::invalid_length( | ||
0, | ||
&"tuple struct XorName with 1 element", | ||
)), | ||
} | ||
} | ||
} |