Skip to content

Commit

Permalink
Merge pull request rust-bitcoin#80 from alekseysidorov/improve-networ…
Browse files Browse the repository at this point in the history
…k-constant

Implement `FromStr` for Network constant
  • Loading branch information
tamasblummer authored May 18, 2018
2 parents 7ffd829 + cc23f09 commit c1ff953
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
10 changes: 10 additions & 0 deletions src/network/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit c1ff953

Please sign in to comment.