statime_wire/messages/
p_delay_req.rs1use crate::{Error, common::Timestamp};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct PDelayReqMessage {
6 pub origin_timestamp: Timestamp,
8}
9
10impl PDelayReqMessage {
11 pub(crate) fn content_size(&self) -> usize {
12 20
13 }
14
15 pub(crate) fn serialize_content(&self, buffer: &mut [u8]) -> Result<(), crate::Error> {
16 if buffer.len() < 20 {
17 return Err(Error::BufferTooShort);
18 }
19
20 self.origin_timestamp.serialize(&mut buffer[0..10])?;
21 buffer[10..20].fill(0);
22
23 Ok(())
24 }
25
26 pub(crate) fn deserialize_content(buffer: &[u8]) -> Result<Self, crate::Error> {
27 let slice = buffer.get(0..20).ok_or(Error::BufferTooShort)?;
28
29 Ok(Self {
30 origin_timestamp: Timestamp::deserialize(slice)?,
31 })
32 }
33}
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38
39 #[test]
40 fn timestamp_wireformat() {
41 let representations = [(
42 [
43 0x00, 0x00, 0x45, 0xb1, 0x11, 0x5a, 0x0a, 0x64, 0xfa, 0xb0, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 ],
46 PDelayReqMessage {
47 origin_timestamp: Timestamp::new(1_169_232_218, 174_389_936).unwrap(),
48 },
49 )];
50
51 for (byte_representation, object_representation) in representations {
52 let mut serialization_buffer = [0; 20];
54 object_representation
55 .serialize_content(&mut serialization_buffer)
56 .unwrap();
57 assert_eq!(serialization_buffer, byte_representation);
58
59 let deserialized_data =
61 PDelayReqMessage::deserialize_content(&byte_representation).unwrap();
62 assert_eq!(deserialized_data, object_representation);
63 }
64 }
65}