xref: /webrtc/sctp/src/param/param_unknown.rs (revision ffe74184)
1 use crate::param::param_header::ParamHeader;
2 use crate::param::param_header::PARAM_HEADER_LENGTH;
3 use crate::param::param_type::ParamType;
4 use crate::param::Param;
5 use bytes::{Bytes, BytesMut};
6 use std::any::Any;
7 use std::fmt::{Debug, Display, Formatter};
8 
9 /// This type is meant to represent ANY parameter for un/remarshaling purposes, where we do not have a more specific type for it.
10 /// This means we do not really understand the semantics of the param but can represent it.
11 ///
12 /// This is useful for usage in e.g.`ParamUnrecognized` where we want to report some unrecognized params back to the sender.
13 #[derive(Clone, Debug, PartialEq, Eq)]
14 pub struct ParamUnknown {
15     typ: u16,
16     value: Bytes,
17 }
18 
19 impl Display for ParamUnknown {
fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result20     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21         write!(f, "ParamUnknown( {} {:?} )", self.header(), self.value)
22     }
23 }
24 
25 impl Param for ParamUnknown {
header(&self) -> ParamHeader26     fn header(&self) -> ParamHeader {
27         ParamHeader {
28             typ: ParamType::Unknown {
29                 param_type: self.typ,
30             },
31             value_length: self.value.len() as u16,
32         }
33     }
34 
as_any(&self) -> &(dyn Any + Send + Sync)35     fn as_any(&self) -> &(dyn Any + Send + Sync) {
36         self
37     }
38 
unmarshal(raw: &Bytes) -> crate::error::Result<Self> where Self: Sized,39     fn unmarshal(raw: &Bytes) -> crate::error::Result<Self>
40     where
41         Self: Sized,
42     {
43         let header = ParamHeader::unmarshal(raw)?;
44         let value = raw.slice(PARAM_HEADER_LENGTH..PARAM_HEADER_LENGTH + header.value_length());
45         Ok(Self {
46             typ: header.typ.into(),
47             value,
48         })
49     }
50 
marshal_to(&self, buf: &mut BytesMut) -> crate::error::Result<usize>51     fn marshal_to(&self, buf: &mut BytesMut) -> crate::error::Result<usize> {
52         self.header().marshal_to(buf)?;
53         buf.extend(self.value.clone());
54         Ok(buf.len())
55     }
56 
value_length(&self) -> usize57     fn value_length(&self) -> usize {
58         self.value.len()
59     }
60 
clone_to(&self) -> Box<dyn Param + Send + Sync>61     fn clone_to(&self) -> Box<dyn Param + Send + Sync> {
62         Box::new(self.clone())
63     }
64 }
65