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
use strum_macros::{Display, EnumCount, FromRepr};
#[repr(u8)]
#[derive(Eq, PartialEq, Copy, Clone, Debug, Display, EnumCount, FromRepr)]
pub enum TsPacketKind {
Connect,
ConnectionEstablished,
Event,
Ack,
Other(u8),
}
impl From<TsPacketKind> for u8 {
fn from(kind: TsPacketKind) -> Self {
match kind {
TsPacketKind::Connect => 1,
TsPacketKind::ConnectionEstablished => 2,
TsPacketKind::Event => 3,
TsPacketKind::Ack => 4,
TsPacketKind::Other(x) => x,
}
}
}
impl From<&TsPacketKind> for u8 {
fn from(pkt: &TsPacketKind) -> Self {
u8::from(*pkt)
}
}
impl From<u8> for TsPacketKind {
fn from(value: u8) -> Self {
match value {
x if x == Self::Connect => Self::Connect,
x if x == Self::ConnectionEstablished => Self::ConnectionEstablished,
x if x == Self::Event => Self::Event,
x if x == Self::Ack => Self::Ack,
x => Self::Other(x),
}
}
}
impl PartialEq<u8> for TsPacketKind {
fn eq(&self, other: &u8) -> bool {
u8::from(self) == *other
}
}
impl PartialEq<TsPacketKind> for u8 {
fn eq(&self, other: &TsPacketKind) -> bool {
u8::from(other) == *self
}
}
impl std::fmt::LowerHex for TsPacketKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let val: u8 = self.into();
std::fmt::LowerHex::fmt(&val, f)
}
}
impl std::fmt::UpperHex for TsPacketKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let val: u8 = self.into();
std::fmt::UpperHex::fmt(&val, f)
}
}
#[cfg(test)]
mod test {
use super::TsPacketKind;
use std::collections::HashSet;
use strum::EnumCount;
#[test]
fn ts_kind_roundtrip() {
let mut seen = HashSet::new();
for v in 0..=u8::MAX {
let m = TsPacketKind::from(v);
seen.insert(std::mem::discriminant(&m));
assert_eq!(u8::from(m), v);
}
assert_eq!(seen.len(), TsPacketKind::COUNT)
}
}