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
use crate::fields::{HexString, HexBytes};
use crate::{get_byteorder, int_to_vec};


#[inline]
fn decode_hex(input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>, length: usize) {
    if let Some(fr) = fattr && let Some(byte_count) = fr.byte_count {
        input.extend(int_to_vec(length, byte_count, &get_byteorder(cattr, fattr)));
    }
}


impl crate::ByteEncode for HexString {
    fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>) {        
        decode_hex(input, cattr, fattr, self.len());
        input.extend_from_slice(self);
    }
}


impl crate::BorrowByteEncode for HexString {
    fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>) {
        decode_hex(input, cattr, fattr, self.len());
        input.extend_from_slice(self);
    }
}


impl<'da> crate::BorrowByteEncode for HexBytes<'da> {
    fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>) {
        decode_hex(input, cattr, fattr, self.len());
        input.extend_from_slice(self);
    }
}


#[cfg(test)]
mod tests {
    use std::str::FromStr;
    use crate::ByteEncode;
    use crate::FieldAttrModifiers;
    use super::*;

    #[test]
    fn test_encode_hex() {
        let fattr = FieldAttrModifiers { length: Some(3), ..Default::default() };
        let value = HexString::from_str("000102").unwrap();
        let mut buf = Vec::new();
        value.encode(&mut buf, None, Some(&fattr));
        assert_eq!(buf, b"\x00\x01\x02");

        let fattr = FieldAttrModifiers { byte_count: Some(1), ..Default::default() };
        let value = HexString::from_str("000102").unwrap();
        let mut buf = Vec::new();
        value.encode(&mut buf, None, Some(&fattr));
        assert_eq!(buf, b"\x03\x00\x01\x02");
    }
}