Skip to content
File

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

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