domain/base/opt/macros.rs
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
//! 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 --------------------------------------------
#[derive(Clone)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize),
serde(bound(
serialize = "
Octs: AsRef<[u8]> + octseq::serde::SerializeOctets,
Name: serde::Serialize,
",
))
)]
#[non_exhaustive]
pub enum AllOptData<Octs, Name> {
$( $(
$opt($module::$opt $( < $( $octets ),* > )? ),
)* )*
Other(UnknownOptData<Octs>),
}
//--- OctetsFrom
impl<Octs, Name, SrcOcts, SrcName>
OctetsFrom<AllOptData<SrcOcts, SrcName>>
for AllOptData<Octs, Name>
where
Octs: OctetsFrom<SrcOcts>,
Name: OctetsFrom<SrcName, Error = Octs::Error>,
{
type Error = Octs::Error;
fn try_octets_from(
source: AllOptData<SrcOcts, SrcName>,
) -> Result<Self, Self::Error> {
match source {
$( $(
AllOptData::$opt(opt) => {
Ok(AllOptData::$opt(
$module::$opt::try_octets_from(opt)?
))
},
)* )*
AllOptData::Other(opt) => {
Ok(AllOptData::Other(
UnknownOptData::try_octets_from(opt)?
))
}
}
}
}
//--- From
$( $(
impl<Octs, Name> From<$opt $( < $( $octets> ),* )?>
for AllOptData<Octs, Name> {
fn from(
value: $module::$opt$( < $( $octets ),* > )*
) -> Self {
AllOptData::$opt(value)
}
}
)* )*
//--- OptData
impl<Octs, Name> OptData for AllOptData<Octs, Name> {
fn code(&self) -> OptionCode {
match *self {
$( $(
AllOptData::$opt(_) => $module::$opt::CODE,
)* )*
AllOptData::Other(ref inner) => inner.code(),
}
}
}
impl<'a, Octs: Octets> ParseOptData<'a, Octs>
for AllOptData<Octs::Range<'a>, Name<Octs::Range<'a>>> {
fn parse_option(
code: OptionCode,
parser: &mut Parser<'a, Octs>,
) -> Result<Option<Self>, ParseError> {
match code {
$( $(
$module::$opt::CODE => {
Ok(Some(AllOptData::$opt(
$opt::parse(parser)?
)))
}
)* )*
_ => {
Ok(UnknownOptData::parse_option(
code, parser
)?.map(AllOptData::Other))
}
}
}
}
impl<Octs, Name> ComposeOptData for AllOptData<Octs, Name>
where Octs: AsRef<[u8]>, Name: ToName {
fn compose_len(&self) -> u16 {
match *self {
$( $(
AllOptData::$opt(ref inner) => inner.compose_len(),
)* )*
AllOptData::Other(ref inner) => inner.compose_len(),
}
}
fn compose_option<Target: octseq::builder::OctetsBuilder + ?Sized>(
&self, target: &mut Target
) -> Result<(), Target::AppendError> {
match *self {
$( $(
AllOptData::$opt(ref inner) => {
inner.compose_option(target)
}
)* )*
AllOptData::Other(ref inner) => {
inner.compose_option(target)
}
}
}
}
//--- Debug
impl<Octs, Name> fmt::Debug for AllOptData<Octs, Name>
where Octs: AsRef<[u8]>, Name: fmt::Display {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
$( $(
AllOptData::$opt(ref inner) => {
fmt::Debug::fmt(inner, f)
}
)* )*
AllOptData::Other(ref inner) => {
fmt::Debug::fmt(inner, f)
}
}
}
}
}
}