Fix formatting

IPv6 addresses without ports are not enclosed in brackets. They only get
enclosed in brackets in combination with a port.
This commit is contained in:
B. Blechschmidt 2023-03-25 10:58:16 +01:00
parent d4410264e6
commit e6e6c70006

View file

@ -30,10 +30,7 @@ pub enum DestinationHost {
impl std::fmt::Display for DestinationHost { impl std::fmt::Display for DestinationHost {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
DestinationHost::Address(addr) => match addr { DestinationHost::Address(addr) => addr.fmt(f),
IpAddr::V4(_) => addr.fmt(f),
IpAddr::V6(_) => write!(f, "[{}]", addr),
},
Hostname(name) => name.fmt(f), Hostname(name) => name.fmt(f),
} }
} }
@ -69,7 +66,11 @@ impl From<SocketAddr> for Destination {
impl std::fmt::Display for Destination { impl std::fmt::Display for Destination {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.host, self.port) if let DestinationHost::Address(IpAddr::V6(addr)) = self.host {
write!(f, "[{}]:{}", addr, self.port)
} else {
write!(f, "{}:{}", self.host, self.port)
}
} }
} }