Merge pull request #80 from alekseysidorov/improve-network-constant
Implement `FromStr` for Network constant
This commit is contained in:
commit
c1ff953b25
|
@ -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>
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue