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
59
60
61
62
63
64
#[cfg(test)]
mod uattrs_test;
use crate::attributes::*;
use crate::errors::*;
use crate::message::*;
use util::Error;
use std::fmt;
pub struct UnknownAttributes(pub Vec<AttrType>);
impl fmt::Display for UnknownAttributes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0.is_empty() {
write!(f, "<nil>")
} else {
let mut s = vec![];
for t in &self.0 {
s.push(t.to_string());
}
write!(f, "{}", s.join(", "))
}
}
}
const ATTR_TYPE_SIZE: usize = 2;
impl Setter for UnknownAttributes {
fn add_to(&self, m: &mut Message) -> Result<(), Error> {
let mut v = Vec::with_capacity(ATTR_TYPE_SIZE * 20);
for t in &self.0 {
v.extend_from_slice(&t.value().to_be_bytes());
}
m.add(ATTR_UNKNOWN_ATTRIBUTES, &v);
Ok(())
}
}
impl Getter for UnknownAttributes {
fn get_from(&mut self, m: &Message) -> Result<(), Error> {
let v = m.get(ATTR_UNKNOWN_ATTRIBUTES)?;
if v.len() % ATTR_TYPE_SIZE != 0 {
return Err(ERR_BAD_UNKNOWN_ATTRS_SIZE.clone());
}
self.0.clear();
let mut first = 0usize;
while first < v.len() {
let last = first + ATTR_TYPE_SIZE;
self.0
.push(AttrType(u16::from_be_bytes([v[first], v[first + 1]])));
first = last;
}
Ok(())
}
}