Skip to content

Commit

Permalink
Add protocol string to Error::UnknownProtocolString (#1355)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
  • Loading branch information
ackintosh and tomaka committed Jan 7, 2020
1 parent 84b6a7d commit d836191
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions misc/multiaddr/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum Error {
InvalidUvar(decode::Error),
ParsingError(Box<dyn error::Error + Send + Sync>),
UnknownProtocolId(u32),
UnknownProtocolString,
UnknownProtocolString(String),
#[doc(hidden)]
__Nonexhaustive
}
Expand All @@ -28,7 +28,7 @@ impl fmt::Display for Error {
Error::InvalidUvar(e) => write!(f, "failed to decode unsigned varint: {}", e),
Error::ParsingError(e) => write!(f, "failed to parse: {}", e),
Error::UnknownProtocolId(id) => write!(f, "unknown protocol id: {}", id),
Error::UnknownProtocolString => f.write_str("unknown protocol string"),
Error::UnknownProtocolString(string) => write!(f, "unknown protocol string: {}", string),
Error::__Nonexhaustive => f.write_str("__Nonexhaustive")
}
}
Expand Down
2 changes: 1 addition & 1 deletion misc/multiaddr/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl<'a> Protocol<'a> {
let s = iter.next().ok_or(Error::InvalidProtocolString)?;
Ok(Protocol::Memory(s.parse()?))
}
_ => Err(Error::UnknownProtocolString)
unknown => Err(Error::UnknownProtocolString(unknown.to_string()))
}
}

Expand Down
12 changes: 12 additions & 0 deletions misc/multiaddr/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,15 @@ fn replace_ip4_with_ip6() {
assert_eq!(result.unwrap(), "/ip6/2001:db8::1/tcp/10000".parse::<Multiaddr>().unwrap())
}

#[test]
fn unknown_protocol_string() {
match "/unknown/1.2.3.4".parse::<Multiaddr>() {
Ok(_) => assert!(false, "The UnknownProtocolString error should be caused"),
Err(e) => match e {
crate::Error::UnknownProtocolString(protocol) => {
assert_eq!(protocol, "unknown")
},
_ => assert!(false, "The UnknownProtocolString error should be caused")
}
}
}

0 comments on commit d836191

Please sign in to comment.