Improve fmt::Debug for network/Address
Original output: "Address {services: ServiceFlags(9), address: [0, 0, 0, 0, 0, 65535, 2560, 1], port: 8333}" New output: * for IPv4: "Address {services: ServiceFlags(NETWORK|WITNESS), address: 10.0.0.1, port: 8333}" * for IPv6: "Address {services: ServiceFlags(NETWORK_LIMITED), address: fd87:d87e:eb43::ffff:a00:1, port: 8333}"
This commit is contained in:
parent
50f3a60712
commit
9e223988fa
|
@ -99,9 +99,14 @@ impl Decodable for Address {
|
|||
|
||||
impl fmt::Debug for Address {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// TODO: render services and hex-ize address
|
||||
write!(f, "Address {{services: {:?}, address: {:?}, port: {:?}}}",
|
||||
self.services, &self.address[..], self.port)
|
||||
let ipv6 = Ipv6Addr::from(self.address);
|
||||
|
||||
match ipv6.to_ipv4() {
|
||||
Some(addr) => write!(f, "Address {{services: {}, address: {}, port: {}}}",
|
||||
self.services, addr, self.port),
|
||||
None => write!(f, "Address {{services: {}, address: {}, port: {}}}",
|
||||
self.services, ipv6, self.port)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -125,6 +130,27 @@ mod test {
|
|||
0, 0, 0, 0xff, 0xff, 0x0a, 0, 0, 1, 0x20, 0x8d]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn debug_format_test() {
|
||||
assert_eq!(
|
||||
format!("The address is: {:?}", Address {
|
||||
services: ServiceFlags::NETWORK.add(ServiceFlags::WITNESS),
|
||||
address: [0, 0, 0, 0, 0, 0xffff, 0x0a00, 0x0001],
|
||||
port: 8333
|
||||
}),
|
||||
"The address is: Address {services: ServiceFlags(NETWORK|WITNESS), address: 10.0.0.1, port: 8333}"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
format!("The address is: {:?}", Address {
|
||||
services: ServiceFlags::NETWORK_LIMITED,
|
||||
address: [0xFD87, 0xD87E, 0xEB43, 0, 0, 0xffff, 0x0a00, 0x0001],
|
||||
port: 8333
|
||||
}),
|
||||
"The address is: Address {services: ServiceFlags(NETWORK_LIMITED), address: fd87:d87e:eb43::ffff:a00:1, port: 8333}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_address_test() {
|
||||
let mut addr: Result<Address, _> = deserialize(&[1u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
|
|
Loading…
Reference in New Issue