zerodds-cdr 1.0.0-rc.1

XCDR1/XCDR2 encoder/decoder + KeyHash + PL_CDR1 helpers. Implements OMG XTypes 1.3 §7.4 wire format. Pure-Rust no_std + alloc.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! `CdrEncode` / `CdrDecode` Traits + Primitive-Implementierungen
//! (W1.3).
//!
//! Trait-basiert statt freier Funktionen, damit Composite-Typen in W2/W3
//! ihre Children einheitlich rekursiv-encodieren koennen ohne grosse
//! Type-Switches.
//!
//! # Alignment-Konvention
//!
//! - `u8`/`i8`/`bool`: 1
//! - `u16`/`i16`: 2
//! - `u32`/`i32`/`f32`/`char`: 4
//! - `u64`/`i64`/`f64`: 8
//!
//! Char wird als XCDR2-`wchar32` (4 Byte) kodiert (OMG-XTypes §7.4.7);
//! das deckt voll Unicode ab. Die XCDR1-`char`-Variante (1 Byte ASCII)
//! lebt im separaten `xcdr1`-Modul (siehe Crate-Header).

use crate::buffer::BufferReader;
use crate::error::DecodeError;

#[cfg(feature = "alloc")]
use crate::buffer::BufferWriter;
#[cfg(feature = "alloc")]
use crate::error::EncodeError;

// ============================================================================
// Traits
// ============================================================================

/// Wert kann in einen [`BufferWriter`] enkodiert werden.
#[cfg(feature = "alloc")]
pub trait CdrEncode {
    /// Schreibt diesen Wert in den Writer (alignment-bewusst).
    ///
    /// # Errors
    /// [`EncodeError`].
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError>;
}

/// Stub-Trait fuer no-alloc Builds: nicht nutzbar, da [`BufferWriter`]
/// nur mit `alloc`-Feature existiert. Wird mit Slice-basiertem Writer
/// in Phase 1 nachgereicht.
#[cfg(not(feature = "alloc"))]
pub trait CdrEncode {}

/// Wert kann aus einem [`BufferReader`] dekodiert werden.
pub trait CdrDecode: Sized {
    /// Liest diesen Wert aus dem Reader (alignment-bewusst).
    ///
    /// # Errors
    /// [`DecodeError`].
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError>;
}

// ============================================================================
// Primitive Encoder/Decoder
// ============================================================================

#[cfg(feature = "alloc")]
impl CdrEncode for u8 {
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError> {
        writer.write_u8(*self)
    }
}

impl CdrDecode for u8 {
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError> {
        reader.read_u8()
    }
}

#[cfg(feature = "alloc")]
impl CdrEncode for i8 {
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError> {
        writer.write_u8(*self as u8)
    }
}

impl CdrDecode for i8 {
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError> {
        Ok(reader.read_u8()? as i8)
    }
}

#[cfg(feature = "alloc")]
impl CdrEncode for u16 {
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError> {
        writer.write_u16(*self)
    }
}

impl CdrDecode for u16 {
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError> {
        reader.read_u16()
    }
}

#[cfg(feature = "alloc")]
impl CdrEncode for i16 {
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError> {
        writer.write_u16(*self as u16)
    }
}

impl CdrDecode for i16 {
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError> {
        Ok(reader.read_u16()? as i16)
    }
}

#[cfg(feature = "alloc")]
impl CdrEncode for u32 {
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError> {
        writer.write_u32(*self)
    }
}

impl CdrDecode for u32 {
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError> {
        reader.read_u32()
    }
}

#[cfg(feature = "alloc")]
impl CdrEncode for i32 {
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError> {
        writer.write_u32(*self as u32)
    }
}

impl CdrDecode for i32 {
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError> {
        Ok(reader.read_u32()? as i32)
    }
}

#[cfg(feature = "alloc")]
impl CdrEncode for u64 {
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError> {
        writer.write_u64(*self)
    }
}

impl CdrDecode for u64 {
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError> {
        reader.read_u64()
    }
}

#[cfg(feature = "alloc")]
impl CdrEncode for i64 {
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError> {
        writer.write_u64(*self as u64)
    }
}

impl CdrDecode for i64 {
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError> {
        Ok(reader.read_u64()? as i64)
    }
}

#[cfg(feature = "alloc")]
impl CdrEncode for f32 {
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError> {
        writer.write_u32(self.to_bits())
    }
}

impl CdrDecode for f32 {
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError> {
        Ok(Self::from_bits(reader.read_u32()?))
    }
}

#[cfg(feature = "alloc")]
impl CdrEncode for f64 {
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError> {
        writer.write_u64(self.to_bits())
    }
}

impl CdrDecode for f64 {
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError> {
        Ok(Self::from_bits(reader.read_u64()?))
    }
}

#[cfg(feature = "alloc")]
impl CdrEncode for bool {
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError> {
        writer.write_u8(u8::from(*self))
    }
}

impl CdrDecode for bool {
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError> {
        let offset = reader.position();
        let byte = reader.read_u8()?;
        match byte {
            0 => Ok(false),
            1 => Ok(true),
            other => Err(DecodeError::InvalidBool {
                value: other,
                offset,
            }),
        }
    }
}

#[cfg(feature = "alloc")]
impl CdrEncode for char {
    fn encode(&self, writer: &mut BufferWriter) -> Result<(), EncodeError> {
        // XCDR2 wchar32 (UTF-32 Codepoint).
        writer.write_u32(*self as u32)
    }
}

