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