use std::fmt::Display;
pub mod encode;
pub mod scmp;
pub mod udp;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum ProtocolNumber {
Tcp = 6,
Udp = 17,
Hbh = 43,
E2e = 201,
Scmp = 202,
Bfd = 203,
Other(u8),
}
impl ProtocolNumber {
#[inline]
pub fn to_u8(&self) -> u8 {
(*self).into()
}
#[inline]
pub const fn as_str(&self) -> &'static str {
match self {
ProtocolNumber::Tcp => "TCP",
ProtocolNumber::Udp => "UDP",
ProtocolNumber::Hbh => "HBH",
ProtocolNumber::E2e => "E2E",
ProtocolNumber::Scmp => "SCMP",
ProtocolNumber::Bfd => "BFD",
ProtocolNumber::Other(_) => "Other",
}
}
}
impl From<u8> for ProtocolNumber {
#[inline]
fn from(value: u8) -> Self {
match value {
6 => ProtocolNumber::Tcp,
17 => ProtocolNumber::Udp,
43 => ProtocolNumber::Hbh,
201 => ProtocolNumber::E2e,
202 => ProtocolNumber::Scmp,
203 => ProtocolNumber::Bfd,
other => ProtocolNumber::Other(other),
}
}
}
impl From<ProtocolNumber> for u8 {
#[inline]
fn from(value: ProtocolNumber) -> Self {
match value {
ProtocolNumber::Tcp => 6,
ProtocolNumber::Udp => 17,
ProtocolNumber::Hbh => 43,
ProtocolNumber::E2e => 201,
ProtocolNumber::Scmp => 202,
ProtocolNumber::Bfd => 203,
ProtocolNumber::Other(other) => other,
}
}
}
impl Display for ProtocolNumber {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[cfg(feature = "proptest")]
pub mod ptest {
use ::proptest::prelude::*;
use super::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ArbitraryProtocolNumberParams {
pub tcp: u32,
pub udp: u32,
pub hbh: u32,
pub e2e: u32,
pub scmp: u32,
pub bfd: u32,
pub other: u32,
}
impl Default for ArbitraryProtocolNumberParams {
fn default() -> Self {
Self {
tcp: 1,
udp: 1,
hbh: 1,
e2e: 1,
scmp: 1,
bfd: 1,
other: 1,
}
}
}
impl Arbitrary for ProtocolNumber {
type Parameters = ArbitraryProtocolNumberParams;
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(params: Self::Parameters) -> Self::Strategy {
prop_oneof![
params.tcp => Just(ProtocolNumber::Tcp),
params.udp => Just(ProtocolNumber::Udp),
params.hbh => Just(ProtocolNumber::Hbh),
params.e2e => Just(ProtocolNumber::E2e),
params.scmp => Just(ProtocolNumber::Scmp),
params.bfd => Just(ProtocolNumber::Bfd),
params.other => any::<u8>().prop_map(|v| {
let mut v = v;
while !matches!(ProtocolNumber::from(v), ProtocolNumber::Other(_)) {
v = v.wrapping_add(1);
}
ProtocolNumber::Other(v)
}),
]
.boxed()
}
}
}