2022-06-29 04:05:31 +00:00
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
|
|
|
|
2021-11-05 21:58:18 +00:00
|
|
|
//! Bitcoin serde utilities.
|
|
|
|
//!
|
|
|
|
//! This module is for special serde serializations.
|
|
|
|
//!
|
2020-10-25 18:27:45 +00:00
|
|
|
|
2023-01-07 15:39:11 +00:00
|
|
|
pub(crate) struct SerializeBytesAsHex<'a>(pub(crate) &'a [u8]);
|
|
|
|
|
|
|
|
impl<'a> serde::Serialize for SerializeBytesAsHex<'a> {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
2023-07-21 00:38:34 +00:00
|
|
|
use hex::DisplayHex;
|
2023-01-07 15:39:11 +00:00
|
|
|
|
|
|
|
serializer.collect_str(&format_args!("{:x}", self.0.as_hex()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:05:26 +00:00
|
|
|
pub mod btreemap_byte_values {
|
|
|
|
//! Module for serialization of BTreeMaps with hex byte values.
|
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
|
|
|
// NOTE: This module can be exactly copied to use with HashMap.
|
|
|
|
|
2023-07-21 00:38:34 +00:00
|
|
|
use hex::FromHex;
|
2020-12-27 21:05:26 +00:00
|
|
|
|
2022-06-06 04:34:09 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2022-01-24 00:33:03 +00:00
|
|
|
pub fn serialize<S, T>(v: &BTreeMap<T, Vec<u8>>, s: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
2020-12-27 21:05:26 +00:00
|
|
|
S: serde::Serializer,
|
2022-05-02 23:02:58 +00:00
|
|
|
T: serde::Serialize + core::hash::Hash + Eq + Ord,
|
2020-12-27 21:05:26 +00:00
|
|
|
{
|
|
|
|
use serde::ser::SerializeMap;
|
|
|
|
|
2020-12-27 21:15:41 +00:00
|
|
|
// Don't do anything special when not human readable.
|
|
|
|
if !s.is_human_readable() {
|
|
|
|
serde::Serialize::serialize(v, s)
|
|
|
|
} else {
|
|
|
|
let mut map = s.serialize_map(Some(v.len()))?;
|
|
|
|
for (key, value) in v.iter() {
|
2023-01-07 15:39:11 +00:00
|
|
|
map.serialize_entry(key, &super::SerializeBytesAsHex(value))?;
|
2020-12-27 21:15:41 +00:00
|
|
|
}
|
|
|
|
map.end()
|
2020-12-27 21:05:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 00:33:03 +00:00
|
|
|
pub fn deserialize<'de, D, T>(d: D) -> Result<BTreeMap<T, Vec<u8>>, D::Error>
|
|
|
|
where
|
2020-12-27 21:05:26 +00:00
|
|
|
D: serde::Deserializer<'de>,
|
2022-05-02 23:02:58 +00:00
|
|
|
T: serde::Deserialize<'de> + core::hash::Hash + Eq + Ord,
|
2020-12-27 21:05:26 +00:00
|
|
|
{
|
2022-05-02 23:02:58 +00:00
|
|
|
use core::marker::PhantomData;
|
2020-12-27 21:05:26 +00:00
|
|
|
|
|
|
|
struct Visitor<T>(PhantomData<T>);
|
2022-01-24 00:33:03 +00:00
|
|
|
impl<'de, T> serde::de::Visitor<'de> for Visitor<T>
|
|
|
|
where
|
2022-05-02 23:02:58 +00:00
|
|
|
T: serde::Deserialize<'de> + core::hash::Hash + Eq + Ord,
|
2020-12-27 21:05:26 +00:00
|
|
|
{
|
|
|
|
type Value = BTreeMap<T, Vec<u8>>;
|
|
|
|
|
2022-05-02 23:02:58 +00:00
|
|
|
fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
2020-12-27 21:05:26 +00:00
|
|
|
write!(f, "a map with hexadecimal values")
|
|
|
|
}
|
|
|
|
|
2022-06-06 04:34:09 +00:00
|
|
|
fn visit_map<A: serde::de::MapAccess<'de>>(
|
|
|
|
self,
|
|
|
|
mut a: A,
|
|
|
|
) -> Result<Self::Value, A::Error> {
|
2020-12-27 21:05:26 +00:00
|
|
|
let mut ret = BTreeMap::new();
|
|
|
|
while let Some((key, value)) = a.next_entry()? {
|
|
|
|
ret.insert(key, FromHex::from_hex(value).map_err(serde::de::Error::custom)?);
|
|
|
|
}
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:15:41 +00:00
|
|
|
// Don't do anything special when not human readable.
|
|
|
|
if !d.is_human_readable() {
|
|
|
|
serde::Deserialize::deserialize(d)
|
|
|
|
} else {
|
|
|
|
d.deserialize_map(Visitor(PhantomData))
|
|
|
|
}
|
2020-12-27 21:05:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 20:44:54 +00:00
|
|
|
pub mod btreemap_as_seq {
|
2020-12-27 21:05:26 +00:00
|
|
|
//! Module for serialization of BTreeMaps as lists of sequences because
|
|
|
|
//! serde_json will not serialize hashmaps with non-string keys be default.
|
2020-10-25 18:27:45 +00:00
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
|
|
|
// NOTE: This module can be exactly copied to use with HashMap.
|
|
|
|
|
2022-06-06 04:34:09 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2022-01-24 00:33:03 +00:00
|
|
|
pub fn serialize<S, T, U>(v: &BTreeMap<T, U>, s: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
2020-10-25 18:27:45 +00:00
|
|
|
S: serde::Serializer,
|
2022-05-02 23:02:58 +00:00
|
|
|
T: serde::Serialize + core::hash::Hash + Eq + Ord,
|
2020-10-25 18:27:45 +00:00
|
|
|
U: serde::Serialize,
|
|
|
|
{
|
|
|
|
use serde::ser::SerializeSeq;
|
|
|
|
|
2020-12-27 21:15:41 +00:00
|
|
|
// Don't do anything special when not human readable.
|
|
|
|
if !s.is_human_readable() {
|
|
|
|
serde::Serialize::serialize(v, s)
|
|
|
|
} else {
|
|
|
|
let mut seq = s.serialize_seq(Some(v.len()))?;
|
|
|
|
for pair in v.iter() {
|
|
|
|
seq.serialize_element(&pair)?;
|
|
|
|
}
|
|
|
|
seq.end()
|
2020-10-25 18:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 00:33:03 +00:00
|
|
|
pub fn deserialize<'de, D, T, U>(d: D) -> Result<BTreeMap<T, U>, D::Error>
|
|
|
|
where
|
2020-10-25 18:27:45 +00:00
|
|
|
D: serde::Deserializer<'de>,
|
2022-05-02 23:02:58 +00:00
|
|
|
T: serde::Deserialize<'de> + core::hash::Hash + Eq + Ord,
|
2020-10-25 18:27:45 +00:00
|
|
|
U: serde::Deserialize<'de>,
|
|
|
|
{
|
2022-05-02 23:02:58 +00:00
|
|
|
use core::marker::PhantomData;
|
2020-10-25 18:27:45 +00:00
|
|
|
|
|
|
|
struct Visitor<T, U>(PhantomData<(T, U)>);
|
2022-01-24 00:33:03 +00:00
|
|
|
impl<'de, T, U> serde::de::Visitor<'de> for Visitor<T, U>
|
|
|
|
where
|
2022-05-02 23:02:58 +00:00
|
|
|
T: serde::Deserialize<'de> + core::hash::Hash + Eq + Ord,
|
2020-10-25 18:27:45 +00:00
|
|
|
U: serde::Deserialize<'de>,
|
|
|
|
{
|
|
|
|
type Value = BTreeMap<T, U>;
|
|
|
|
|
2022-05-02 23:02:58 +00:00
|
|
|
fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
2020-10-25 18:27:45 +00:00
|
|
|
write!(f, "a sequence of pairs")
|
|
|
|
}
|
|
|
|
|
2022-06-06 04:34:09 +00:00
|
|
|
fn visit_seq<A: serde::de::SeqAccess<'de>>(
|
|
|
|
self,
|
|
|
|
mut a: A,
|
|
|
|
) -> Result<Self::Value, A::Error> {
|
2020-10-25 18:27:45 +00:00
|
|
|
let mut ret = BTreeMap::new();
|
|
|
|
while let Some((key, value)) = a.next_element()? {
|
|
|
|
ret.insert(key, value);
|
|
|
|
}
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:15:41 +00:00
|
|
|
// Don't do anything special when not human readable.
|
|
|
|
if !d.is_human_readable() {
|
|
|
|
serde::Deserialize::deserialize(d)
|
|
|
|
} else {
|
|
|
|
d.deserialize_seq(Visitor(PhantomData))
|
|
|
|
}
|
2020-10-25 18:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 20:44:54 +00:00
|
|
|
pub mod btreemap_as_seq_byte_values {
|
2020-12-27 21:05:26 +00:00
|
|
|
//! Module for serialization of BTreeMaps as lists of sequences because
|
2020-12-27 16:55:35 +00:00
|
|
|
//! serde_json will not serialize hashmaps with non-string keys be default.
|
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
|
|
|
// NOTE: This module can be exactly copied to use with HashMap.
|
|
|
|
|
2022-06-06 04:34:09 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2020-12-27 16:55:35 +00:00
|
|
|
/// A custom key-value pair type that serialized the bytes as hex.
|
|
|
|
#[derive(Debug, Deserialize)]
|
2022-05-25 06:41:59 +00:00
|
|
|
#[serde(crate = "actual_serde")]
|
2020-12-27 16:55:35 +00:00
|
|
|
struct OwnedPair<T>(
|
|
|
|
T,
|
2022-06-06 04:34:09 +00:00
|
|
|
#[serde(deserialize_with = "crate::serde_utils::hex_bytes::deserialize")] Vec<u8>,
|
2020-12-27 16:55:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/// A custom key-value pair type that serialized the bytes as hex.
|
|
|
|
#[derive(Debug, Serialize)]
|
2022-05-25 06:41:59 +00:00
|
|
|
#[serde(crate = "actual_serde")]
|
2020-12-27 16:55:35 +00:00
|
|
|
struct BorrowedPair<'a, T: 'static>(
|
|
|
|
&'a T,
|
2022-06-06 04:34:09 +00:00
|
|
|
#[serde(serialize_with = "crate::serde_utils::hex_bytes::serialize")] &'a [u8],
|
2020-12-27 16:55:35 +00:00
|
|
|
);
|
|
|
|
|
2022-01-24 00:33:03 +00:00
|
|
|
pub fn serialize<S, T>(v: &BTreeMap<T, Vec<u8>>, s: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
2020-12-27 16:55:35 +00:00
|
|
|
S: serde::Serializer,
|
2022-05-02 23:02:58 +00:00
|
|
|
T: serde::Serialize + core::hash::Hash + Eq + Ord + 'static,
|
2020-12-27 16:55:35 +00:00
|
|
|
{
|
|
|
|
use serde::ser::SerializeSeq;
|
|
|
|
|
2020-12-27 21:15:41 +00:00
|
|
|
// Don't do anything special when not human readable.
|
|
|
|
if !s.is_human_readable() {
|
|
|
|
serde::Serialize::serialize(v, s)
|
|
|
|
} else {
|
|
|
|
let mut seq = s.serialize_seq(Some(v.len()))?;
|
|
|
|
for (key, value) in v.iter() {
|
|
|
|
seq.serialize_element(&BorrowedPair(key, value))?;
|
|
|
|
}
|
|
|
|
seq.end()
|
2020-12-27 16:55:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 00:33:03 +00:00
|
|
|
pub fn deserialize<'de, D, T>(d: D) -> Result<BTreeMap<T, Vec<u8>>, D::Error>
|
|
|
|
where
|
2020-12-27 16:55:35 +00:00
|
|
|
D: serde::Deserializer<'de>,
|
2022-05-02 23:02:58 +00:00
|
|
|
T: serde::Deserialize<'de> + core::hash::Hash + Eq + Ord,
|
2020-12-27 16:55:35 +00:00
|
|
|
{
|
2022-05-02 23:02:58 +00:00
|
|
|
use core::marker::PhantomData;
|
2020-12-27 16:55:35 +00:00
|
|
|
|
|
|
|
struct Visitor<T>(PhantomData<T>);
|
2022-01-24 00:33:03 +00:00
|
|
|
impl<'de, T> serde::de::Visitor<'de> for Visitor<T>
|
|
|
|
where
|
2022-05-02 23:02:58 +00:00
|
|
|
T: serde::Deserialize<'de> + core::hash::Hash + Eq + Ord,
|
2020-12-27 16:55:35 +00:00
|
|
|
{
|
|
|
|
type Value = BTreeMap<T, Vec<u8>>;
|
|
|
|
|
2022-05-02 23:02:58 +00:00
|
|
|
fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
2020-12-27 16:55:35 +00:00
|
|
|
write!(f, "a sequence of pairs")
|
|
|
|
}
|
|
|
|
|
2022-06-06 04:34:09 +00:00
|
|
|
fn visit_seq<A: serde::de::SeqAccess<'de>>(
|
|
|
|
self,
|
|
|
|
mut a: A,
|
|
|
|
) -> Result<Self::Value, A::Error> {
|
2020-12-27 16:55:35 +00:00
|
|
|
let mut ret = BTreeMap::new();
|
|
|
|
while let Option::Some(OwnedPair(key, value)) = a.next_element()? {
|
|
|
|
ret.insert(key, value);
|
|
|
|
}
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:15:41 +00:00
|
|
|
// Don't do anything special when not human readable.
|
|
|
|
if !d.is_human_readable() {
|
|
|
|
serde::Deserialize::deserialize(d)
|
|
|
|
} else {
|
|
|
|
d.deserialize_seq(Visitor(PhantomData))
|
|
|
|
}
|
2020-12-27 16:55:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 18:27:45 +00:00
|
|
|
pub mod hex_bytes {
|
|
|
|
//! Module for serialization of byte arrays as hex strings.
|
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
2023-07-21 00:38:34 +00:00
|
|
|
use hex::FromHex;
|
2020-10-25 18:27:45 +00:00
|
|
|
|
2020-12-27 21:15:41 +00:00
|
|
|
pub fn serialize<T, S>(bytes: &T, s: S) -> Result<S::Ok, S::Error>
|
2022-01-24 00:33:03 +00:00
|
|
|
where
|
2022-06-06 04:34:09 +00:00
|
|
|
T: serde::Serialize + AsRef<[u8]>,
|
|
|
|
S: serde::Serializer,
|
2020-10-25 18:27:45 +00:00
|
|
|
{
|
2020-12-27 21:15:41 +00:00
|
|
|
// Don't do anything special when not human readable.
|
|
|
|
if !s.is_human_readable() {
|
|
|
|
serde::Serialize::serialize(bytes, s)
|
|
|
|
} else {
|
2023-01-07 15:39:11 +00:00
|
|
|
serde::Serialize::serialize(&super::SerializeBytesAsHex(bytes.as_ref()), s)
|
2020-12-27 21:15:41 +00:00
|
|
|
}
|
2020-10-25 18:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize<'de, D, B>(d: D) -> Result<B, D::Error>
|
2022-01-24 00:33:03 +00:00
|
|
|
where
|
2022-06-06 04:34:09 +00:00
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
B: serde::Deserialize<'de> + FromHex,
|
2020-10-25 18:27:45 +00:00
|
|
|
{
|
2022-05-02 23:02:58 +00:00
|
|
|
struct Visitor<B>(core::marker::PhantomData<B>);
|
2020-10-25 18:27:45 +00:00
|
|
|
|
|
|
|
impl<'de, B: FromHex> serde::de::Visitor<'de> for Visitor<B> {
|
|
|
|
type Value = B;
|
|
|
|
|
2022-05-02 23:02:58 +00:00
|
|
|
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
|
2020-10-25 18:27:45 +00:00
|
|
|
formatter.write_str("an ASCII hex string")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
2022-01-24 00:33:03 +00:00
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
2020-10-25 18:27:45 +00:00
|
|
|
{
|
2022-05-02 23:02:58 +00:00
|
|
|
if let Ok(hex) = core::str::from_utf8(v) {
|
2020-10-25 18:27:45 +00:00
|
|
|
FromHex::from_hex(hex).map_err(E::custom)
|
|
|
|
} else {
|
|
|
|
return Err(E::invalid_value(serde::de::Unexpected::Bytes(v), &self));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
2022-01-24 00:33:03 +00:00
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
2020-10-25 18:27:45 +00:00
|
|
|
{
|
|
|
|
FromHex::from_hex(v).map_err(E::custom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:15:41 +00:00
|
|
|
// Don't do anything special when not human readable.
|
|
|
|
if !d.is_human_readable() {
|
|
|
|
serde::Deserialize::deserialize(d)
|
|
|
|
} else {
|
2022-05-02 23:02:58 +00:00
|
|
|
d.deserialize_str(Visitor(core::marker::PhantomData))
|
2020-12-27 21:15:41 +00:00
|
|
|
}
|
2020-10-25 18:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-20 06:07:58 +00:00
|
|
|
|
2022-12-19 15:15:20 +00:00
|
|
|
macro_rules! serde_string_serialize_impl {
|
|
|
|
($name:ty, $expecting:literal) => {
|
2022-11-02 22:36:37 +00:00
|
|
|
impl $crate::serde::Serialize for $name {
|
2024-01-18 15:39:09 +00:00
|
|
|
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
|
2022-12-19 15:15:20 +00:00
|
|
|
where
|
|
|
|
S: $crate::serde::Serializer,
|
|
|
|
{
|
|
|
|
serializer.collect_str(&self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! serde_string_deserialize_impl {
|
|
|
|
($name:ty, $expecting:literal) => {
|
2022-09-20 06:07:58 +00:00
|
|
|
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
2024-01-18 15:39:09 +00:00
|
|
|
fn deserialize<D>(deserializer: D) -> core::result::Result<$name, D::Error>
|
2022-09-20 06:07:58 +00:00
|
|
|
where
|
|
|
|
D: $crate::serde::de::Deserializer<'de>,
|
|
|
|
{
|
2024-01-18 15:39:09 +00:00
|
|
|
use core::fmt::Formatter;
|
2022-09-20 06:07:58 +00:00
|
|
|
|
|
|
|
struct Visitor;
|
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
|
|
|
type Value = $name;
|
|
|
|
|
2024-01-18 15:39:09 +00:00
|
|
|
fn expecting(&self, f: &mut Formatter) -> core::fmt::Result {
|
2022-09-20 06:07:58 +00:00
|
|
|
f.write_str($expecting)
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:39:09 +00:00
|
|
|
fn visit_str<E>(self, v: &str) -> core::result::Result<Self::Value, E>
|
2022-09-20 06:07:58 +00:00
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
|
|
|
{
|
2022-11-02 22:36:37 +00:00
|
|
|
v.parse::<$name>().map_err(E::custom)
|
2022-09-20 06:07:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize_str(Visitor)
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 15:15:20 +00:00
|
|
|
};
|
|
|
|
}
|
2022-09-20 06:07:58 +00:00
|
|
|
|
2022-12-19 15:15:20 +00:00
|
|
|
macro_rules! serde_string_impl {
|
|
|
|
($name:ty, $expecting:literal) => {
|
2022-11-02 22:36:37 +00:00
|
|
|
$crate::serde_utils::serde_string_deserialize_impl!($name, $expecting);
|
|
|
|
$crate::serde_utils::serde_string_serialize_impl!($name, $expecting);
|
2022-09-20 06:07:58 +00:00
|
|
|
};
|
|
|
|
}
|
2022-11-02 22:36:37 +00:00
|
|
|
pub(crate) use {serde_string_deserialize_impl, serde_string_impl, serde_string_serialize_impl};
|
2022-09-20 06:07:58 +00:00
|
|
|
|
|
|
|
/// A combination macro where the human-readable serialization is done like
|
|
|
|
/// serde_string_impl and the non-human-readable impl is done as a struct.
|
|
|
|
macro_rules! serde_struct_human_string_impl {
|
|
|
|
($name:ident, $expecting:literal, $($fe:ident),*) => (
|
|
|
|
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
2024-01-18 15:39:09 +00:00
|
|
|
fn deserialize<D>(deserializer: D) -> core::result::Result<$name, D::Error>
|
2022-09-20 06:07:58 +00:00
|
|
|
where
|
|
|
|
D: $crate::serde::de::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
if deserializer.is_human_readable() {
|
2024-01-18 15:39:09 +00:00
|
|
|
use core::fmt::Formatter;
|
2022-09-20 06:07:58 +00:00
|
|
|
use core::str::FromStr;
|
|
|
|
|
|
|
|
struct Visitor;
|
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
|
|
|
type Value = $name;
|
|
|
|
|
2024-01-18 15:39:09 +00:00
|
|
|
fn expecting(&self, f: &mut Formatter) -> core::fmt::Result {
|
2022-09-20 06:07:58 +00:00
|
|
|
f.write_str($expecting)
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:39:09 +00:00
|
|
|
fn visit_str<E>(self, v: &str) -> core::result::Result<Self::Value, E>
|
2022-09-20 06:07:58 +00:00
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
|
|
|
{
|
|
|
|
$name::from_str(v).map_err(E::custom)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize_str(Visitor)
|
|
|
|
} else {
|
2024-01-18 15:39:09 +00:00
|
|
|
use core::fmt::Formatter;
|
2022-09-20 06:07:58 +00:00
|
|
|
use $crate::serde::de::IgnoredAny;
|
|
|
|
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
enum Enum { Unknown__Field, $($fe),* }
|
|
|
|
|
|
|
|
struct EnumVisitor;
|
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for EnumVisitor {
|
|
|
|
type Value = Enum;
|
|
|
|
|
2024-01-18 15:39:09 +00:00
|
|
|
fn expecting(&self, f: &mut Formatter) -> core::fmt::Result {
|
2022-09-20 06:07:58 +00:00
|
|
|
f.write_str("a field name")
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:39:09 +00:00
|
|
|
fn visit_str<E>(self, v: &str) -> core::result::Result<Self::Value, E>
|
2022-09-20 06:07:58 +00:00
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
|
|
|
{
|
|
|
|
match v {
|
|
|
|
$(
|
|
|
|
stringify!($fe) => Ok(Enum::$fe)
|
|
|
|
),*,
|
|
|
|
_ => Ok(Enum::Unknown__Field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> $crate::serde::Deserialize<'de> for Enum {
|
2024-01-18 15:39:09 +00:00
|
|
|
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
|
2022-09-20 06:07:58 +00:00
|
|
|
where
|
|
|
|
D: $crate::serde::de::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
deserializer.deserialize_str(EnumVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Visitor;
|
|
|
|
|
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
|
|
|
type Value = $name;
|
|
|
|
|
2024-01-18 15:39:09 +00:00
|
|
|
fn expecting(&self, f: &mut Formatter) -> core::fmt::Result {
|
2022-09-20 06:07:58 +00:00
|
|
|
f.write_str("a struct")
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:39:09 +00:00
|
|
|
fn visit_seq<V>(self, mut seq: V) -> core::result::Result<Self::Value, V::Error>
|
2022-09-20 06:07:58 +00:00
|
|
|
where
|
|
|
|
V: $crate::serde::de::SeqAccess<'de>,
|
|
|
|
{
|
|
|
|
use $crate::serde::de::Error;
|
|
|
|
|
|
|
|
let length = 0;
|
|
|
|
$(
|
|
|
|
let $fe = seq.next_element()?.ok_or_else(|| {
|
|
|
|
Error::invalid_length(length, &self)
|
|
|
|
})?;
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
let length = length + 1;
|
|
|
|
)*
|
|
|
|
|
|
|
|
let ret = $name {
|
|
|
|
$($fe),*
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:39:09 +00:00
|
|
|
fn visit_map<A>(self, mut map: A) -> core::result::Result<Self::Value, A::Error>
|
2022-09-20 06:07:58 +00:00
|
|
|
where
|
|
|
|
A: $crate::serde::de::MapAccess<'de>,
|
|
|
|
{
|
|
|
|
use $crate::serde::de::Error;
|
|
|
|
|
|
|
|
$(let mut $fe = None;)*
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match map.next_key::<Enum>()? {
|
|
|
|
Some(Enum::Unknown__Field) => {
|
|
|
|
map.next_value::<IgnoredAny>()?;
|
|
|
|
}
|
|
|
|
$(
|
|
|
|
Some(Enum::$fe) => {
|
|
|
|
$fe = Some(map.next_value()?);
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
None => { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$(
|
|
|
|
let $fe = match $fe {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return Err(A::Error::missing_field(stringify!($fe))),
|
|
|
|
};
|
|
|
|
)*
|
|
|
|
|
|
|
|
let ret = $name {
|
|
|
|
$($fe),*
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// end type defs
|
|
|
|
|
|
|
|
static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*];
|
|
|
|
|
|
|
|
deserializer.deserialize_struct(stringify!($name), FIELDS, Visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $crate::serde::Serialize for $name {
|
2024-01-18 15:39:09 +00:00
|
|
|
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
|
2022-09-20 06:07:58 +00:00
|
|
|
where
|
|
|
|
S: $crate::serde::Serializer,
|
|
|
|
{
|
|
|
|
if serializer.is_human_readable() {
|
|
|
|
serializer.collect_str(&self)
|
|
|
|
} else {
|
|
|
|
use $crate::serde::ser::SerializeStruct;
|
|
|
|
|
|
|
|
// Only used to get the struct length.
|
|
|
|
static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*];
|
|
|
|
|
|
|
|
let mut st = serializer.serialize_struct(stringify!($name), FIELDS.len())?;
|
|
|
|
|
|
|
|
$(
|
|
|
|
st.serialize_field(stringify!($fe), &self.$fe)?;
|
|
|
|
)*
|
|
|
|
|
|
|
|
st.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
pub(crate) use serde_struct_human_string_impl;
|