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 ParamSupportedExtensions {
8     pub(crate) chunk_types: Vec<ChunkType>,
9 }
10 
11 impl fmt::Display for ParamSupportedExtensions {
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 ParamSupportedExtensions {
header(&self) -> ParamHeader27     fn header(&self) -> ParamHeader {
28         ParamHeader {
29             typ: ParamType::SupportedExt,
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         let reader =
38             &mut raw.slice(PARAM_HEADER_LENGTH..PARAM_HEADER_LENGTH + header.value_length());
39 
40         let mut chunk_types = vec![];
41         while reader.has_remaining() {
42             chunk_types.push(ChunkType(reader.get_u8()));
43         }
44 
45         Ok(ParamSupportedExtensions { chunk_types })
46     }
47 
marshal_to(&self, buf: &mut BytesMut) -> Result<usize>48     fn marshal_to(&self, buf: &mut BytesMut) -> Result<usize> {
49         self.header().marshal_to(buf)?;
50         for ct in &self.chunk_types {
51             buf.put_u8(ct.0);
52         }
53         Ok(buf.len())
54     }
55 
value_length(&self) -> usize56     fn value_length(&self) -> usize {
57         self.chunk_types.len()
58     }
59 
clone_to(&self) -> Box<dyn Param + Send + Sync>60     fn clone_to(&self) -> Box<dyn Param + Send + Sync> {
61         Box::new(self.clone())
62     }
63 
as_any(&self) -> &(dyn Any + Send + Sync)64     fn as_any(&self) -> &(dyn Any + Send + Sync) {
65         self
66     }
67 }
68