Merge pull request #211 from dongcarl/2019-01-move-user-enum-macro
Internalize unnecessarily exported macros
This commit is contained in:
commit
81bfc4f027
|
@ -12,6 +12,10 @@
|
|||
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
||||
//
|
||||
|
||||
//! Internal Macros
|
||||
//!
|
||||
//! Macros meant to be used inside the Rust Bitcoin library
|
||||
|
||||
macro_rules! impl_consensus_encoding {
|
||||
($thing:ident, $($field:ident),+) => (
|
||||
impl<S: ::consensus::encode::Encoder> ::consensus::encode::Encodable<S> for $thing {
|
||||
|
@ -416,3 +420,107 @@ macro_rules! serde_struct_impl {
|
|||
}
|
||||
)
|
||||
}
|
||||
|
||||
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,
|
||||
{
|
||||
serializer.serialize_str(&self.to_string())
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -58,7 +58,6 @@ mod test_macros;
|
|||
#[macro_use]
|
||||
mod internal_macros;
|
||||
#[macro_use]
|
||||
pub mod macros;
|
||||
pub mod network;
|
||||
pub mod blockdata;
|
||||
pub mod util;
|
||||
|
@ -82,4 +81,4 @@ pub use util::decimal::Decimal;
|
|||
pub use util::decimal::UDecimal;
|
||||
|
||||
#[cfg(feature = "fuzztarget")]
|
||||
pub mod fuzz_util;
|
||||
pub mod fuzz_util;
|
||||
|
|
123
src/macros.rs
123
src/macros.rs
|
@ -1,123 +0,0 @@
|
|||
// Rust Bitcoin Library
|
||||
// Written in 2014 by
|
||||
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
||||
//
|
||||
// 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/>.
|
||||
//
|
||||
|
||||
//! Macros
|
||||
//!
|
||||
//! Macros available to users of the Bitcoin library
|
||||
|
||||
#[macro_export]
|
||||
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,
|
||||
{
|
||||
serializer.serialize_str(&self.to_string())
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
@ -16,7 +16,6 @@
|
|||
//!
|
||||
//! Internal macros used for unit tests
|
||||
|
||||
#[macro_export]
|
||||
#[cfg(all(feature = "serde", feature = "strason"))]
|
||||
macro_rules! serde_round_trip (
|
||||
($var:expr) => ({
|
||||
|
|
Loading…
Reference in New Issue