mirror of
https://codeberg.org/icewind/rfc7239.git
synced 2026-06-03 08:34:20 +02:00
Let NodeIdentifier return an IP address directly
This commit is contained in:
parent
e7dbbd847c
commit
d06e90f161
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
|
/// values containing `:` or `[]` characters need to be quoted
|
||||||
fn display_needs_quote(&self) -> bool {
|
fn display_needs_quote(&self) -> bool {
|
||||||
self.port.is_some() || matches!(self.name, NodeName::Ip(IpAddr::V6(_)))
|
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]
|
#[test]
|
||||||
fn test_display_node_identifier() {
|
fn test_display_node_identifier() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
@ -349,6 +365,14 @@ impl<'a> NodeName<'a> {
|
||||||
.map_err(|_| RfcError::InvalidIdentifier),
|
.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> {
|
impl<'a> Display for NodeName<'a> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue