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
use std::cmp::Ordering;
use std::fmt::{self, Display, Formatter};
use std::io;
use crate::{ReadStruct, VariantName, WriteStruct, STRICT_TYPES_LIB};
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))]
pub struct Sizing {
pub min: u16,
pub max: u16,
}
impl_strict_struct!(Sizing, STRICT_TYPES_LIB; min, max);
impl Sizing {
pub const ONE: Sizing = Sizing { min: 1, max: 1 };
pub const U8: Sizing = Sizing {
min: 0,
max: u8::MAX as u16,
};
pub const U16: Sizing = Sizing {
min: 0,
max: u16::MAX,
};
pub const U8_NONEMPTY: Sizing = Sizing {
min: 1,
max: u8::MAX as u16,
};
pub const U16_NONEMPTY: Sizing = Sizing {
min: 1,
max: u16::MAX,
};
pub const fn new(min: u16, max: u16) -> Self { Sizing { min, max } }
pub const fn fixed(len: u16) -> Self { Sizing { min: len, max: len } }
}
impl Display for Sizing {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match (self.min, self.max) {
(0, u16::MAX) => Ok(()),
(0, max) => write!(f, " ^ ..{}", max),
(min, u16::MAX) => write!(f, " ^ {}..", min),
(min, max) => write!(f, " ^ {}..{:#04x}", min, max),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))]
pub struct Variant {
pub name: VariantName,
pub tag: u8,
}
impl_strict_struct!(Variant, STRICT_TYPES_LIB; name, tag);
impl Variant {
pub fn named(tag: u8, name: VariantName) -> Variant { Variant { name, tag } }
pub fn none() -> Variant {
Variant {
name: vname!("none"),
tag: 0,
}
}
pub fn some() -> Variant {
Variant {
name: vname!("some"),
tag: 1,
}
}
}
impl PartialOrd for Variant {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }
}
impl Ord for Variant {
fn cmp(&self, other: &Self) -> Ordering {
if self == other {
return Ordering::Equal;
}
self.tag.cmp(&other.tag)
}
}
impl Display for Variant {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name)?;
if f.alternate() {
f.write_str(" = ")?;
Display::fmt(&self.tag, f)?;
}
Ok(())
}
}