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
use super::Tlv;
use crate::error::TlvError;
use crate::Result;

use alloc::vec::Vec;

/// Value definition of BER-TLV data
#[derive(PartialEq, Debug, Clone)]
pub enum Value {
  /// constructed data object, i.e., the value is encoded in BER-TLV
  Constructed(Vec<Tlv>),
  /// primitive data object, i.e., the value is not encoded in BER-TLV
  /// (may be empty)
  Primitive(Vec<u8>),
}

impl Value {
  /// Wether the value is constructed or not
  #[must_use]
  pub fn is_constructed(&self) -> bool {
    match self {
      Self::Constructed(_) => true,
      _ => false,
    }
  }

  /// Get value length once serialized into BER-TLV data
  #[must_use]
  pub fn len_as_bytes(&self) -> usize {
    match &self {
      Self::Primitive(v) => v.len(),
      Self::Constructed(tlv) => tlv.iter().fold(0, |sum, x| sum + x.len()),
    }
  }

  /// Append a BER-TLV data object.
  /// # Errors
  /// Fails with `TlvError::Inconsistant` on primitive or empty values.
  pub fn push(&mut self, tlv: Tlv) -> Result<()> {
    match self {
      Self::Constructed(t) => {
        t.push(tlv);
        Ok(())
      }
      _ => Err(TlvError::Inconsistant),
    }
  }
}