1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::param::param_header::PARAM_HEADER_LENGTH;
use crate::param::param_type::ParamType;
use crate::param::ParamHeader;
use crate::param::{build_param, Param};
use bytes::{Bytes, BytesMut};
use std::any::Any;
use std::fmt::{Debug, Display, Formatter};

/// This is the parameter type used to report unrecognized parameters in e.g. init chunks back to the sender in the init ack.
/// The contained param is likely to be a `ParamUnknown` but might be something more specific.
#[derive(Clone, Debug)]
pub struct ParamUnrecognized {
    param: Box<dyn Param + Send + Sync>,
}

impl ParamUnrecognized {
    pub(crate) fn wrap(param: Box<dyn Param + Send + Sync>) -> Self {
        Self { param }
    }
}

impl Display for ParamUnrecognized {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str("UnrecognizedParam")?;
        Display::fmt(&self.param, f)
    }
}

impl Param for ParamUnrecognized {
    fn header(&self) -> ParamHeader {
        ParamHeader {
            typ: ParamType::UnrecognizedParam,
            value_length: self.value_length() as u16,
        }
    }

    fn as_any(&self) -> &(dyn Any + Send + Sync) {
        self
    }

    fn unmarshal(raw: &Bytes) -> crate::error::Result<Self>
    where
        Self: Sized,
    {
        let header = ParamHeader::unmarshal(raw)?;
        let raw_param = raw.slice(PARAM_HEADER_LENGTH..PARAM_HEADER_LENGTH + header.value_length());
        let param = build_param(&raw_param)?;
        Ok(Self { param })
    }

    fn marshal_to(&self, buf: &mut BytesMut) -> crate::error::Result<usize> {
        self.header().marshal_to(buf)?;
        self.param.marshal_to(buf)?;
        Ok(buf.len())
    }

    fn value_length(&self) -> usize {
        self.param.value_length() + PARAM_HEADER_LENGTH
    }

    fn clone_to(&self) -> Box<dyn Param + Send + Sync> {
        Box::new(self.clone())
    }
}