Move serde string macros to internals
The macros are internal things and can live in `internals`. This will help with future crate smashing.
This commit is contained in:
parent
4a2b13fcde
commit
865ba3fc39
|
@ -365,7 +365,7 @@ impl<N: NetworkValidation> fmt::Display for DisplayUnchecked<'_, N> {
|
|||
}
|
||||
|
||||
#[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")]
|
||||
impl<N: NetworkValidation> serde::Serialize for Address<N> {
|
||||
|
|
|
@ -77,7 +77,7 @@ pub struct Xpriv {
|
|||
pub chain_code: ChainCode,
|
||||
}
|
||||
#[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"))]
|
||||
impl fmt::Debug for Xpriv {
|
||||
|
@ -110,7 +110,7 @@ pub struct Xpub {
|
|||
pub chain_code: ChainCode,
|
||||
}
|
||||
#[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
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
|
||||
|
@ -259,7 +259,7 @@ pub trait IntoDerivationPath {
|
|||
pub struct DerivationPath(Vec<ChildNumber>);
|
||||
|
||||
#[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
|
||||
where
|
||||
|
|
|
@ -68,7 +68,7 @@ pub struct OutPoint {
|
|||
pub vout: u32,
|
||||
}
|
||||
#[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 {
|
||||
/// The number of bytes that an outpoint contributes to the size of a transaction.
|
||||
|
|
|
@ -160,7 +160,7 @@ pub enum TapSighashType {
|
|||
SinglePlusAnyoneCanPay = 0x83,
|
||||
}
|
||||
#[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 {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
|
@ -348,7 +348,7 @@ pub enum EcdsaSighashType {
|
|||
SinglePlusAnyoneCanPay = 0x83,
|
||||
}
|
||||
#[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 {
|
||||
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;
|
||||
|
|
|
@ -59,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