impl CdrDecode for char {
    fn decode(reader: &mut BufferReader<'_>) -> Result<Self, DecodeError> {
        let offset = reader.position();
        let value = reader.read_u32()?;
        char::from_u32(value).ok_or(DecodeError::InvalidChar { value, offset })
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)]
    use super::*;
    use crate::Endianness;

    #[cfg(feature = "alloc")]
    fn roundtrip_alloc<T>(value: T)
    where
        T: CdrEncode + CdrDecode + PartialEq + core::fmt::Debug,
    {
        let mut w = BufferWriter::new(Endianness::Little);
        value.encode(&mut w).expect("encode");
        let bytes = w.into_bytes();
        let mut r = BufferReader::new(&bytes, Endianness::Little);
        let decoded = T::decode(&mut r).expect("decode");
        assert_eq!(decoded, value);
        assert_eq!(r.remaining(), 0);
    }

    #[cfg(feature = "alloc")]
    fn roundtrip_be_alloc<T>(value: T)
    where
        T: CdrEncode + CdrDecode + PartialEq + core::fmt::Debug,
    {
        let mut w = BufferWriter::new(Endianness::Big);
        value.encode(&mut w).expect("encode");
        let bytes = w.into_bytes();
        let mut r = BufferReader::new(&bytes, Endianness::Big);
        let decoded = T::decode(&mut r).expect("decode");
        assert_eq!(decoded, value);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn u8_roundtrip() {
        roundtrip_alloc(0u8);
        roundtrip_alloc(0xABu8);
        roundtrip_alloc(0xFFu8);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn i8_roundtrip() {
        roundtrip_alloc(0i8);
        roundtrip_alloc(-1i8);
        roundtrip_alloc(127i8);
        roundtrip_alloc(-128i8);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn u16_roundtrip_le_and_be() {
        roundtrip_alloc(0x1234u16);
        roundtrip_be_alloc(0x1234u16);
        roundtrip_alloc(u16::MAX);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn i16_roundtrip() {
        roundtrip_alloc(-1i16);
        roundtrip_alloc(i16::MIN);
        roundtrip_alloc(i16::MAX);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn u32_roundtrip() {
        roundtrip_alloc(0xDEAD_BEEFu32);
        roundtrip_be_alloc(0xCAFE_BABEu32);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn i32_roundtrip() {
        roundtrip_alloc(-1i32);
        roundtrip_alloc(i32::MIN);
        roundtrip_alloc(i32::MAX);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn u64_roundtrip() {
        roundtrip_alloc(0x0102_0304_0506_0708u64);
        roundtrip_be_alloc(0x0102_0304_0506_0708u64);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn i64_roundtrip() {
        roundtrip_alloc(-1i64);
        roundtrip_alloc(i64::MIN);
        roundtrip_alloc(i64::MAX);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn f32_roundtrip_normal_values() {
        roundtrip_alloc(0.0f32);
        roundtrip_alloc(1.5f32);
        roundtrip_alloc(-2.5f32);
        roundtrip_alloc(f32::MIN_POSITIVE);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn f32_roundtrip_special() {
        // NaN ist nicht selbst-equal — nutze to_bits-Vergleich.
        let mut w = BufferWriter::new(Endianness::Little);
        f32::NAN.encode(&mut w).unwrap();
        let bytes = w.into_bytes();
        let mut r = BufferReader::new(&bytes, Endianness::Little);
        let decoded = f32::decode(&mut r).unwrap();
        assert!(decoded.is_nan());
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn f64_roundtrip() {
        roundtrip_alloc(0.0f64);
        roundtrip_alloc(core::f64::consts::PI);
        roundtrip_alloc(-1.0e-308f64);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn bool_roundtrip() {
        roundtrip_alloc(true);
        roundtrip_alloc(false);
    }

    #[test]
    fn bool_decode_rejects_other_values() {
        let bytes = [0xFFu8];
        let mut r = BufferReader::new(&bytes, Endianness::Little);
        let res = bool::decode(&mut r);
        assert!(matches!(
            res,
            Err(DecodeError::InvalidBool {
                value: 0xFF,
                offset: 0
            })
        ));
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn char_roundtrip_ascii_and_unicode() {
        roundtrip_alloc('A');
        roundtrip_alloc('z');
        roundtrip_alloc('0');
        roundtrip_alloc('🦀'); // Unicode-Code-Point ueber BMP
        roundtrip_alloc('ä');
    }

    #[test]
    fn char_decode_rejects_surrogate() {
        // 0xD800 ist Surrogate-Pair-Marker, kein gueltiger Codepoint.
        let bytes = [0x00, 0xD8, 0x00, 0x00];
        let mut r = BufferReader::new(&bytes, Endianness::Little);
        let res = char::decode(&mut r);
        assert!(matches!(
            res,
            Err(DecodeError::InvalidChar { value: 0xD800, .. })
        ));
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn primitives_align_correctly_when_mixed() {
        // u8 + u16 + u32 + u64 → erwartet 16 Byte (1 + 1pad + 2 + 4 + 8)
        let mut w = BufferWriter::new(Endianness::Little);
        1u8.encode(&mut w).unwrap();
        2u16.encode(&mut w).unwrap();
        3u32.encode(&mut w).unwrap();
        4u64.encode(&mut w).unwrap();
        assert_eq!(w.position(), 16);

        let bytes = w.into_bytes();
        let mut r = BufferReader::new(&bytes, Endianness::Little);
        assert_eq!(u8::decode(&mut r).unwrap(), 1);
        assert_eq!(u16::decode(&mut r).unwrap(), 2);
        assert_eq!(u32::decode(&mut r).unwrap(), 3);
        assert_eq!(u64::decode(&mut r).unwrap(), 4);
    }
}