Merge pull request #80 from alekseysidorov/improve-network-constant

Implement `FromStr` for Network constant
This commit is contained in:
Tamás Blummer 2018-05-18 09:35:57 +02:00 committed by GitHub
commit c1ff953b25
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 {
#[inline]
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());
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());
}
}