1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use super::NetAddr;
use crate::traits::Broadcast;
use std::net::IpAddr;

impl Broadcast for NetAddr {
	type Output = Option<IpAddr>;

	fn broadcast(&self) -> Self::Output {
		match self {
			Self::V4(netaddr) => Some(IpAddr::from(netaddr.broadcast())),
			_ => None,
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::NetAddr;
	use std::net::IpAddr;

	mod v4 {
		use super::*;
		use std::net::Ipv4Addr;

		#[test]
		fn returns_correct_address() {
			let net: NetAddr = "127.0.0.1/8".parse().unwrap();
			assert_eq!(
				net.broadcast().unwrap(),
				IpAddr::V4(Ipv4Addr::new(127, 255, 255, 255))
			);

			let net: NetAddr = "192.168.69.25/29".parse().unwrap();
			assert_eq!(
				net.broadcast().unwrap(),
				IpAddr::V4(Ipv4Addr::new(192, 168, 69, 31))
			);

			let net: NetAddr = "192.168.128.127/32".parse().unwrap();
			assert_eq!(
				net.broadcast().unwrap(),
				IpAddr::V4(Ipv4Addr::new(192, 168, 128, 127))
			);
		}
	}

	mod v6 {
		use super::*;

		#[test]
		fn returns_none() {
			let net: NetAddr = "fe80::1/64".parse().unwrap();
			assert_eq!(net.broadcast(), None);
		}
	}
}