asn1_rs/to_ber/
constructed.rs

1use std::io;
2use std::io::Write;
3
4use crate::{BerEncoder, Class, Tag};
5
6/// Encoder for constructed objects, with *Definite* length
7#[allow(missing_debug_implementations)]
8pub struct Constructed {}
9
10impl Constructed {
11    pub const fn new() -> Self {
12        Self {}
13    }
14}
15
16impl Default for Constructed {
17    fn default() -> Self {
18        Self::new()
19    }
20}
21
22impl BerEncoder for Constructed {
23    fn new() -> Self {
24        Constructed::new()
25    }
26
27    fn write_tag_info<W: Write>(
28        &mut self,
29        class: Class,
30        _constructed: bool,
31        tag: Tag,
32        target: &mut W,
33    ) -> Result<usize, io::Error> {
34        self.write_tag_generic(class, true, tag, target)
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use hex_literal::hex;
41    // use nom::HexDisplay;
42
43    use crate::ToBer;
44
45    #[test]
46    fn tober_constructed_vec() {
47        let mut v: Vec<u8> = Vec::new();
48
49        let value = vec![true, false];
50        value.ber_encode(&mut v).expect("serialization failed");
51        // eprintln!("encoding for {:?}:\n{}", &value, v.to_hex(16));
52        assert_eq!(&v, &hex!("30 06 0101ff 010100"));
53
54        v.clear();
55    }
56}