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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! Macros for option types.
//!
//! These macros are only used to generate enums in the parent module.
//! They are here in a separate module to keep the parent tidy.
macro_rules! opt_types {
( $(
$module:ident::{
$( $opt:ident $( <$octets:ident> )* ),*
};
)* ) => {
$( $( pub use self::$module::$opt; )* )*
$( pub mod $module; )*
//------------ AllOptData --------------------------------------------
// TODO Impl Debug.
#[derive(Clone)]
#[non_exhaustive]
pub enum AllOptData<Octets> {
$( $(
$opt($module::$opt $( <$octets> )* ),
)* )*
Other(UnknownOptData<Octets>),
}
//--- From
$( $(
impl<Octets> From<$opt $( <$octets> )*> for AllOptData<Octets> {
fn from(value: $module::$opt$( <$octets> )*) -> Self {
AllOptData::$opt(value)
}
}
)* )*
//--- Compose
impl<Octets: AsRef<[u8]>> Compose for AllOptData<Octets> {
fn compose<T: $crate::base::octets::OctetsBuilder + AsMut<[u8]>>(
&self, target: &mut T
) -> Result<(), ShortBuf> {
match *self {
$( $(
AllOptData::$opt(ref inner) => inner.compose(target),
)* )*
AllOptData::Other(ref inner) => inner.compose(target),
}
}
}
//--- OptData
impl<Octets: AsRef<[u8]>> OptData for AllOptData<Octets> {
fn code(&self) -> OptionCode {
match *self {
$( $(
AllOptData::$opt(_) => OptionCode::$opt,
)* )*
AllOptData::Other(ref inner) => inner.code(),
}
}
}
impl<Ref: OctetsRef> ParseOptData<Ref> for AllOptData<Ref::Range> {
fn parse_option(
code: OptionCode,
parser: &mut Parser<Ref>,
) -> Result<Option<Self>, ParseError> {
match code {
$( $(
OptionCode::$opt => {
Ok(Some(AllOptData::$opt(
$opt::parse(parser)?
)))
}
)* )*
_ => {
Ok(UnknownOptData::parse_option(
code, parser
)?.map(AllOptData::Other))
}
}
}
}
}
}