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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::net::{Ipv4Addr, Ipv6Addr, IpAddr};
use crate::fields::MacAddress;
use crate::{ByteOrder, get_byteorder};


impl crate::ByteEncode for MacAddress {
    #[inline]
    fn encode(&self, input: &mut Vec<u8>, _cattr: Option<&crate::ContainerAttrModifiers>, _fattr: Option<&crate::FieldAttrModifiers>) {        
        input.extend_from_slice(self);
    }
}


impl crate::BorrowByteEncode for MacAddress {
    #[inline]
    fn encode(&self, input: &mut Vec<u8>, _cattr: Option<&crate::ContainerAttrModifiers>, _fattr: Option<&crate::FieldAttrModifiers>) {
        input.extend_from_slice(self);
    }
}


impl crate::ByteEncode for Ipv4Addr {
    #[inline]
    fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>) {        
        let byteorder = get_byteorder(cattr, fattr);

        if byteorder == ByteOrder::Be {
            input.extend(self.to_bits().to_be_bytes());            
        }
        else {
            input.extend(self.to_bits().to_le_bytes());
        }
    }
}


impl crate::BorrowByteEncode for Ipv4Addr {
    #[inline]
    fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>) {
        let byteorder = get_byteorder(cattr, fattr);

        if byteorder == ByteOrder::Be {
            input.extend(self.to_bits().to_be_bytes());            
        }
        else {
            input.extend(self.to_bits().to_le_bytes());
        }
    }
}


impl crate::ByteEncode for Ipv6Addr {
    #[inline]
    fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>) {        
        let byteorder = get_byteorder(cattr, fattr);

        if byteorder == ByteOrder::Be {
            input.extend(self.to_bits().to_be_bytes());            
        }
        else {
            input.extend(self.to_bits().to_le_bytes());
        }
    }
}


impl crate::BorrowByteEncode for Ipv6Addr {
    #[inline]
    fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>) {
        let byteorder = get_byteorder(cattr, fattr);

        if byteorder == ByteOrder::Be {
            input.extend(self.to_bits().to_be_bytes());            
        }
        else {
            input.extend(self.to_bits().to_le_bytes());
        }
    }
}


impl crate::ByteEncode for IpAddr {
    #[inline]
    fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>) {        
        match self {
            Self::V4(v) => v.encode(input, cattr, fattr),
            Self::V6(v) => v.encode(input, cattr, fattr),
        }
    }
}


impl crate::BorrowByteEncode for IpAddr {
    #[inline]
    fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>) {
        match self {
            Self::V4(v) => v.encode(input, cattr, fattr),
            Self::V6(v) => v.encode(input, cattr, fattr),
        }
    }
}


#[cfg(test)]
mod test {
    use crate::{encode::ByteEncode, FieldAttrModifiers};
    use super::*;

    #[test]
    fn test_encode_mac_address() {
        let mut buf = vec![];
        let value = MacAddress::from_bits(0x1234567890ff);
        value.encode(&mut buf, None, None);
        assert_eq!(buf, [0x12, 0x34, 0x56, 0x78, 0x90, 0xff]);
        assert_eq!(value.to_bits(), 0x1234567890ff);
    }

    #[test]
    fn test_encode_ip_address() {
        let mut buf = vec![];
        let value = Ipv4Addr::from_bits(0x12345678);
        value.encode(&mut buf, None, None);
        assert_eq!(buf, [0x12, 0x34, 0x56, 0x78]);

        let mut buf = vec![];
        let value = Ipv4Addr::from_bits(0x12345678);
        let fattr = FieldAttrModifiers { byteorder: Some(ByteOrder::Le), ..Default::default() };
        value.encode(&mut buf, None, Some(&fattr));
        assert_eq!(buf, [0x78, 0x56, 0x34, 0x12]);

        let mut buf = vec![];
        let value = IpAddr::V4(Ipv4Addr::from_bits(0x12345678));
        let fattr = FieldAttrModifiers { byteorder: Some(ByteOrder::Le), ..Default::default() };
        value.encode(&mut buf, None, Some(&fattr));
        assert_eq!(buf, [0x78, 0x56, 0x34, 0x12]);
    }
}