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
use crate::parser::bmp::error::ParserBmpError;
use crate::num_traits::FromPrimitive;
use crate::parser::DataBytes;
#[derive(Debug)]
pub struct TerminationMessage {
pub tlvs: Vec<TerminationTlv>
}
#[derive(Debug)]
pub struct TerminationTlv {
pub info_type: TerminationTlvType,
pub info_len: u16,
pub info: String
}
#[derive(Debug, Primitive)]
pub enum TerminationTlvType {
String=0,
Reason=1,
}
pub fn parse_termination_message(reader: &mut DataBytes) -> Result<TerminationMessage, ParserBmpError> {
let mut tlvs = vec![];
while reader.bytes_left() > 4 {
let info_type: TerminationTlvType = TerminationTlvType::from_u16(reader.read_16b()?).unwrap();
let info_len = reader.read_16b()?;
if reader.bytes_left() < info_len as usize {
break
}
let info = reader.read_n_bytes_to_string(info_len as usize)?;
tlvs.push(
TerminationTlv {
info_type,
info_len,
info,
}
)
}
Ok(TerminationMessage{ tlvs })
}