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
use crate::common::MacAddress;

///
/// Representation of a device on the network, with the mac, ip, and port involved in a connection
///
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Device {
    pub mac: MacAddress,
    pub ip: std::net::IpAddr,
    pub port: u16,
}

impl Default for Device {
    fn default() -> Self {
        Device {
            mac: MacAddress::default(),
            ip: std::net::IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0)),
            port: 0,
        }
    }
}

impl std::fmt::Display for Device {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "Mac={}   Ip={}   Port={}", self.mac, self.ip, self.port)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn format_device() {
        let dev = Device {
            ip: std::net::IpAddr::V4(std::net::Ipv4Addr::new(0, 1, 2, 3)),
            mac: MacAddress([0u8, 1u8, 2u8, 3u8, 4u8, 5u8]),
            port: 80,
        };

        assert_eq!(
            format!("{}", dev),
            "Mac=00:01:02:03:04:05   Ip=0.1.2.3   Port=80".to_string()
        );
    }
}