Merge rust-bitcoin/rust-bitcoin#2890: Move `serde` string macros to internals
865ba3fc39
Move serde string macros to internals (Tobin C. Harding)4a2b13fcde
internals: Feature gate whole serde module (Tobin C. Harding) Pull request description: The macros are internal things and can live in `internals`. This will help with future crate smashing. ACKs for top commit: apoelstra: ACK865ba3fc39
Kixunil: ACK865ba3fc39
Tree-SHA512: 7b3f029206c690ecf2894e0ad099d391312f7f8ec65ac9b5d4d9f25e6827f92075dcc851d0940a0faf1e27e7d0a305b575c8cc790939b3f222d7a2920d4d24fe
This commit is contained in:
commit
9b089d42c7
|
@ -365,7 +365,7 @@ impl<N: NetworkValidation> fmt::Display for DisplayUnchecked<'_, N> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
crate::serde_utils::serde_string_deserialize_impl!(Address<NetworkUnchecked>, "a Bitcoin address");
|
internals::serde_string_deserialize_impl!(Address<NetworkUnchecked>, "a Bitcoin address");
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
impl<N: NetworkValidation> serde::Serialize for Address<N> {
|
impl<N: NetworkValidation> serde::Serialize for Address<N> {
|
||||||
|
|
|
@ -76,7 +76,7 @@ pub struct Xpriv {
|
||||||
pub chain_code: ChainCode,
|
pub chain_code: ChainCode,
|
||||||
}
|
}
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
crate::serde_utils::serde_string_impl!(Xpriv, "a BIP-32 extended private key");
|
internals::serde_string_impl!(Xpriv, "a BIP-32 extended private key");
|
||||||
|
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
impl fmt::Debug for Xpriv {
|
impl fmt::Debug for Xpriv {
|
||||||
|
@ -109,7 +109,7 @@ pub struct Xpub {
|
||||||
pub chain_code: ChainCode,
|
pub chain_code: ChainCode,
|
||||||
}
|
}
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
crate::serde_utils::serde_string_impl!(Xpub, "a BIP-32 extended public key");
|
internals::serde_string_impl!(Xpub, "a BIP-32 extended public key");
|
||||||
|
|
||||||
/// A child number for a derived key
|
/// A child number for a derived key
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
|
||||||
|
@ -269,7 +269,7 @@ pub trait IntoDerivationPath {
|
||||||
pub struct DerivationPath(Vec<ChildNumber>);
|
pub struct DerivationPath(Vec<ChildNumber>);
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
crate::serde_utils::serde_string_impl!(DerivationPath, "a BIP-32 derivation path");
|
internals::serde_string_impl!(DerivationPath, "a BIP-32 derivation path");
|
||||||
|
|
||||||
impl<I> Index<I> for DerivationPath
|
impl<I> Index<I> for DerivationPath
|
||||||
where
|
where
|
||||||
|
|
|
@ -85,7 +85,7 @@ pub struct OutPoint {
|
||||||
pub vout: u32,
|
pub vout: u32,
|
||||||
}
|
}
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
crate::serde_utils::serde_struct_human_string_impl!(OutPoint, "an OutPoint", txid, vout);
|
internals::serde_struct_human_string_impl!(OutPoint, "an OutPoint", txid, vout);
|
||||||
|
|
||||||
impl OutPoint {
|
impl OutPoint {
|
||||||
/// The number of bytes that an outpoint contributes to the size of a transaction.
|
/// The number of bytes that an outpoint contributes to the size of a transaction.
|
||||||
|
|
|
@ -172,7 +172,7 @@ pub enum TapSighashType {
|
||||||
SinglePlusAnyoneCanPay = 0x83,
|
SinglePlusAnyoneCanPay = 0x83,
|
||||||
}
|
}
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
crate::serde_utils::serde_string_impl!(TapSighashType, "a TapSighashType data");
|
internals::serde_string_impl!(TapSighashType, "a TapSighashType data");
|
||||||
|
|
||||||
impl fmt::Display for TapSighashType {
|
impl fmt::Display for TapSighashType {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
@ -360,7 +360,7 @@ pub enum EcdsaSighashType {
|
||||||
SinglePlusAnyoneCanPay = 0x83,
|
SinglePlusAnyoneCanPay = 0x83,
|
||||||
}
|
}
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
crate::serde_utils::serde_string_impl!(EcdsaSighashType, "a EcdsaSighashType data");
|
internals::serde_string_impl!(EcdsaSighashType, "a EcdsaSighashType data");
|
||||||
|
|
||||||
impl fmt::Display for EcdsaSighashType {
|
impl fmt::Display for EcdsaSighashType {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
|
|
@ -298,226 +298,3 @@ pub mod hex_bytes {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! serde_string_serialize_impl {
|
|
||||||
($name:ty, $expecting:literal) => {
|
|
||||||
impl $crate::serde::Serialize for $name {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: $crate::serde::Serializer,
|
|
||||||
{
|
|
||||||
serializer.collect_str(&self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! serde_string_deserialize_impl {
|
|
||||||
($name:ty, $expecting:literal) => {
|
|
||||||
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
|
||||||
fn deserialize<D>(deserializer: D) -> core::result::Result<$name, D::Error>
|
|
||||||
where
|
|
||||||
D: $crate::serde::de::Deserializer<'de>,
|
|
||||||
{
|
|
||||||
use core::fmt::Formatter;
|
|
||||||
|
|
||||||
struct Visitor;
|
|
||||||
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
|
||||||
type Value = $name;
|
|
||||||
|
|
||||||
fn expecting(&self, f: &mut Formatter) -> core::fmt::Result {
|
|
||||||
f.write_str($expecting)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> core::result::Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: $crate::serde::de::Error,
|
|
||||||
{
|
|
||||||
v.parse::<$name>().map_err(E::custom)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deserializer.deserialize_str(Visitor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! serde_string_impl {
|
|
||||||
($name:ty, $expecting:literal) => {
|
|
||||||
$crate::serde_utils::serde_string_deserialize_impl!($name, $expecting);
|
|
||||||
$crate::serde_utils::serde_string_serialize_impl!($name, $expecting);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
pub(crate) use {serde_string_deserialize_impl, serde_string_impl, serde_string_serialize_impl};
|
|
||||||
|
|
||||||
/// 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 {
|
|
||||||
fn deserialize<D>(deserializer: D) -> core::result::Result<$name, D::Error>
|
|
||||||
where
|
|
||||||
D: $crate::serde::de::Deserializer<'de>,
|
|
||||||
{
|
|
||||||
if deserializer.is_human_readable() {
|
|
||||||
use core::fmt::Formatter;
|
|
||||||
use core::str::FromStr;
|
|
||||||
|
|
||||||
struct Visitor;
|
|
||||||
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
|
||||||
type Value = $name;
|
|
||||||
|
|
||||||
fn expecting(&self, f: &mut Formatter) -> core::fmt::Result {
|
|
||||||
f.write_str($expecting)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> core::result::Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: $crate::serde::de::Error,
|
|
||||||
{
|
|
||||||
$name::from_str(v).map_err(E::custom)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
deserializer.deserialize_str(Visitor)
|
|
||||||
} else {
|
|
||||||
use core::fmt::Formatter;
|
|
||||||
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;
|
|
||||||
|
|
||||||
fn expecting(&self, f: &mut Formatter) -> core::fmt::Result {
|
|
||||||
f.write_str("a field name")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> core::result::Result<Self::Value, E>
|
|
||||||
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 {
|
|
||||||
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
|
|
||||||
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;
|
|
||||||
|
|
||||||
fn expecting(&self, f: &mut Formatter) -> core::fmt::Result {
|
|
||||||
f.write_str("a struct")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_seq<V>(self, mut seq: V) -> core::result::Result<Self::Value, V::Error>
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_map<A>(self, mut map: A) -> core::result::Result<Self::Value, A::Error>
|
|
||||||
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 {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
|
|
||||||
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;
|
|
||||||
|
|
|
@ -26,4 +26,5 @@ pub mod const_tools;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod macros;
|
pub mod macros;
|
||||||
mod parse;
|
mod parse;
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
pub mod serde;
|
pub mod serde;
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
//! Contains extensions of `serde` and internal reexports.
|
//! Contains extensions of `serde` and internal reexports.
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use serde::{de, ser, Deserialize, Deserializer, Serialize, Serializer};
|
pub use serde::{de, ser, Deserialize, Deserializer, Serialize, Serializer};
|
||||||
|
|
||||||
|
@ -8,7 +7,6 @@ pub use serde::{de, ser, Deserialize, Deserializer, Serialize, Serializer};
|
||||||
///
|
///
|
||||||
/// This is used in [`Deserialize`] implementations to convert specialized errors into serde
|
/// This is used in [`Deserialize`] implementations to convert specialized errors into serde
|
||||||
/// errors.
|
/// errors.
|
||||||
#[cfg(feature = "serde")]
|
|
||||||
pub trait IntoDeError: Sized {
|
pub trait IntoDeError: Sized {
|
||||||
/// Converts to deserializer error possibly outputting vague message.
|
/// Converts to deserializer error possibly outputting vague message.
|
||||||
///
|
///
|
||||||
|
@ -28,7 +26,6 @@ pub trait IntoDeError: Sized {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
|
||||||
mod impls {
|
mod impls {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
@ -62,3 +59,237 @@ mod impls {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Implements `serde::Serialize` by way of `Display`.
|
||||||
|
///
|
||||||
|
/// `$name` is required to implement `core::fmt::Display`.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! serde_string_serialize_impl {
|
||||||
|
($name:ty, $expecting:literal) => {
|
||||||
|
impl $crate::serde::Serialize for $name {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: $crate::serde::Serializer,
|
||||||
|
{
|
||||||
|
serializer.collect_str(&self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implements `serde::Deserialize` by way of `FromStr`.
|
||||||
|
///
|
||||||
|
/// `$name` is required to implement `core::str::FromStr`.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! serde_string_deserialize_impl {
|
||||||
|
($name:ty, $expecting:literal) => {
|
||||||
|
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
||||||
|
fn deserialize<D>(deserializer: D) -> core::result::Result<$name, D::Error>
|
||||||
|
where
|
||||||
|
D: $crate::serde::de::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
use core::fmt::Formatter;
|
||||||
|
|
||||||
|
struct Visitor;
|
||||||
|
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
||||||
|
type Value = $name;
|
||||||
|
|
||||||
|
fn expecting(&self, f: &mut Formatter) -> core::fmt::Result {
|
||||||
|
f.write_str($expecting)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<E>(self, v: &str) -> core::result::Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: $crate::serde::de::Error,
|
||||||
|
{
|
||||||
|
v.parse::<$name>().map_err(E::custom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deserializer.deserialize_str(Visitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implements `serde::Serialize` and `Deserialize` by way of `Display` and `FromStr` respectively.
|
||||||
|
///
|
||||||
|
/// `$name` is required to implement `core::fmt::Display` and `core::str::FromStr`.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! serde_string_impl {
|
||||||
|
($name:ty, $expecting:literal) => {
|
||||||
|
$crate::serde_string_deserialize_impl!($name, $expecting);
|
||||||
|
$crate::serde_string_serialize_impl!($name, $expecting);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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_export]
|
||||||
|
macro_rules! serde_struct_human_string_impl {
|
||||||
|
($name:ident, $expecting:literal, $($fe:ident),*) => (
|
||||||
|
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
||||||
|
fn deserialize<D>(deserializer: D) -> core::result::Result<$name, D::Error>
|
||||||
|
where
|
||||||
|
D: $crate::serde::de::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
if deserializer.is_human_readable() {
|
||||||
|
use core::fmt::Formatter;
|
||||||
|
use core::str::FromStr;
|
||||||
|
|
||||||
|
struct Visitor;
|
||||||
|
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
||||||
|
type Value = $name;
|
||||||
|
|
||||||
|
fn expecting(&self, f: &mut Formatter) -> core::fmt::Result {
|
||||||
|
f.write_str($expecting)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<E>(self, v: &str) -> core::result::Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: $crate::serde::de::Error,
|
||||||
|
{
|
||||||
|
$name::from_str(v).map_err(E::custom)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
deserializer.deserialize_str(Visitor)
|
||||||
|
} else {
|
||||||
|
use core::fmt::Formatter;
|
||||||
|
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;
|
||||||
|
|
||||||
|
fn expecting(&self, f: &mut Formatter) -> core::fmt::Result {
|
||||||
|
f.write_str("a field name")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<E>(self, v: &str) -> core::result::Result<Self::Value, E>
|
||||||
|
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 {
|
||||||
|
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
|
||||||
|
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;
|
||||||
|
|
||||||
|
fn expecting(&self, f: &mut Formatter) -> core::fmt::Result {
|
||||||
|
f.write_str("a struct")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<V>(self, mut seq: V) -> core::result::Result<Self::Value, V::Error>
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<A>(self, mut map: A) -> core::result::Result<Self::Value, A::Error>
|
||||||
|
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 {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue