Skip to main content

statime_wire/messages/
follow_up.rs

1use crate::{Error, common::Timestamp};
2
3/// Follow up message for a sync message.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct FollowUpMessage {
6    /// Precise time the matching sync message was transmitted.
7    pub precise_origin_timestamp: Timestamp,
8}
9
10impl FollowUpMessage {
11    pub(crate) fn content_size(&self) -> usize {
12        10
13    }
14
15    pub(crate) fn serialize_content(&self, buffer: &mut [u8]) -> Result<(), Error> {
16        self.precise_origin_timestamp
17            .serialize(buffer.get_mut(0..10).ok_or(Error::BufferTooShort)?)?;
18
19        Ok(())
20    }
21
22    pub(crate) fn deserialize_content(buffer: &[u8]) -> Result<Self, Error> {
23        let slice = buffer.get(0..10).ok_or(Error::BufferTooShort)?;
24        let precise_origin_timestamp = Timestamp::deserialize(slice)?;
25
26        Ok(Self {
27            precise_origin_timestamp,
28        })
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn timestamp_wireformat() {
38        let representations = [
39            (
40                [0x00, 0x00, 0x45, 0xb1, 0x11, 0x5a, 0x0a, 0x64, 0xfa, 0xb0],
41                FollowUpMessage {
42                    precise_origin_timestamp: Timestamp::new(1_169_232_218, 174_389_936).unwrap(),
43                },
44            ),
45            (
46                [0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01u8],
47                FollowUpMessage {
48                    precise_origin_timestamp: Timestamp::new(0x0000_0000_0002, 0x0000_0001)
49                        .unwrap(),
50                },
51            ),
52        ];
53
54        for (byte_representation, object_representation) in representations {
55            // Test the serialization output
56            let mut serialization_buffer = [0; 10];
57            object_representation
58                .serialize_content(&mut serialization_buffer)
59                .unwrap();
60            assert_eq!(serialization_buffer, byte_representation);
61
62            // Test the deserialization output
63            let deserialized_data =
64                FollowUpMessage::deserialize_content(&byte_representation).unwrap();
65            assert_eq!(deserialized_data, object_representation);
66        }
67    }
68}