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;
#[derive(PartialEq, Debug, Clone)]
pub enum Value {
Constructed(Vec<Tlv>),
Primitive(Vec<u8>),
}
impl Value {
#[must_use]
pub fn is_constructed(&self) -> bool {
match self {
Self::Constructed(_) => true,
_ => false,
}
}
#[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()),
}
}
pub fn push(&mut self, tlv: Tlv) -> Result<()> {
match self {
Self::Constructed(t) => {
t.push(tlv);
Ok(())
}
_ => Err(TlvError::Inconsistant),
}
}
}