rustdds/serialization/
representation_identifier.rs

1use std::io;
2
3use speedy::{Readable, Writable};
4use byteorder::ReadBytesExt;
5
6/// Used to identify serialization format of payload data over RTPS.
7#[derive(Debug, PartialEq, Eq, Clone, Copy, Readable, Writable)]
8pub struct RepresentationIdentifier {
9  pub(crate) bytes: [u8; 2], // semi-public for serialization elsewhere
10}
11
12impl RepresentationIdentifier {
13  // Numeric values are from RTPS spec v2.3 Section 10.5 , Table 10.3
14  pub const CDR_BE: Self = Self {
15    bytes: [0x00, 0x00],
16  };
17  pub const CDR_LE: Self = Self {
18    bytes: [0x00, 0x01],
19  };
20
21  pub const PL_CDR_BE: Self = Self {
22    bytes: [0x00, 0x02],
23  };
24  pub const PL_CDR_LE: Self = Self {
25    bytes: [0x00, 0x03],
26  };
27
28  // [0x00,0x04] defined below
29  // [0x00,0x05] is not defined, as far as we know
30
31  pub const CDR2_BE: Self = Self {
32    bytes: [0x00, 0x10],
33  };
34  pub const CDR2_LE: Self = Self {
35    bytes: [0x00, 0x11],
36  };
37
38  pub const PL_CDR2_BE: Self = Self {
39    bytes: [0x00, 0x12],
40  };
41  pub const PL_CDR2_LE: Self = Self {
42    bytes: [0x00, 0x13],
43  };
44
45  pub const D_CDR_BE: Self = Self {
46    bytes: [0x00, 0x14],
47  };
48  pub const D_CDR_LE: Self = Self {
49    bytes: [0x00, 0x15],
50  };
51
52  // XML
53  pub const XML: Self = Self {
54    bytes: [0x00, 0x04],
55  };
56
57  // The following are from
58  // "Extensible and Dynamic Topic Types for DDS" (DDS X-Types v 1.2) spec, Table
59  // 60, Section 7.6.2.1.2 Use of the RTPS Encapsulation Identifier
60
61  // Table says "CDR2_BE", but that name is already taken.
62  pub const XCDR2_BE: Self = Self {
63    bytes: [0x00, 0x06],
64  };
65
66  pub const XCDR2_LE: Self = Self {
67    bytes: [0x00, 0x07],
68  };
69
70  pub const D_CDR2_BE: Self = Self {
71    bytes: [0x00, 0x08],
72  };
73
74  pub const D_CDR2_LE: Self = Self {
75    bytes: [0x00, 0x09],
76  };
77
78  // Table says "PL_CDR2_BE", but name is already taken
79  pub const PL_XCDR2_BE: Self = Self {
80    bytes: [0x00, 0x0a],
81  };
82  // Table says "PL_CDR_LE" (no "2"), but this is likely a typo in the spec.
83  pub const PL_XCDR2_LE: Self = Self {
84    bytes: [0x00, 0x0b],
85  };
86
87  // Reads two bytes to form a `RepresentationIdentifier`
88  pub fn from_bytes(bytes: &[u8]) -> io::Result<Self> {
89    let mut reader = io::Cursor::new(bytes);
90    Ok(Self {
91      bytes: [reader.read_u8()?, reader.read_u8()?],
92    })
93  }
94
95  pub fn to_bytes(self) -> [u8; 2] {
96    self.bytes
97  }
98}