Skip to main content

flashbang/message/attributes/
unknown_attributes.rs

1use super::*;
2
3/// The UNKNOWN-ATTRIBUTES attribute.
4///
5/// Contains a list of attribute types that were not
6/// understood by the server.
7///
8/// See [RFC8489 Section 14.13](https://datatracker.ietf.org/doc/html/rfc8489#section-14.13) for more details.
9pub struct UnknownAttributes {
10    attributes: Vec<u16>,
11}
12
13impl Attribute for UnknownAttributes {
14    const TY: u16 = 0x000A;
15
16    const SIZE: usize = 0;
17
18    fn encode(&self, buf: &mut [u8], offset: usize) {
19        for (i, attr) in self.attributes.iter().enumerate() {
20            let i = i * 2;
21            buf[(i + offset)..(i + 1 + offset)].copy_from_slice(&attr.to_be_bytes());
22        }
23    }
24
25    fn decode(buf: &[u8], meta: &AttributeMeta) -> Self {
26        if meta.len % 2 != 0 {
27            panic!("Each attribute type must be two bytes"); // TODO: Remove panic
28        }
29
30        let mut attributes = vec!();
31
32        for i in (meta.offset..(meta.offset + meta.len)).step_by(2) {
33            attributes.push(u16::from_be_bytes(buf[i..(i+1)].try_into().unwrap()));
34        }
35
36        Self {
37            attributes,
38        }
39    }
40
41    fn size(&self) -> usize {
42        self.attributes.len() * 2
43    }
44}