1 use crate::param::param_header::PARAM_HEADER_LENGTH; 2 use crate::param::param_type::ParamType; 3 use crate::param::ParamHeader; 4 use crate::param::{build_param, Param}; 5 use bytes::{Bytes, BytesMut}; 6 use std::any::Any; 7 use std::fmt::{Debug, Display, Formatter}; 8 9 /// This is the parameter type used to report unrecognized parameters in e.g. init chunks back to the sender in the init ack. 10 /// The contained param is likely to be a `ParamUnknown` but might be something more specific. 11 #[derive(Clone, Debug)] 12 pub struct ParamUnrecognized { 13 param: Box<dyn Param + Send + Sync>, 14 } 15 16 impl ParamUnrecognized { wrap(param: Box<dyn Param + Send + Sync>) -> Self17 pub(crate) fn wrap(param: Box<dyn Param + Send + Sync>) -> Self { 18 Self { param } 19 } 20 } 21 22 impl Display for ParamUnrecognized { fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result23 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { 24 f.write_str("UnrecognizedParam")?; 25 Display::fmt(&self.param, f) 26 } 27 } 28 29 impl Param for ParamUnrecognized { header(&self) -> ParamHeader30 fn header(&self) -> ParamHeader { 31 ParamHeader { 32 typ: ParamType::UnrecognizedParam, 33 value_length: self.value_length() as u16, 34 } 35 } 36 as_any(&self) -> &(dyn Any + Send + Sync)37 fn as_any(&self) -> &(dyn Any + Send + Sync) { 38 self 39 } 40 unmarshal(raw: &Bytes) -> crate::error::Result<Self> where Self: Sized,41 fn unmarshal(raw: &Bytes) -> crate::error::Result<Self> 42 where 43 Self: Sized, 44 { 45 let header = ParamHeader::unmarshal(raw)?; 46 let raw_param = raw.slice(PARAM_HEADER_LENGTH..PARAM_HEADER_LENGTH + header.value_length()); 47 let param = build_param(&raw_param)?; 48 Ok(Self { param }) 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 self.param.marshal_to(buf)?; 54 Ok(buf.len()) 55 } 56 value_length(&self) -> usize57 fn value_length(&self) -> usize { 58 self.param.value_length() + PARAM_HEADER_LENGTH 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