net_parser_rs/flow/
device.rs

1use crate::common::MacAddress;
2
3///
4/// Representation of a device on the network, with the mac, ip, and port involved in a connection
5///
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub struct Device {
8    pub mac: MacAddress,
9    pub ip: std::net::IpAddr,
10    pub port: u16,
11}
12
13impl Default for Device {
14    fn default() -> Self {
15        Device {
16            mac: MacAddress::default(),
17            ip: std::net::IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0)),
18            port: 0,
19        }
20    }
21}
22
23impl std::fmt::Display for Device {
24    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
25        write!(f, "Mac={}   Ip={}   Port={}", self.mac, self.ip, self.port)
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn format_device() {
35        let dev = Device {
36            ip: std::net::IpAddr::V4(std::net::Ipv4Addr::new(0, 1, 2, 3)),
37            mac: MacAddress([0u8, 1u8, 2u8, 3u8, 4u8, 5u8]),
38            port: 80,
39        };
40
41        assert_eq!(
42            format!("{}", dev),
43            "Mac=00:01:02:03:04:05   Ip=0.1.2.3   Port=80".to_string()
44        );
45    }
46}