statime/datastructures/common/
clock_accuracy.rs1use core::cmp::Ordering;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5pub enum ClockAccuracy {
8 Reserved,
10 PS1,
12 PS2_5,
14 PS10,
16 PS25,
18 PS100,
20 PS250,
22 NS1,
24 NS2_5,
26 NS10,
28 NS25,
30 NS100,
32 NS250,
34 US1,
36 US2_5,
38 US10,
40 US25,
42 US100,
44 US250,
46 MS1,
48 MS2_5,
50 MS10,
52 MS25,
54 MS100,
56 MS250,
58 S1,
60 S10,
62 SGT10,
64 ProfileSpecific(u8),
66 Unknown,
68}
69
70impl ClockAccuracy {
71 pub fn to_primitive(self) -> u8 {
73 match self {
74 Self::Reserved => 0x00,
75 Self::PS1 => 0x17,
76 Self::PS2_5 => 0x18,
77 Self::PS10 => 0x19,
78 Self::PS25 => 0x1a,
79 Self::PS100 => 0x1b,
80 Self::PS250 => 0x1c,
81 Self::NS1 => 0x1d,
82 Self::NS2_5 => 0x1e,
83 Self::NS10 => 0x1f,
84 Self::NS25 => 0x20,
85 Self::NS100 => 0x21,
86 Self::NS250 => 0x22,
87 Self::US1 => 0x23,
88 Self::US2_5 => 0x24,
89 Self::US10 => 0x25,
90 Self::US25 => 0x26,
91 Self::US100 => 0x27,
92 Self::US250 => 0x28,
93 Self::MS1 => 0x29,
94 Self::MS2_5 => 0x2a,
95 Self::MS10 => 0x2b,
96 Self::MS25 => 0x2c,
97 Self::MS100 => 0x2d,
98 Self::MS250 => 0x2e,
99 Self::S1 => 0x2f,
100 Self::S10 => 0x30,
101 Self::SGT10 => 0x31,
102 Self::ProfileSpecific(value) => 0x80 + value,
103 Self::Unknown => 0xfe,
104 }
105 }
106
107 pub(crate) fn from_primitive(value: u8) -> Self {
108 match value {
109 0x00..=0x16 | 0x32..=0x7f | 0xff => Self::Reserved,
110 0x17 => Self::PS1,
111 0x18 => Self::PS2_5,
112 0x19 => Self::PS10,
113 0x1a => Self::PS25,
114 0x1b => Self::PS100,
115 0x1c => Self::PS250,
116 0x1d => Self::NS1,
117 0x1e => Self::NS2_5,
118 0x1f => Self::NS10,
119 0x20 => Self::NS25,
120 0x21 => Self::NS100,
121 0x22 => Self::NS250,
122 0x23 => Self::US1,
123 0x24 => Self::US2_5,
124 0x25 => Self::US10,
125 0x26 => Self::US25,
126 0x27 => Self::US100,
127 0x28 => Self::US250,
128 0x29 => Self::MS1,
129 0x2a => Self::MS2_5,
130 0x2b => Self::MS10,
131 0x2c => Self::MS25,
132 0x2d => Self::MS100,
133 0x2e => Self::MS250,
134 0x2f => Self::S1,
135 0x30 => Self::S10,
136 0x31 => Self::SGT10,
137 0x80..=0xfd => Self::ProfileSpecific(value - 0x80),
138 0xfe => ClockAccuracy::Unknown,
139 }
140 }
141
142 pub(crate) fn cmp_numeric(&self, other: &Self) -> Ordering {
144 self.to_primitive().cmp(&other.to_primitive())
145 }
146}
147
148impl Default for ClockAccuracy {
149 fn default() -> Self {
150 Self::Unknown
151 }
152}
153
154#[cfg(test)]
155mod tests {
156 use super::*;
157
158 #[test]
159 fn network_protocol_values() {
160 for i in 0..u8::MAX {
161 let protocol = ClockAccuracy::from_primitive(i);
162 if !matches!(protocol, ClockAccuracy::Reserved) {
163 assert_eq!(protocol.to_primitive(), i);
164 }
165 }
166
167 assert_eq!(ClockAccuracy::ProfileSpecific(5).to_primitive(), 0x85);
168 }
169
170 #[test]
171 fn ordering() {
172 let a = ClockAccuracy::PS1;
174 let b = ClockAccuracy::PS10;
175
176 assert_eq!(a.cmp_numeric(&b), Ordering::Less);
177 }
178}