etherparse/link/
linux_sll_payload_slice.rs

1use crate::{EtherPayloadSlice, EtherType, LenSource, LinuxSllProtocolType};
2
3/// Payload of Linux Cooked Capture v1 (SLL) packet
4#[derive(Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
5pub struct LinuxSllPayloadSlice<'a> {
6    /// Identifying content of the payload.
7    pub protocol_type: LinuxSllProtocolType,
8
9    /// Payload
10    pub payload: &'a [u8],
11}
12
13impl<'a> From<EtherPayloadSlice<'a>> for LinuxSllPayloadSlice<'a> {
14    fn from(value: EtherPayloadSlice<'a>) -> LinuxSllPayloadSlice<'a> {
15        LinuxSllPayloadSlice {
16            protocol_type: LinuxSllProtocolType::EtherType(value.ether_type),
17            payload: value.payload,
18        }
19    }
20}
21
22impl<'a> TryFrom<LinuxSllPayloadSlice<'a>> for EtherPayloadSlice<'a> {
23    type Error = ();
24
25    fn try_from(value: LinuxSllPayloadSlice<'a>) -> Result<EtherPayloadSlice<'a>, Self::Error> {
26        match value.protocol_type {
27            LinuxSllProtocolType::LinuxNonstandardEtherType(nonstandard_ether_type) => {
28                Ok(EtherPayloadSlice {
29                    ether_type: EtherType(nonstandard_ether_type.into()),
30                    len_source: LenSource::Slice,
31                    payload: value.payload,
32                })
33            }
34            LinuxSllProtocolType::EtherType(ether_type) => Ok(EtherPayloadSlice {
35                ether_type,
36                len_source: LenSource::Slice,
37                payload: value.payload,
38            }),
39            _ => Err(()),
40        }
41    }
42}
43
44#[cfg(test)]
45mod test {
46    use super::*;
47    use alloc::format;
48
49    #[test]
50    fn debug() {
51        let s = LinuxSllPayloadSlice {
52            protocol_type: LinuxSllProtocolType::EtherType(EtherType::IPV4),
53            payload: &[],
54        };
55        assert_eq!(
56            format!(
57                "LinuxSllPayloadSlice {{ protocol_type: {:?}, payload: {:?} }}",
58                s.protocol_type, s.payload
59            ),
60            format!("{:?}", s)
61        );
62    }
63
64    #[test]
65    fn clone_eq() {
66        let s = LinuxSllPayloadSlice {
67            protocol_type: LinuxSllProtocolType::EtherType(EtherType::IPV4),
68            payload: &[],
69        };
70        assert_eq!(s.clone(), s);
71    }
72}