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),+) => (
|
2019-07-11 14:56:37 +00:00
|
|
|
impl ::consensus::Encodable for $thing {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
2019-07-11 14:56:37 +00:00
|
|
|
fn consensus_encode<S: ::std::io::Write>(
|
|
|
|
&self,
|
|
|
|
mut s: S,
|
|
|
|
) -> Result<usize, ::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
|
|
|
|
2019-07-11 17:06:42 +00:00
|
|
|
impl ::consensus::Decodable for $thing {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
2019-07-11 17:06:42 +00:00
|
|
|
fn consensus_decode<D: ::std::io::Read>(
|
|
|
|
mut d: D,
|
|
|
|
) -> Result<$thing, ::consensus::encode::Error> {
|
2015-04-07 01:51:11 +00:00
|
|
|
Ok($thing {
|
2019-07-11 17:06:42 +00:00
|
|
|
$($field: ::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
|
|
|
|
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
|
|
|
|
2015-10-14 13:56:48 +00:00
|
|
|
impl<'a> From<&'a [$ty]> for $thing {
|
|
|
|
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-05 03:13:19 +00:00
|
|
|
impl ::std::ops::Index<usize> for $thing {
|
|
|
|
type Output = $ty;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: usize) -> &$ty {
|
|
|
|
let &$thing(ref dat) = self;
|
|
|
|
&dat[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 22:51:57 +00:00
|
|
|
impl_index_newtype!($thing, $ty);
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2015-04-07 01:51:11 +00:00
|
|
|
impl PartialEq for $thing {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &$thing) -> bool {
|
|
|
|
&self[..] == &other[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for $thing {}
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2015-12-20 21:38:02 +00:00
|
|
|
impl PartialOrd for $thing {
|
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, other: &$thing) -> Option<::std::cmp::Ordering> {
|
|
|
|
Some(self.cmp(&other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for $thing {
|
|
|
|
#[inline]
|
|
|
|
fn cmp(&self, other: &$thing) -> ::std::cmp::Ordering {
|
|
|
|
// manually implement comparison to get little-endian ordering
|
|
|
|
// (we need this for our numeric types; non-numeric ones shouldn't
|
|
|
|
// be ordered anyway except to put them in BTrees or whatever, and
|
2019-01-23 18:30:32 +00:00
|
|
|
// they don't care how we order as long as we're consistent).
|
2015-12-20 21:38:02 +00:00
|
|
|
for i in 0..$len {
|
|
|
|
if self[$len - 1 - i] < other[$len - 1 - i] { return ::std::cmp::Ordering::Less; }
|
|
|
|
if self[$len - 1 - i] > other[$len - 1 - i] { return ::std::cmp::Ordering::Greater; }
|
|
|
|
}
|
|
|
|
::std::cmp::Ordering::Equal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 14:35:27 +00:00
|
|
|
#[cfg_attr(feature = "clippy", allow(expl_impl_clone_on_copy))] // we don't define the `struct`, we have to explicitly impl
|
2015-04-07 01:51:11 +00:00
|
|
|
impl Clone for $thing {
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> $thing {
|
2015-10-14 13:56:48 +00:00
|
|
|
$thing::from(&self[..])
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2015-04-10 23:15:57 +00:00
|
|
|
impl Copy for $thing {}
|
|
|
|
|
2015-04-07 01:51:11 +00:00
|
|
|
impl ::std::hash::Hash for $thing {
|
|
|
|
#[inline]
|
|
|
|
fn hash<H>(&self, state: &mut H)
|
|
|
|
where H: ::std::hash::Hasher
|
|
|
|
{
|
|
|
|
(&self[..]).hash(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hash_slice<H>(data: &[$thing], state: &mut H)
|
|
|
|
where H: ::std::hash::Hasher
|
|
|
|
{
|
|
|
|
for d in data.iter() {
|
|
|
|
(&d[..]).hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
}
|
2015-01-18 18:16:01 +00:00
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
macro_rules! impl_array_newtype_show {
|
2015-04-07 01:51:11 +00:00
|
|
|
($thing:ident) => {
|
|
|
|
impl ::std::fmt::Debug for $thing {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
|
|
|
write!(f, concat!(stringify!($thing), "({:?})"), &self[..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 22:51:57 +00:00
|
|
|
macro_rules! impl_index_newtype {
|
|
|
|
($thing:ident, $ty:ty) => {
|
|
|
|
impl ::std::ops::Index<::std::ops::Range<usize>> for $thing {
|
|
|
|
type Output = [$ty];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ::std::ops::Range<usize>) -> &[$ty] {
|
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::Index<::std::ops::RangeTo<usize>> for $thing {
|
|
|
|
type Output = [$ty];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ::std::ops::RangeTo<usize>) -> &[$ty] {
|
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::Index<::std::ops::RangeFrom<usize>> for $thing {
|
|
|
|
type Output = [$ty];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ::std::ops::RangeFrom<usize>) -> &[$ty] {
|
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::Index<::std::ops::RangeFull> for $thing {
|
|
|
|
type Output = [$ty];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, _: ::std::ops::RangeFull) -> &[$ty] {
|
|
|
|
&self.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 01:51:11 +00:00
|
|
|
macro_rules! display_from_debug {
|
|
|
|
($thing:ident) => {
|
|
|
|
impl ::std::fmt::Display for $thing {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
|
|
|
|
::std::fmt::Debug::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
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)]
|
2018-07-24 18:43:03 +00:00
|
|
|
macro_rules! hex_script (($s:expr) => (::blockdata::script::Script::from(::hex::decode($s).unwrap())));
|
2018-01-15 17:54:32 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-01-16 20:45:31 +00:00
|
|
|
macro_rules! hex_hash (($s:expr) => (::bitcoin_hashes::sha256d::Hash::from_slice(&::hex::decode($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
|
|
|
{
|
2018-08-20 16:37:19 +00:00
|
|
|
use $crate::std::fmt::{self, Formatter};
|
|
|
|
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
|
|
|
|
D: ::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
|
|
|
}
|
|
|
|
|
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>,
|
|
|
|
{
|
2019-07-29 09:30:35 +00:00
|
|
|
use $crate::std::fmt::{self, Formatter};
|
|
|
|
use $crate::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() {
|
2019-07-29 09:30:35 +00:00
|
|
|
use $crate::std::fmt::{self, Formatter};
|
|
|
|
use $crate::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 {
|
|
|
|
use $crate::std::fmt::{self, 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, 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
/// - std::fmt::LowerHex (implies bitcoin_hashes::hex::ToHex)
|
|
|
|
/// - std::fmt::Display
|
|
|
|
/// - std::str::FromStr
|
|
|
|
/// - bitcoin_hashes::hex::FromHex
|
|
|
|
macro_rules! impl_bytes_newtype {
|
|
|
|
($t:ident, $len:expr) => (
|
|
|
|
|
|
|
|
impl ::std::fmt::LowerHex for $t {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
for &ch in self.0.iter() {
|
|
|
|
write!(f, "{:02x}", ch)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::fmt::Display for $t {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::LowerHex::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::bitcoin_hashes::hex::FromHex for $t {
|
|
|
|
fn from_byte_iter<I>(iter: I) -> Result<Self, bitcoin_hashes::hex::Error>
|
|
|
|
where I: Iterator<Item=Result<u8, bitcoin_hashes::hex::Error>> +
|
|
|
|
ExactSizeIterator +
|
|
|
|
DoubleEndedIterator,
|
|
|
|
{
|
|
|
|
if iter.len() == $len {
|
|
|
|
let mut ret = [0; $len];
|
|
|
|
for (n, byte) in iter.enumerate() {
|
|
|
|
ret[n] = byte?;
|
|
|
|
}
|
|
|
|
Ok($t(ret))
|
|
|
|
} else {
|
|
|
|
Err(::bitcoin_hashes::hex::Error::InvalidLength(2 * $len, 2 * iter.len()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::str::FromStr for $t {
|
|
|
|
type Err = bitcoin_hashes::hex::Error;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
hex::FromHex::from_hex(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature="serde")]
|
|
|
|
impl ::serde::Serialize for $t {
|
|
|
|
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
|
|
|
if s.is_human_readable() {
|
|
|
|
s.serialize_str(&::bitcoin_hashes::hex::ToHex::to_hex(self))
|
|
|
|
} else {
|
|
|
|
s.serialize_bytes(&self[..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature="serde")]
|
|
|
|
impl<'de> ::serde::Deserialize<'de> for $t {
|
|
|
|
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<$t, D::Error> {
|
|
|
|
if d.is_human_readable() {
|
|
|
|
struct HexVisitor;
|
|
|
|
|
|
|
|
impl<'de> ::serde::de::Visitor<'de> for HexVisitor {
|
|
|
|
type Value = $t;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
|
|
|
formatter.write_str("an ASCII hex string")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: ::serde::de::Error,
|
|
|
|
{
|
|
|
|
if let Ok(hex) = ::std::str::from_utf8(v) {
|
|
|
|
::bitcoin_hashes::hex::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>
|
|
|
|
where
|
|
|
|
E: ::serde::de::Error,
|
|
|
|
{
|
|
|
|
::bitcoin_hashes::hex::FromHex::from_hex(v).map_err(E::custom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.deserialize_str(HexVisitor)
|
|
|
|
} else {
|
|
|
|
struct BytesVisitor;
|
|
|
|
|
|
|
|
impl<'de> ::serde::de::Visitor<'de> for BytesVisitor {
|
|
|
|
type Value = $t;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
|
|
|
formatter.write_str("a bytestring")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: ::serde::de::Error,
|
|
|
|
{
|
|
|
|
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),*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::fmt::Debug for $name {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
|
|
|
f.pad(match *self {
|
|
|
|
$($name::$elem => $txt),*
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::fmt::Display for $name {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
|
|
|
f.pad(match *self {
|
|
|
|
$($name::$elem => $txt),*
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::str::FromStr for $name {
|
|
|
|
type Err = ::std::io::Error;
|
|
|
|
#[inline]
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
$($txt => Ok($name::$elem)),*,
|
|
|
|
_ => Err(::std::io::Error::new(
|
|
|
|
::std::io::ErrorKind::InvalidInput,
|
|
|
|
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>,
|
|
|
|
{
|
|
|
|
use $crate::std::fmt::{self, Formatter};
|
|
|
|
|
|
|
|
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")]
|
|
|
|
impl ::serde::Serialize for $name {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: ::serde::Serializer,
|
|
|
|
{
|
2019-06-06 10:07:41 +00:00
|
|
|
serializer.collect_str(&self)
|
2019-01-15 16:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|