statime/datastructures/common/
clock_quality.rs1use super::clock_accuracy::ClockAccuracy;
2use crate::datastructures::{WireFormat, WireFormatError};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct ClockQuality {
8 pub clock_class: u8,
17
18 pub clock_accuracy: ClockAccuracy,
20
21 pub offset_scaled_log_variance: u16,
25}
26
27impl Default for ClockQuality {
28 fn default() -> Self {
29 Self {
30 clock_class: 248,
31 clock_accuracy: Default::default(),
32 offset_scaled_log_variance: 0x8000 - (23 * 256),
36 }
37 }
38}
39
40impl WireFormat for ClockQuality {
41 fn serialize(&self, buffer: &mut [u8]) -> Result<(), WireFormatError> {
42 buffer[0] = self.clock_class;
43 buffer[1] = self.clock_accuracy.to_primitive();
44 buffer[2..4].copy_from_slice(&self.offset_scaled_log_variance.to_be_bytes());
45 Ok(())
46 }
47
48 fn deserialize(buffer: &[u8]) -> Result<Self, WireFormatError> {
49 Ok(Self {
50 clock_class: buffer[0],
51 clock_accuracy: ClockAccuracy::from_primitive(buffer[1]),
52 offset_scaled_log_variance: u16::from_be_bytes(buffer[2..4].try_into().unwrap()),
53 })
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn timestamp_wireformat() {
63 let representations = [(
64 [0x7a, 0x2a, 0x12, 0x34u8],
65 ClockQuality {
66 clock_class: 122,
67 clock_accuracy: ClockAccuracy::MS2_5,
68 offset_scaled_log_variance: 0x1234,
69 },
70 )];
71
72 for (byte_representation, object_representation) in representations {
73 let mut serialization_buffer = [0; 4];
75 object_representation
76 .serialize(&mut serialization_buffer)
77 .unwrap();
78 assert_eq!(serialization_buffer, byte_representation);
79
80 let deserialized_data = ClockQuality::deserialize(&byte_representation).unwrap();
82 assert_eq!(deserialized_data, object_representation);
83 }
84 }
85}