Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize `XorName` to a string with hex representation, only if serializing to a human readable format like with `serde_json` or `toml`. This is guarded behind a feature as it adds a dependency to the `hex` crate.
- Loading branch information
Showing
5 changed files
with
336 additions
and
6 deletions.
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
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,284 @@ | ||
use crate::{Prefix, XorName}; | ||
use serde::{ | ||
de::{self, MapAccess, SeqAccess, Visitor}, | ||
ser::SerializeStruct, | ||
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() { | ||
struct XorNameHexStrVisitor; | ||
impl<'de> Visitor<'de> for XorNameHexStrVisitor { | ||
type Value = XorName; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
write!(formatter, "32 byte hex string") | ||
} | ||
|
||
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)) | ||
} | ||
} | ||
return deserializer.deserialize_str(XorNameHexStrVisitor); | ||
} | ||
|
||
struct XorNameNewtypeVisitor; | ||
impl<'de> Visitor<'de> for XorNameNewtypeVisitor { | ||
type Value = XorName; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
write!(formatter, "tuple struct XorName") | ||
} | ||
|
||
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: 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", | ||
)), | ||
} | ||
} | ||
} | ||
|
||
deserializer.deserialize_newtype_struct("XorName", XorNameNewtypeVisitor) | ||
} | ||
} | ||
|
||
impl Serialize for Prefix { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
if serializer.is_human_readable() { | ||
let hex_str = hex::encode(&self.name); | ||
let bit_count = self.bit_count; | ||
let s = std::format!("{hex_str}/{bit_count}"); | ||
|
||
return serializer.serialize_str(&s); | ||
} | ||
|
||
let mut s = serializer.serialize_struct("Prefix", 2)?; | ||
s.serialize_field("bit_count", &self.bit_count)?; | ||
s.serialize_field("name", &self.name)?; | ||
s.end() | ||
} | ||
} | ||
impl<'de> Deserialize<'de> for Prefix { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
if deserializer.is_human_readable() { | ||
struct PrefixVisitor; | ||
impl<'de> Visitor<'de> for PrefixVisitor { | ||
type Value = Prefix; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
write!( | ||
formatter, | ||
"prefix in string format (\"<xor hex string>/<prefix>\")" | ||
) | ||
} | ||
|
||
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> | ||
where | ||
E: de::Error, | ||
{ | ||
let mut split = s.split('/'); | ||
|
||
let hex_str = split | ||
.next() | ||
.ok_or_else(|| de::Error::custom("`str::split` logic error"))?; | ||
let bit_count = split | ||
.next() | ||
.ok_or_else(|| de::Error::custom("no `/` symbol encountered"))?; | ||
|
||
let k: [u8; 32] = hex::FromHex::from_hex(hex_str) | ||
.map_err(|_| de::Error::custom("invalid 32 byte hex string"))?; | ||
let bit_count = bit_count | ||
.parse::<usize>() | ||
.map_err(|_e| de::Error::custom("bit_count is not a valid `usize`"))?; | ||
|
||
Ok(Prefix::new(bit_count, XorName(k))) | ||
} | ||
} | ||
return deserializer.deserialize_str(PrefixVisitor); | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[serde(field_identifier, rename_all = "snake_case")] | ||
enum Field { | ||
BitCount, | ||
Name, | ||
} | ||
|
||
struct PrefixVisitor; | ||
impl<'de> Visitor<'de> for PrefixVisitor { | ||
type Value = Prefix; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str("whaat") | ||
} | ||
|
||
fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error> | ||
where | ||
V: SeqAccess<'de>, | ||
{ | ||
let bit_count = seq | ||
.next_element()? | ||
.ok_or_else(|| de::Error::invalid_length(0, &self))?; | ||
let name = seq | ||
.next_element()? | ||
.ok_or_else(|| de::Error::invalid_length(1, &self))?; | ||
Ok(Prefix { bit_count, name }) | ||
} | ||
|
||
fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error> | ||
where | ||
V: MapAccess<'de>, | ||
{ | ||
let mut bit_count = None; | ||
let mut name = None; | ||
while let Some(key) = map.next_key()? { | ||
match key { | ||
Field::BitCount => { | ||
if bit_count.is_some() { | ||
return Err(de::Error::duplicate_field("bit_count")); | ||
} | ||
bit_count = Some(map.next_value()?); | ||
} | ||
Field::Name => { | ||
if name.is_some() { | ||
return Err(de::Error::duplicate_field("name")); | ||
} | ||
name = Some(map.next_value()?); | ||
} | ||
} | ||
} | ||
let bit_count = bit_count.ok_or_else(|| de::Error::missing_field("bit_count"))?; | ||
let name = name.ok_or_else(|| de::Error::missing_field("name"))?; | ||
Ok(Prefix { bit_count, name }) | ||
} | ||
} | ||
const FIELDS: &'static [&'static str] = &["bit_count", "name"]; | ||
deserializer.deserialize_struct("Prefix", FIELDS, PrefixVisitor) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use serde_test::*; | ||
|
||
/// `XorName` with derived `Serialize` impl. Used to compare against. | ||
#[derive(PartialEq, Debug, serde::Serialize, Deserialize)] | ||
struct XorNameDerived([u8; 32]); | ||
|
||
/// `Prefix` with derived `Serialize` impl. Used to compare against. | ||
#[derive(PartialEq, Debug, serde::Serialize, Deserialize)] | ||
struct PrefixDerived { | ||
bit_count: u16, | ||
name: XorNameDerived, | ||
} | ||
|
||
#[test] | ||
fn xorname_ser_de() { | ||
let xor = XorName([0xAA; 32]); | ||
let xor_derived = XorNameDerived([0xAA; 32]); | ||
|
||
let xor_hex_str = static_str("aa".repeat(32)); | ||
assert_tokens(&xor.readable(), &[Token::Str(xor_hex_str)]); | ||
|
||
assert_tokens(&xor.compact(), &xor_tokens("XorName")); | ||
// Verify our `Serialize` impl is same as when it would be derived | ||
assert_tokens(&xor_derived.compact(), &xor_tokens("XorNameDerived")); | ||
} | ||
|
||
#[test] | ||
fn prefix_ser_de() { | ||
let prefix = Prefix { | ||
bit_count: 14, | ||
name: XorName([0xAA; 32]), | ||
}; | ||
let prefix_derived = PrefixDerived { | ||
bit_count: 14, | ||
name: XorNameDerived([0xAA; 32]), | ||
}; | ||
|
||
let xor_hex_str = static_str("aa".repeat(32) + "/14"); | ||
assert_tokens(&prefix.readable(), &[Token::Str(xor_hex_str)]); | ||
|
||
assert_tokens(&prefix.compact(), &prefix_tokens("Prefix", "XorName")); | ||
// Verify our `Serialize` impl is same as when it would be derived | ||
assert_tokens( | ||
&prefix_derived.compact(), | ||
&prefix_tokens("PrefixDerived", "XorNameDerived"), | ||
); | ||
} | ||
|
||
// Little helper to leak a &str to obtain a static str (`Token::Str` requires &'static str) | ||
fn static_str(s: String) -> &'static str { | ||
Box::leak(s.into_boxed_str()) | ||
} | ||
|
||
// Compact/derived representation of `XorName` | ||
fn xor_tokens(name: &'static str) -> Vec<Token> { | ||
let mut a = vec![]; | ||
a.extend_from_slice(&[Token::NewtypeStruct { name }, Token::Tuple { len: 32 }]); | ||
a.extend_from_slice(&[Token::U8(0xAA); 32]); // Repeat a U8 Token 32 times | ||
a.extend_from_slice(&[Token::TupleEnd]); | ||
a | ||
} | ||
|
||
// Compact/derived representation of `Prefix` | ||
fn prefix_tokens(name: &'static str, name2: &'static str) -> Vec<Token> { | ||
let mut v = vec![ | ||
Token::Struct { name, len: 2 }, | ||
Token::Str("bit_count"), | ||
Token::U16(14), | ||
Token::Str("name"), | ||
]; | ||
v.extend_from_slice(&xor_tokens(name2)); | ||
v.extend_from_slice(&[Token::StructEnd]); | ||
v | ||
} | ||
} |