2014-07-18 13:56:17 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
2015-04-07 01:51:11 +00:00
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
2014-07-18 13:56:17 +00:00
|
|
|
//
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2019-01-15 16:14:54 +00:00
|
|
|
//! Internal Macros
|
|
|
|
//!
|
|
|
|
//! Macros meant to be used inside the Rust Bitcoin library
|
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
macro_rules! impl_consensus_encoding {
|
2015-04-07 01:51:11 +00:00
|
|
|
($thing:ident, $($field:ident),+) => (
|
2020-01-25 04:19:46 +00:00
|
|
|
impl $crate::consensus::Encodable for $thing {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
2020-01-25 04:43:39 +00:00
|
|
|
fn consensus_encode<S: ::std::io::Write>(
|
2019-07-11 14:56:37 +00:00
|
|
|
&self,
|
|
|
|
mut s: S,
|
2020-01-25 04:19:46 +00:00
|
|
|
) -> Result<usize, $crate::consensus::encode::Error> {
|
2019-05-23 20:28:10 +00:00
|
|
|
let mut len = 0;
|
2019-07-11 14:56:37 +00:00
|
|
|
$(len += self.$field.consensus_encode(&mut s)?;)+
|
2019-05-23 20:28:10 +00:00
|
|
|
Ok(len)
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2020-01-25 04:19:46 +00:00
|
|
|
impl $crate::consensus::Decodable for $thing {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
2020-01-25 04:43:39 +00:00
|
|
|
fn consensus_decode<D: ::std::io::Read>(
|
2019-07-11 17:06:42 +00:00
|
|
|
mut d: D,
|
2020-01-25 04:19:46 +00:00
|
|
|
) -> Result<$thing, $crate::consensus::encode::Error> {
|
2015-04-07 01:51:11 +00:00
|
|
|
Ok($thing {
|
2020-01-25 04:19:46 +00:00
|
|
|
$($field: $crate::consensus::Decodable::consensus_decode(&mut d)?),+
|
2015-04-07 01:51:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-01-18 18:16:01 +00:00
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2020-01-25 04:19:46 +00:00
|
|
|
/// Implements standard array methods for a given wrapper type
|
2015-01-18 18:16:01 +00:00
|
|
|
macro_rules! impl_array_newtype {
|
2015-04-07 01:51:11 +00:00
|
|
|
($thing:ident, $ty:ty, $len:expr) => {
|
|
|
|
impl $thing {
|
|
|
|
#[inline]
|
|
|
|
/// Converts the object to a raw pointer
|
|
|
|
pub fn as_ptr(&self) -> *const $ty {
|
|
|
|
let &$thing(ref dat) = self;
|
|
|
|
dat.as_ptr()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Converts the object to a mutable raw pointer
|
|
|
|
pub fn as_mut_ptr(&mut self) -> *mut $ty {
|
|
|
|
let &mut $thing(ref mut dat) = self;
|
|
|
|
dat.as_mut_ptr()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns the length of the object as an array
|
|
|
|
pub fn len(&self) -> usize { $len }
|
2015-10-28 16:27:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns whether the object, as an array, is empty. Always false.
|
|
|
|
pub fn is_empty(&self) -> bool { false }
|
2018-05-16 09:31:59 +00:00
|
|
|
|
|
|
|
#[inline]
|
2018-08-20 21:20:43 +00:00
|
|
|
/// Returns the underlying bytes.
|
|
|
|
pub fn as_bytes(&self) -> &[$ty; $len] { &self.0 }
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns the underlying bytes.
|
|
|
|
pub fn to_bytes(&self) -> [$ty; $len] { self.0.clone() }
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns the underlying bytes.
|
|
|
|
pub fn into_bytes(self) -> [$ty; $len] { self.0 }
|
2015-10-14 13:56:48 +00:00
|
|
|
}
|
2015-04-07 01:51:11 +00:00
|
|
|
|
2020-01-25 04:43:39 +00:00
|
|
|
impl<'a> ::std::convert::From<&'a [$ty]> for $thing {
|
2015-10-14 13:56:48 +00:00
|
|
|
fn from(data: &'a [$ty]) -> $thing {
|
2015-04-07 01:51:11 +00:00
|
|
|
assert_eq!(data.len(), $len);
|
2018-02-14 16:44:02 +00:00
|
|
|
let mut ret = [0; $len];
|
|
|
|
ret.copy_from_slice(&data[..]);
|
|
|
|
$thing(ret)
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2015-04-07 22:51:57 +00:00
|
|
|
impl_index_newtype!($thing, $ty);
|
2014-08-28 16:49:03 +00:00
|
|
|
}
|
2015-01-18 18:16:01 +00:00
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2020-01-25 04:19:46 +00:00
|
|
|
/// Implements debug formatting for a given wrapper type
|
2015-01-18 18:16:01 +00:00
|
|
|
macro_rules! impl_array_newtype_show {
|
2015-04-07 01:51:11 +00:00
|
|
|
($thing:ident) => {
|
2020-01-25 04:43:39 +00:00
|
|
|
impl ::std::fmt::Debug for $thing {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
2015-04-07 01:51:11 +00:00
|
|
|
write!(f, concat!(stringify!($thing), "({:?})"), &self[..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:19:46 +00:00
|
|
|
/// Implements standard indexing methods for a given wrapper type
|
2015-04-07 22:51:57 +00:00
|
|
|
macro_rules! impl_index_newtype {
|
|
|
|
($thing:ident, $ty:ty) => {
|
2019-12-02 11:00:09 +00:00
|
|
|
|
|
|
|
impl ::std::ops::Index<usize> for $thing {
|
|
|
|
type Output = $ty;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: usize) -> &$ty {
|
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:43:39 +00:00
|
|
|
impl ::std::ops::Index<::std::ops::Range<usize>> for $thing {
|
2015-04-07 22:51:57 +00:00
|
|
|
type Output = [$ty];
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-25 04:43:39 +00:00
|
|
|
fn index(&self, index: ::std::ops::Range<usize>) -> &[$ty] {
|
2015-04-07 22:51:57 +00:00
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:43:39 +00:00
|
|
|
impl ::std::ops::Index<::std::ops::RangeTo<usize>> for $thing {
|
2015-04-07 22:51:57 +00:00
|
|
|
type Output = [$ty];
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-25 04:43:39 +00:00
|
|
|
fn index(&self, index: ::std::ops::RangeTo<usize>) -> &[$ty] {
|
2015-04-07 22:51:57 +00:00
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:43:39 +00:00
|
|
|
impl ::std::ops::Index<::std::ops::RangeFrom<usize>> for $thing {
|
2015-04-07 22:51:57 +00:00
|
|
|
type Output = [$ty];
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-25 04:43:39 +00:00
|
|
|
fn index(&self, index: ::std::ops::RangeFrom<usize>) -> &[$ty] {
|
2015-04-07 22:51:57 +00:00
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:43:39 +00:00
|
|
|
impl ::std::ops::Index<::std::ops::RangeFull> for $thing {
|
2015-04-07 22:51:57 +00:00
|
|
|
type Output = [$ty];
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-25 04:43:39 +00:00
|
|
|
fn index(&self, _: ::std::ops::RangeFull) -> &[$ty] {
|
2015-04-07 22:51:57 +00:00
|
|
|
&self.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 01:51:11 +00:00
|
|
|
macro_rules! display_from_debug {
|
|
|
|
($thing:ident) => {
|
2020-01-25 04:43:39 +00:00
|
|
|
impl ::std::fmt::Display for $thing {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
|
|
|
|
::std::fmt::Debug::fmt(self, f)
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
}
|
2015-01-18 18:16:01 +00:00
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2018-01-15 17:54:32 +00:00
|
|
|
#[cfg(test)]
|
2020-01-25 04:19:46 +00:00
|
|
|
macro_rules! hex_script (($s:expr) => ($crate::blockdata::script::Script::from(<Vec<u8> as $crate::hashes::hex::FromHex>::from_hex($s).unwrap())));
|
2018-01-15 17:54:32 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2020-01-25 04:19:46 +00:00
|
|
|
macro_rules! hex_hash (($h:ident, $s:expr) => ($h::from_slice(&<Vec<u8> as $crate::hashes::hex::FromHex>::from_hex($s).unwrap()).unwrap()));
|
2018-01-15 17:54:32 +00:00
|
|
|
|
2018-02-18 15:28:24 +00:00
|
|
|
macro_rules! serde_struct_impl {
|
2018-08-20 16:37:19 +00:00
|
|
|
($name:ident, $($fe:ident),*) => (
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
|
|
|
|
where
|
|
|
|
D: $crate::serde::de::Deserializer<'de>,
|
2018-02-18 15:28:24 +00:00
|
|
|
{
|
2020-01-25 04:43:39 +00:00
|
|
|
use ::std::fmt::{self, Formatter};
|
2018-08-20 16:37:19 +00:00
|
|
|
use $crate::serde::de::IgnoredAny;
|
2018-02-18 15:28:24 +00:00
|
|
|
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
enum Enum { Unknown__Field, $($fe),* }
|
|
|
|
|
|
|
|
struct EnumVisitor;
|
2018-08-20 16:37:19 +00:00
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for EnumVisitor {
|
2018-02-18 15:28:24 +00:00
|
|
|
type Value = Enum;
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a field name")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
2018-02-18 15:28:24 +00:00
|
|
|
{
|
2018-08-20 16:37:19 +00:00
|
|
|
match v {
|
2018-02-18 15:28:24 +00:00
|
|
|
$(
|
|
|
|
stringify!($fe) => Ok(Enum::$fe)
|
|
|
|
),*,
|
|
|
|
_ => Ok(Enum::Unknown__Field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
impl<'de> $crate::serde::Deserialize<'de> for Enum {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
2020-01-25 04:19:46 +00:00
|
|
|
D: $crate::serde::de::Deserializer<'de>,
|
2018-02-18 15:28:24 +00:00
|
|
|
{
|
2018-08-20 16:37:19 +00:00
|
|
|
deserializer.deserialize_str(EnumVisitor)
|
2018-02-18 15:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Visitor;
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
2018-02-18 15:28:24 +00:00
|
|
|
type Value = $name;
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a struct")
|
2018-02-18 15:28:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-26 04:39:03 +00:00
|
|
|
fn visit_seq<V>(self, mut seq: V) -> 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: $fe),*
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
|
|
|
where
|
|
|
|
A: $crate::serde::de::MapAccess<'de>,
|
2018-02-18 15:28:24 +00:00
|
|
|
{
|
2018-08-20 16:37:19 +00:00
|
|
|
use $crate::serde::de::Error;
|
2018-02-18 15:28:24 +00:00
|
|
|
|
|
|
|
$(let mut $fe = None;)*
|
|
|
|
|
|
|
|
loop {
|
2018-08-20 16:37:19 +00:00
|
|
|
match map.next_key::<Enum>()? {
|
|
|
|
Some(Enum::Unknown__Field) => {
|
|
|
|
map.next_value::<IgnoredAny>()?;
|
|
|
|
}
|
|
|
|
$(
|
|
|
|
Some(Enum::$fe) => {
|
|
|
|
$fe = Some(map.next_value()?);
|
|
|
|
}
|
|
|
|
)*
|
2018-02-18 15:28:24 +00:00
|
|
|
None => { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$(
|
2018-08-20 16:37:19 +00:00
|
|
|
let $fe = match $fe {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return Err(A::Error::missing_field(stringify!($fe))),
|
|
|
|
};
|
2018-02-18 15:28:24 +00:00
|
|
|
)*
|
2018-08-20 16:37:19 +00:00
|
|
|
|
|
|
|
let ret = $name {
|
|
|
|
$($fe: $fe),*
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ret)
|
2018-02-18 15:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// end type defs
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*];
|
2018-02-18 15:28:24 +00:00
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
deserializer.deserialize_struct(stringify!($name), FIELDS, Visitor)
|
2018-02-18 15:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl<'de> $crate::serde::Serialize for $name {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: $crate::serde::Serializer,
|
2018-02-18 15:28:24 +00:00
|
|
|
{
|
2018-08-20 16:37:19 +00:00
|
|
|
use $crate::serde::ser::SerializeStruct;
|
2018-02-18 15:28:24 +00:00
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
// Only used to get the struct length.
|
|
|
|
static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*];
|
2018-02-18 15:28:24 +00:00
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
let mut st = serializer.serialize_struct(stringify!($name), FIELDS.len())?;
|
2018-02-18 15:28:24 +00:00
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
$(
|
|
|
|
st.serialize_field(stringify!($fe), &self.$fe)?;
|
|
|
|
)*
|
2018-02-18 15:28:24 +00:00
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
st.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2018-02-18 15:28:24 +00:00
|
|
|
}
|
2019-01-15 16:14:54 +00:00
|
|
|
|
2019-05-28 13:06:32 +00:00
|
|
|
macro_rules! serde_string_impl {
|
|
|
|
($name:ident, $expecting:expr) => {
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
|
|
|
|
where
|
|
|
|
D: $crate::serde::de::Deserializer<'de>,
|
|
|
|
{
|
2020-01-25 04:43:39 +00:00
|
|
|
use ::std::fmt::{self, Formatter};
|
|
|
|
use ::std::str::FromStr;
|
2019-05-28 13:06:32 +00:00
|
|
|
|
|
|
|
struct Visitor;
|
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
|
|
|
type Value = $name;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str($expecting)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
|
|
|
{
|
|
|
|
$name::from_str(v).map_err(E::custom)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_str(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_str(&v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize_str(Visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl<'de> $crate::serde::Serialize for $name {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: $crate::serde::Serializer,
|
|
|
|
{
|
2019-06-06 10:07:41 +00:00
|
|
|
serializer.collect_str(&self)
|
2019-05-28 13:06:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-29 11:34:54 +00:00
|
|
|
/// A combination of serde_struct_impl and serde_string_impl where string is
|
|
|
|
/// used for human-readable serialization and struct is used for
|
|
|
|
/// non-human-readable serialization.
|
|
|
|
macro_rules! serde_struct_human_string_impl {
|
|
|
|
($name:ident, $expecting:expr, $($fe:ident),*) => (
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
|
|
|
|
where
|
|
|
|
D: $crate::serde::de::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
if deserializer.is_human_readable() {
|
2020-01-25 04:43:39 +00:00
|
|
|
use ::std::fmt::{self, Formatter};
|
|
|
|
use ::std::str::FromStr;
|
2019-05-29 11:34:54 +00:00
|
|
|
|
|
|
|
struct Visitor;
|
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
|
|
|
type Value = $name;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str($expecting)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
|
|
|
{
|
|
|
|
$name::from_str(v).map_err(E::custom)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_str(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_str(&v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize_str(Visitor)
|
|
|
|
} else {
|
2020-01-25 04:43:39 +00:00
|
|
|
use ::std::fmt::{self, Formatter};
|
2019-05-29 11:34:54 +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;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a field name")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> 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) -> 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, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a struct")
|
|
|
|
}
|
|
|
|
|
2019-12-26 04:39:03 +00:00
|
|
|
fn visit_seq<V>(self, mut seq: V) -> 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: $fe),*
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
|
2019-05-29 11:34:54 +00:00
|
|
|
fn visit_map<A>(self, mut map: A) -> 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: $fe),*
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// end type defs
|
|
|
|
|
|
|
|
static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*];
|
|
|
|
|
|
|
|
deserializer.deserialize_struct(stringify!($name), FIELDS, Visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl<'de> $crate::serde::Serialize for $name {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: $crate::serde::Serializer,
|
|
|
|
{
|
|
|
|
if serializer.is_human_readable() {
|
2019-06-06 10:07:41 +00:00
|
|
|
serializer.collect_str(&self)
|
2019-05-29 11:34:54 +00:00
|
|
|
} 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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-15 17:41:59 +00:00
|
|
|
/// Implements several traits for byte-based newtypes.
|
|
|
|
/// Implements:
|
2019-07-06 13:34:55 +00:00
|
|
|
/// - std::fmt::LowerHex (implies hashes::hex::ToHex)
|
2019-07-15 17:41:59 +00:00
|
|
|
/// - std::fmt::Display
|
|
|
|
/// - std::str::FromStr
|
2019-07-06 13:34:55 +00:00
|
|
|
/// - hashes::hex::FromHex
|
2019-07-15 17:41:59 +00:00
|
|
|
macro_rules! impl_bytes_newtype {
|
|
|
|
($t:ident, $len:expr) => (
|
|
|
|
|
2020-01-25 04:43:39 +00:00
|
|
|
impl ::std::fmt::LowerHex for $t {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
2019-07-15 17:41:59 +00:00
|
|
|
for &ch in self.0.iter() {
|
|
|
|
write!(f, "{:02x}", ch)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:43:39 +00:00
|
|
|
impl ::std::fmt::Display for $t {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
2019-07-15 17:41:59 +00:00
|
|
|
fmt::LowerHex::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:19:46 +00:00
|
|
|
impl $crate::hashes::hex::FromHex for $t {
|
|
|
|
fn from_byte_iter<I>(iter: I) -> Result<Self, $crate::hashes::hex::Error>
|
2020-01-25 04:43:39 +00:00
|
|
|
where I: ::std::iter::Iterator<Item=Result<u8, $crate::hashes::hex::Error>> +
|
|
|
|
::std::iter::ExactSizeIterator +
|
|
|
|
::std::iter::DoubleEndedIterator,
|
2019-07-15 17:41:59 +00:00
|
|
|
{
|
|
|
|
if iter.len() == $len {
|
|
|
|
let mut ret = [0; $len];
|
|
|
|
for (n, byte) in iter.enumerate() {
|
|
|
|
ret[n] = byte?;
|
|
|
|
}
|
|
|
|
Ok($t(ret))
|
|
|
|
} else {
|
2020-01-25 04:19:46 +00:00
|
|
|
Err($crate::hashes::hex::Error::InvalidLength(2 * $len, 2 * iter.len()))
|
2019-07-15 17:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:43:39 +00:00
|
|
|
impl ::std::str::FromStr for $t {
|
2020-01-25 04:19:46 +00:00
|
|
|
type Err = $crate::hashes::hex::Error;
|
2019-07-15 17:41:59 +00:00
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2020-01-25 04:19:46 +00:00
|
|
|
$crate::hashes::hex::FromHex::from_hex(s)
|
2019-07-15 17:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature="serde")]
|
2020-01-25 04:19:46 +00:00
|
|
|
impl $crate::serde::Serialize for $t {
|
|
|
|
fn serialize<S: $crate::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
2019-07-15 17:41:59 +00:00
|
|
|
if s.is_human_readable() {
|
2020-01-25 04:19:46 +00:00
|
|
|
s.serialize_str(&$crate::hashes::hex::ToHex::to_hex(self))
|
2019-07-15 17:41:59 +00:00
|
|
|
} else {
|
|
|
|
s.serialize_bytes(&self[..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature="serde")]
|
2020-01-25 04:19:46 +00:00
|
|
|
impl<'de> $crate::serde::Deserialize<'de> for $t {
|
|
|
|
fn deserialize<D: $crate::serde::Deserializer<'de>>(d: D) -> Result<$t, D::Error> {
|
2019-07-15 17:41:59 +00:00
|
|
|
if d.is_human_readable() {
|
|
|
|
struct HexVisitor;
|
|
|
|
|
2020-01-25 04:19:46 +00:00
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for HexVisitor {
|
2019-07-15 17:41:59 +00:00
|
|
|
type Value = $t;
|
|
|
|
|
2020-01-25 04:43:39 +00:00
|
|
|
fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
2019-07-15 17:41:59 +00:00
|
|
|
formatter.write_str("an ASCII hex string")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
|
|
|
where
|
2020-01-25 04:19:46 +00:00
|
|
|
E: $crate::serde::de::Error,
|
2019-07-15 17:41:59 +00:00
|
|
|
{
|
2020-01-25 04:43:39 +00:00
|
|
|
if let Ok(hex) = ::std::str::from_utf8(v) {
|
2020-01-25 04:19:46 +00:00
|
|
|
$crate::hashes::hex::FromHex::from_hex(hex).map_err(E::custom)
|
2019-07-15 17:41:59 +00:00
|
|
|
} else {
|
2020-01-25 04:19:46 +00:00
|
|
|
return Err(E::invalid_value($crate::serde::de::Unexpected::Bytes(v), &self));
|
2019-07-15 17:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
|
|
where
|
2020-01-25 04:19:46 +00:00
|
|
|
E: $crate::serde::de::Error,
|
2019-07-15 17:41:59 +00:00
|
|
|
{
|
2020-01-25 04:19:46 +00:00
|
|
|
$crate::hashes::hex::FromHex::from_hex(v).map_err(E::custom)
|
2019-07-15 17:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.deserialize_str(HexVisitor)
|
|
|
|
} else {
|
|
|
|
struct BytesVisitor;
|
|
|
|
|
2020-01-25 04:19:46 +00:00
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for BytesVisitor {
|
2019-07-15 17:41:59 +00:00
|
|
|
type Value = $t;
|
|
|
|
|
2020-01-25 04:43:39 +00:00
|
|
|
fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
2019-07-15 17:41:59 +00:00
|
|
|
formatter.write_str("a bytestring")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
|
|
|
where
|
2020-01-25 04:19:46 +00:00
|
|
|
E: $crate::serde::de::Error,
|
2019-07-15 17:41:59 +00:00
|
|
|
{
|
|
|
|
if v.len() != $len {
|
|
|
|
Err(E::invalid_length(v.len(), &stringify!($len)))
|
|
|
|
} else {
|
|
|
|
let mut ret = [0; $len];
|
|
|
|
ret.copy_from_slice(v);
|
|
|
|
Ok($t(ret))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.deserialize_bytes(BytesVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:14:54 +00:00
|
|
|
macro_rules! user_enum {
|
|
|
|
(
|
|
|
|
$(#[$attr:meta])*
|
|
|
|
pub enum $name:ident {
|
|
|
|
$(#[$doc:meta]
|
|
|
|
$elem:ident <-> $txt:expr),*
|
|
|
|
}
|
|
|
|
) => (
|
|
|
|
$(#[$attr])*
|
|
|
|
pub enum $name {
|
|
|
|
$(#[$doc] $elem),*
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:43:39 +00:00
|
|
|
impl ::std::fmt::Display for $name {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
2019-01-15 16:14:54 +00:00
|
|
|
f.pad(match *self {
|
|
|
|
$($name::$elem => $txt),*
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:43:39 +00:00
|
|
|
impl ::std::str::FromStr for $name {
|
|
|
|
type Err = ::std::io::Error;
|
2019-01-15 16:14:54 +00:00
|
|
|
#[inline]
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
$($txt => Ok($name::$elem)),*,
|
2020-01-25 04:43:39 +00:00
|
|
|
_ => Err(::std::io::Error::new(
|
|
|
|
::std::io::ErrorKind::InvalidInput,
|
2019-01-15 16:14:54 +00:00
|
|
|
format!("Unknown network (type {})", s),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
|
|
|
#[inline]
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: $crate::serde::Deserializer<'de>,
|
|
|
|
{
|
2020-01-25 04:43:39 +00:00
|
|
|
use ::std::fmt::{self, Formatter};
|
2019-01-15 16:14:54 +00:00
|
|
|
|
|
|
|
struct Visitor;
|
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
|
|
|
type Value = $name;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("an enum value")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
|
|
|
{
|
|
|
|
static FIELDS: &'static [&'static str] = &[$(stringify!($txt)),*];
|
|
|
|
|
|
|
|
$( if v == $txt { Ok($name::$elem) } )else*
|
|
|
|
else {
|
|
|
|
Err(E::unknown_variant(v, FIELDS))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_str(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_str(&v)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize_str(Visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
2020-01-25 04:19:46 +00:00
|
|
|
impl $crate::serde::Serialize for $name {
|
2019-01-15 16:14:54 +00:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
2020-01-25 04:19:46 +00:00
|
|
|
S: $crate::serde::Serializer,
|
2019-01-15 16:14:54 +00:00
|
|
|
{
|
2019-06-06 10:07:41 +00:00
|
|
|
serializer.collect_str(&self)
|
2019-01-15 16:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|