Skip to content
File

Blob: firmware/vendor/sctp-proto/src/param/param_supported_extensions.rs

rust70 lines
1use super::{param_header::*, param_type::*, *};
2use crate::chunk::chunk_type::*;
3 
4use alloc::string::{String, ToString};
5use alloc::vec::Vec;
6use bytes::BufMut;
7 
8#[derive(Default, Debug, Clone, PartialEq)]
9pub(crate) struct ParamSupportedExtensions {
10 pub(crate) chunk_types: Vec<ChunkType>,
11}
12 
13impl fmt::Display for ParamSupportedExtensions {
14 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15 write!(
16 f,
17 "{} {}",
18 self.header(),
19 self.chunk_types
20 .iter()
21 .map(|ct| ct.to_string())
22 .collect::<Vec<String>>()
23 .join(" "),
24 )
25 }
26}
27 
28impl Param for ParamSupportedExtensions {
29 fn header(&self) -> ParamHeader {
30 ParamHeader {
31 typ: ParamType::SupportedExt,
32 value_length: self.value_length() as u16,
33 }
34 }
35 
36 fn unmarshal(raw: &Bytes) -> Result<Self> {
37 let header = ParamHeader::unmarshal(raw)?;
38 
39 let reader =
40 &mut raw.slice(PARAM_HEADER_LENGTH..PARAM_HEADER_LENGTH + header.value_length());
41 
42 let mut chunk_types = vec![];
43 while reader.has_remaining() {
44 chunk_types.push(ChunkType(reader.get_u8()));
45 }
46 
47 Ok(ParamSupportedExtensions { chunk_types })
48 }
49 
50 fn marshal_to(&self, buf: &mut BytesMut) -> Result<usize> {
51 self.header().marshal_to(buf)?;
52 for ct in &self.chunk_types {
53 buf.put_u8(ct.0);
54 }
55 Ok(buf.len())
56 }
57 
58 fn value_length(&self) -> usize {
59 self.chunk_types.len()
60 }
61 
62 fn clone_to(&self) -> Box<dyn Param + Send + Sync> {
63 Box::new(self.clone())
64 }
65 
66 fn as_any(&self) -> &(dyn Any + Send + Sync) {
67 self
68 }
69}