Merge branch 'master' into data-method-in-impl-array-newtype

This commit is contained in:
Andrew Poelstra 2018-05-19 17:29:53 +00:00 committed by GitHub
commit ab96df162a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -105,6 +105,17 @@ macro_rules! user_enum {
} }
} }
impl ::std::str::FromStr for $name {
type Err = ::serde::de::value::Error;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
$($txt => Ok($name::$elem)),*,
_ => Err(::serde::de::Error::syntax(stringify!(s)))
}
}
}
impl ::serde::Deserialize for $name { impl ::serde::Deserialize for $name {
#[inline] #[inline]
fn deserialize<D>(d: &mut D) -> Result<$name, D::Error> fn deserialize<D>(d: &mut D) -> Result<$name, D::Error>

View File

@ -84,5 +84,15 @@ mod tests {
let bad: Result<Network, _> = deserialize("fakenet".as_bytes()); let bad: Result<Network, _> = deserialize("fakenet".as_bytes());
assert!(bad.is_err()); assert!(bad.is_err());
} }
#[test]
fn string_test() {
assert_eq!(Network::Bitcoin.to_string(), "bitcoin");
assert_eq!(Network::Testnet.to_string(), "testnet");
assert_eq!("bitcoin".parse(), Ok(Network::Bitcoin));
assert_eq!("testnet".parse(), Ok(Network::Testnet));
assert!("fakenet".parse::<Network>().is_err());
}
} }