File
Blob: firmware/vendor/sctp-proto/src/param/param_chunk_list.rs
| 1 | use super::{param_header::*, param_type::*, *}; |
| 2 | use crate::chunk::chunk_type::*; |
| 3 | |
| 4 | use alloc::string::{String, ToString}; |
| 5 | use alloc::vec::Vec; |
| 6 | use bytes::BufMut; |
| 7 | |
| 8 | #[derive(Default, Debug, Clone, PartialEq)] |
| 9 | pub(crate) struct ParamChunkList { |
| 10 | pub(crate) chunk_types: Vec<ChunkType>, |
| 11 | } |
| 12 | |
| 13 | impl fmt::Display for ParamChunkList { |
| 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 | |
| 28 | impl Param for ParamChunkList { |
| 29 | fn header(&self) -> ParamHeader { |
| 30 | ParamHeader { |
| 31 | typ: ParamType::ChunkList, |
| 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 | if header.typ != ParamType::ChunkList { |
| 40 | return Err(Error::ErrParamTypeUnexpected); |
| 41 | } |
| 42 | |
| 43 | let reader = |
| 44 | &mut raw.slice(PARAM_HEADER_LENGTH..PARAM_HEADER_LENGTH + header.value_length()); |
| 45 | |
| 46 | let mut chunk_types = vec![]; |
| 47 | while reader.has_remaining() { |
| 48 | chunk_types.push(ChunkType(reader.get_u8())); |
| 49 | } |
| 50 | |
| 51 | Ok(ParamChunkList { chunk_types }) |
| 52 | } |
| 53 | |
| 54 | fn marshal_to(&self, buf: &mut BytesMut) -> Result<usize> { |
| 55 | self.header().marshal_to(buf)?; |
| 56 | for ct in &self.chunk_types { |
| 57 | buf.put_u8(ct.0); |
| 58 | } |
| 59 | Ok(buf.len()) |
| 60 | } |
| 61 | |
| 62 | fn value_length(&self) -> usize { |
| 63 | self.chunk_types.len() |
| 64 | } |
| 65 | |
| 66 | fn clone_to(&self) -> Box<dyn Param + Send + Sync> { |
| 67 | Box::new(self.clone()) |
| 68 | } |
| 69 | |
| 70 | fn as_any(&self) -> &(dyn Any + Send + Sync) { |
| 71 | self |
| 72 | } |
| 73 | } |