1 /// Types of values that have a limited allowed value range
2 /// and can cause an [`crate::err::ValueTooBigError`].
3 #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
4 pub enum ValueType {
5     /// VLAN identifier field present in a [`crate::SingleVlanHeader`].
6     VlanId,
7     /// VLAN PCP (Priority Code Point) field in a [`crate::SingleVlanHeader`].
8     VlanPcp,
9     /// IP Fragment offset present in the IPv4 header and
10     /// IPv6 fragmentation header.
11     IpFragmentOffset,
12     /// IPv4 Header DSCP (Differentiated Services Code Point) field
13     /// present in an [`crate::Ipv4Header`].
14     Ipv4Dscp,
15     /// IPv4 Header ECN (Explicit Congestion Notification) field
16     /// present in an [`crate::Ipv4Header`].
17     Ipv4Ecn,
18     /// IPv6 Header Flow Label field present in [`crate::Ipv6Header`].
19     Ipv6FlowLabel,
20     /// IPv4 Header "total length" field based on the payload
21     /// length after the header.
22     Ipv4PayloadLength,
23     /// IPv6 Header "payload length" field present in an
24     /// [`crate::Ipv6Header`].
25     Ipv6PayloadLength,
26     /// Payload length used when calculating the checksum of a
27     /// [`crate::UdpHeader`] for IPv4.
28     UdpPayloadLengthIpv4,
29     /// Payload length used when calculating the checksum of a
30     /// [`crate::UdpHeader`] for IPv6.
31     UdpPayloadLengthIpv6,
32     /// Payload length used when calculating the checksum of a
33     /// [`crate::TcpHeader`] for IPv4.
34     TcpPayloadLengthIpv4,
35     /// Payload length used when calculating the checksum of a
36     /// [`crate::TcpHeader`] for IPv6.
37     TcpPayloadLengthIpv6,
38     /// Variable length data of an ICMPv6 packet.
39     Icmpv6PayloadLength,
40     /// Packet type of a Linux Cooked Capture v1 (SLL)
41     LinuxSllType,
42 }
43 
44 impl core::fmt::Display for ValueType {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result45     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
46         use ValueType::*;
47         match self {
48             VlanId => write!(f, "VLAN ID"),
49             VlanPcp => write!(f, "VLAN PCP (Priority Code Point)"),
50             IpFragmentOffset => write!(f, "IP Fragment Offset"),
51             Ipv4Dscp => write!(f, "IPv4 DSCP (Differentiated Services Code Point)"),
52             Ipv4Ecn => write!(f, "IPv4 ECN (Explicit Congestion Notification)"),
53             Ipv6FlowLabel => write!(f, "IPv6 Flow Label"),
54             Ipv4PayloadLength => write!(f, "IPv4 Header 'Payload Length' (sets 'Total Length')"),
55             Ipv6PayloadLength => write!(f, "IPv6 Header 'Payload Length'"),
56             UdpPayloadLengthIpv4 => write!(f, "UDP Payload Length (in IPv4 checksum calculation)"),
57             UdpPayloadLengthIpv6 => write!(f, "UDP Payload Length (in IPv6 checksum calculation)"),
58             TcpPayloadLengthIpv4 => write!(f, "TCP Payload Length (in IPv4 checksum calculation)"),
59             TcpPayloadLengthIpv6 => write!(f, "TCP Payload Length (in IPv6 checksum calculation)"),
60             Icmpv6PayloadLength => write!(f, "ICMPv6 Payload Length"),
61             LinuxSllType => write!(f, "Linux Cooked Capture v1 (SLL)"),
62         }
63     }
64 }
65 
66 #[cfg(test)]
67 mod test {
68     use super::*;
69     use std::format;
70 
71     #[test]
debug()72     fn debug() {
73         assert_eq!(
74             format!("{:?}", ValueType::IpFragmentOffset),
75             "IpFragmentOffset"
76         );
77     }
78 
79     #[test]
cmp_partial_cmp()80     fn cmp_partial_cmp() {
81         use core::cmp::Ordering;
82         let a = ValueType::IpFragmentOffset;
83         let b = a;
84         assert_eq!(a.cmp(&b), Ordering::Equal);
85         assert_eq!(a.partial_cmp(&b), Some(Ordering::Equal));
86     }
87 
88     #[test]
display()89     fn display() {
90         use ValueType::*;
91 
92         assert_eq!("VLAN ID", &format!("{}", VlanId));
93         assert_eq!("VLAN PCP (Priority Code Point)", &format!("{}", VlanPcp));
94         assert_eq!("IP Fragment Offset", &format!("{}", IpFragmentOffset));
95         assert_eq!(
96             "IPv4 DSCP (Differentiated Services Code Point)",
97             &format!("{}", Ipv4Dscp)
98         );
99         assert_eq!(
100             "IPv4 ECN (Explicit Congestion Notification)",
101             &format!("{}", Ipv4Ecn)
102         );
103         assert_eq!("IPv6 Flow Label", &format!("{}", Ipv6FlowLabel));
104         assert_eq!(
105             "IPv4 Header 'Payload Length' (sets 'Total Length')",
106             &format!("{}", Ipv4PayloadLength)
107         );
108         assert_eq!(
109             "IPv6 Header 'Payload Length'",
110             &format!("{}", Ipv6PayloadLength)
111         );
112         assert_eq!(
113             "UDP Payload Length (in IPv4 checksum calculation)",
114             &format!("{}", UdpPayloadLengthIpv4)
115         );
116         assert_eq!(
117             "UDP Payload Length (in IPv6 checksum calculation)",
118             &format!("{}", UdpPayloadLengthIpv6)
119         );
120         assert_eq!(
121             "TCP Payload Length (in IPv4 checksum calculation)",
122             &format!("{}", TcpPayloadLengthIpv4)
123         );
124         assert_eq!(
125             "TCP Payload Length (in IPv6 checksum calculation)",
126             &format!("{}", TcpPayloadLengthIpv6)
127         );
128         assert_eq!("ICMPv6 Payload Length", &format!("{}", Icmpv6PayloadLength));
129     }
130 }
131