Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve fmt::Debug for network/Address #379

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/network/address.rs
Expand Up @@ -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)
}
}
}

Expand All @@ -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,
Expand Down