mirror of
https://codeberg.org/icewind/rfc7239.git
synced 2026-06-03 16:44:10 +02:00
Merge pull request #1 from mpalmer/node-identifier-ip
Let NodeIdentifier return an IP address directly
This commit is contained in:
commit
b68c1de04c
1 changed files with 24 additions and 0 deletions
24
src/lib.rs
24
src/lib.rs
|
|
@ -237,6 +237,10 @@ impl<'a> NodeIdentifier<'a> {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn ip(&self) -> Option<&IpAddr> {
|
||||
self.name.ip()
|
||||
}
|
||||
|
||||
/// values containing `:` or `[]` characters need to be quoted
|
||||
fn display_needs_quote(&self) -> bool {
|
||||
self.port.is_some() || matches!(self.name, NodeName::Ip(IpAddr::V6(_)))
|
||||
|
|
@ -282,6 +286,18 @@ fn test_parse_node_identifier() {
|
|||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_node_identifier_ip() {
|
||||
assert_eq!(
|
||||
Some(&IpAddr::from([192, 0, 2, 42])),
|
||||
NodeIdentifier::parse("192.0.2.42:31337").unwrap().ip()
|
||||
);
|
||||
assert_eq!(
|
||||
Some(&IpAddr::from([0x2001, 0xdb8, 0, 0, 0, 0, 0, 0x45])),
|
||||
NodeIdentifier::parse("[2001:db8::45]:31337").unwrap().ip()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_display_node_identifier() {
|
||||
assert_eq!(
|
||||
|
|
@ -349,6 +365,14 @@ impl<'a> NodeName<'a> {
|
|||
.map_err(|_| RfcError::InvalidIdentifier),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ip(&self) -> Option<&IpAddr> {
|
||||
if let NodeName::Ip(ip) = self {
|
||||
Some(ip)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Display for NodeName<'a> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue