zerodds-amqp-bridge 1.0.0-rc.1

OASIS AMQP 1.0 Wire-Codec — no_std + alloc Type-System + Frame-Format + Performatives + Message-Sections + Codec-/Codec-Lite-Profile-Marker.
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors

//! AMQP 1.0 Type System — Spec `amqp-1.0-types`.
//!
//! Implementiert die Format-Code-Constructors fuer Primitive-Types
//! und Variable-Width-Encodings. Compound-Types (list/map) werden mit
//! Length-Prefix-Validation unterstuetzt; die innere Struktur ist
//! Caller-Layer (Caller serialisiert Elemente und uebergibt
//! Element-Count + Element-Bytes).

use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;

/// AMQP 1.0 Format Codes — Spec §1.2 Table 1-1.
///
/// Wir geben die wichtigsten benannten Konstanten + Kategorie-Helpers.
pub mod codes {
    /// `0x40` — null (fixed-0).
    pub const NULL: u8 = 0x40;
    /// `0x41` — boolean true (fixed-0).
    pub const BOOLEAN_TRUE: u8 = 0x41;
    /// `0x42` — boolean false (fixed-0).
    pub const BOOLEAN_FALSE: u8 = 0x42;
    /// `0x56` — boolean (fixed-1, 0x00=false, 0x01=true).
    pub const BOOLEAN: u8 = 0x56;
    /// `0x50` — ubyte (fixed-1).
    pub const UBYTE: u8 = 0x50;
    /// `0x60` — ushort (fixed-2, BE).
    pub const USHORT: u8 = 0x60;
    /// `0x70` — uint (fixed-4, BE).
    pub const UINT: u8 = 0x70;
    /// `0x52` — smalluint (fixed-1).
    pub const SMALLUINT: u8 = 0x52;
    /// `0x43` — uint0 (fixed-0, value=0).
    pub const UINT0: u8 = 0x43;
    /// `0x80` — ulong (fixed-8, BE).
    pub const ULONG: u8 = 0x80;
    /// `0x53` — smallulong (fixed-1).
    pub const SMALLULONG: u8 = 0x53;
    /// `0x44` — ulong0 (fixed-0, value=0).
    pub const ULONG0: u8 = 0x44;
    /// `0x51` — byte (fixed-1, signed).
    pub const BYTE: u8 = 0x51;
    /// `0x61` — short (fixed-2, BE signed).
    pub const SHORT: u8 = 0x61;
    /// `0x71` — int (fixed-4, BE signed).
    pub const INT: u8 = 0x71;
    /// `0x54` — smallint (fixed-1, signed).
    pub const SMALLINT: u8 = 0x54;
    /// `0x81` — long (fixed-8, BE signed).
    pub const LONG: u8 = 0x81;
    /// `0x55` — smalllong (fixed-1, signed).
    pub const SMALLLONG: u8 = 0x55;
    /// `0xA0` — vbin8 (1-byte length + bytes).
    pub const VBIN8: u8 = 0xA0;
    /// `0xB0` — vbin32 (4-byte BE length + bytes).
    pub const VBIN32: u8 = 0xB0;
    /// `0xA1` — str8-utf8 (1-byte length + UTF-8).
    pub const STR8: u8 = 0xA1;
    /// `0xB1` — str32-utf8 (4-byte BE length + UTF-8).
    pub const STR32: u8 = 0xB1;
    /// `0xA3` — sym8 (1-byte length + ASCII).
    pub const SYM8: u8 = 0xA3;
    /// `0xB3` — sym32 (4-byte BE length + ASCII).
    pub const SYM32: u8 = 0xB3;

    // ------------------------------------------------------------------
    //  Floating + Char + Timestamp + UUID + Decimal.
    // ------------------------------------------------------------------
    /// `0x72` — float (fixed-4, IEEE 754 binary32 BE).
    pub const FLOAT: u8 = 0x72;
    /// `0x82` — double (fixed-8, IEEE 754 binary64 BE).
    pub const DOUBLE: u8 = 0x82;
    /// `0x73` — char (fixed-4, UTF-32 BE codepoint).
    pub const CHAR: u8 = 0x73;
    /// `0x74` — decimal32 (fixed-4, IEEE 754-2008 BID).
    pub const DECIMAL32: u8 = 0x74;
    /// `0x84` — decimal64 (fixed-8, IEEE 754-2008 BID).
    pub const DECIMAL64: u8 = 0x84;
    /// `0x94` — decimal128 (fixed-16, IEEE 754-2008 BID).
    pub const DECIMAL128: u8 = 0x94;
    /// `0x83` — timestamp (fixed-8, BE signed ms since Unix epoch).
    pub const TIMESTAMP: u8 = 0x83;
    /// `0x98` — uuid (fixed-16, RFC 4122).
    pub const UUID: u8 = 0x98;

    // ------------------------------------------------------------------
    //  Compound (list, map, array).
    // ------------------------------------------------------------------
    /// `0x45` — list0 (fixed-0, count=0).
    pub const LIST0: u8 = 0x45;
    /// `0xC0` — list8 (1-byte size + 1-byte count + items).
    pub const LIST8: u8 = 0xC0;
    /// `0xD0` — list32 (4-byte BE size + 4-byte BE count + items).
    pub const LIST32: u8 = 0xD0;
    /// `0xC1` — map8 (1-byte size + 1-byte count + entries).
    pub const MAP8: u8 = 0xC1;
    /// `0xD1` — map32 (4-byte BE size + 4-byte BE count + entries).
    pub const MAP32: u8 = 0xD1;
    /// `0xE0` — array8 (1-byte size + 1-byte count + element-constructor + items).
    pub const ARRAY8: u8 = 0xE0;
    /// `0xF0` — array32 (4-byte BE size + 4-byte BE count + element-constructor + items).
    pub const ARRAY32: u8 = 0xF0;
}

/// Format-Code-Kategorie (Spec §1.2).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FormatCode {
    /// Spec §1.2.1 — fixed-width: 0..=8 octets payload, kategorie 0x4*-0x9*.
    Fixed(u8),
    /// Spec §1.2.2 — variable-width: 1- oder 4-byte length + bytes,
    /// kategorie 0xA*/0xB*.
    Variable(u8),
    /// Spec §1.2.3 — compound: 1- oder 4-byte size + count + items,
    /// kategorie 0xC*/0xD*.
    Compound(u8),
    /// Spec §1.2.4 — array: 1- oder 4-byte size + count + element-
    /// constructor + items, kategorie 0xE*/0xF*.
    Array(u8),
}

impl FormatCode {
    /// Bestimmt Kategorie aus dem Format-Code-Byte (Spec §1.2 Table 1-1
    /// Subcategory column).
    #[must_use]
    pub const fn from_byte(b: u8) -> Self {
        match b >> 4 {
            0x4..=0x9 => Self::Fixed(b),
            0xA | 0xB => Self::Variable(b),
            0xC | 0xD => Self::Compound(b),
            0xE | 0xF => Self::Array(b),
            _ => Self::Fixed(b),
        }
    }
}

/// AMQP-Wert (Subset der Primitive-Types).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AmqpValue {
    /// `null`.
    Null,
    /// `boolean`.
    Boolean(bool),
    /// `ulong` (oder smallulong/ulong0 je nach Wert).
    Ulong(u64),
    /// `long`.
    Long(i64),
    /// `binary` (max u32::MAX-Bytes).
    Binary(Vec<u8>),
    /// `string` (UTF-8).
    String(String),
    /// `symbol` (ASCII).
    Symbol(String),
}

/// Type-Codec-Fehler.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TypeError {
    /// Input bytes truncated.
    Truncated,
    /// Unknown / unsupported Format-Code.
    UnsupportedFormatCode(u8),
    /// String hat invalides UTF-8.
    InvalidUtf8,
    /// Symbol enthaelt non-ASCII (Spec §1.6.21: "Symbols are encoded
    /// as ASCII").
    NonAsciiSymbol,
    /// Length-Praefix > u32::MAX (geht nicht ueber Wire).
    LengthTooLarge,
}

impl fmt::Display for TypeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Truncated => f.write_str("input truncated"),
            Self::UnsupportedFormatCode(c) => write!(f, "unsupported format code 0x{c:02X}"),
            Self::InvalidUtf8 => f.write_str("invalid UTF-8 in str8/str32"),
            Self::NonAsciiSymbol => f.write_str("non-ASCII byte in symbol"),
            Self::LengthTooLarge => f.write_str("length exceeds u32::MAX"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for TypeError {}

/// Spec §1.6.1 — `null` = 0x40.
#[must_use]
pub fn encode_null() -> Vec<u8> {
    alloc::vec![codes::NULL]
}

/// Spec §1.6.2 — `boolean`. Wir nutzen die kompakte Form 0x41/0x42
/// (fixed-0).
#[must_use]
pub fn encode_boolean(v: bool) -> Vec<u8> {
    alloc::vec![if v {
        codes::BOOLEAN_TRUE
    } else {
        codes::BOOLEAN_FALSE
    }]
}

/// Spec §1.6.6 — `ulong`. Wir waehlen die kompakteste Form (ulong0
/// fuer 0, smallulong fuer 1..=255, ulong sonst).
#[must_use]
pub fn encode_ulong(v: u64) -> Vec<u8> {
    if v == 0 {
        alloc::vec![codes::ULONG0]
    } else if v <= u64::from(u8::MAX) {
        let b = (v & 0xFF) as u8;
        alloc::vec![codes::SMALLULONG, b]
    } else {
        let mut out = Vec::with_capacity(9);
        out.push(codes::ULONG);
        out.extend_from_slice(&v.to_be_bytes());
        out
    }
}

/// Spec §1.6.10 — `long`. Wir waehlen smalllong fuer -128..=127, long
/// (full 8-byte) sonst.
#[must_use]
pub fn encode_long(v: i64) -> Vec<u8> {
    if (i64::from(i8::MIN)..=i64::from(i8::MAX)).contains(&v) {
        let b = (v as i8) as u8;
        alloc::vec![codes::SMALLLONG, b]
    } else {
        let mut out = Vec::with_capacity(9);
        out.push(codes::LONG);
        out.extend_from_slice(&v.to_be_bytes());
        out
    }
}

/// Spec §1.6.19 — `binary`. Waehlt vbin8 fuer len <= 255, vbin32 sonst.
///
/// # Errors
/// `LengthTooLarge` wenn `data.len() > u32::MAX`.
pub fn encode_binary(data: &[u8]) -> Result<Vec<u8>, TypeError> {
    let len = data.len();
    if len > u32::MAX as usize {
        return Err(TypeError::LengthTooLarge);
    }
    if len <= u8::MAX as usize {
        let mut out = Vec::with_capacity(2 + len);
        out.push(codes::VBIN8);
        #[allow(clippy::cast_possible_truncation)]
        out.push(len as u8);
        out.extend_from_slice(data);
        Ok(out)
    } else {
        let mut out = Vec::with_capacity(5 + len);
        out.push(codes::VBIN32);
        #[allow(clippy::cast_possible_truncation)]
        out.extend_from_slice(&(len as u32).to_be_bytes());
        out.extend_from_slice(data);
        Ok(out)
    }
}

/// Spec §1.6.20 — `string`. Waehlt str8 fuer len <= 255, str32 sonst.
///
/// # Errors
/// `LengthTooLarge` wenn `s.len() > u32::MAX`.
pub fn encode_string(s: &str) -> Result<Vec<u8>, TypeError> {
    let bytes = s.as_bytes();
    let len = bytes.len();
    if len > u32::MAX as usize {
        return Err(TypeError::LengthTooLarge);
    }
    if len <= u8::MAX as usize {
        let mut out = Vec::with_capacity(2 + len);
        out.push(codes::STR8);
        #[allow(clippy::cast_possible_truncation)]
        out.push(len as u8);
        out.extend_from_slice(bytes);
        Ok(out)
    } else {
        let mut out = Vec::with_capacity(5 + len);
        out.push(codes::STR32);
        #[allow(clippy::cast_possible_truncation)]
        out.extend_from_slice(&(len as u32).to_be_bytes());
        out.extend_from_slice(bytes);
        Ok(out)
    }
}

/// Spec §1.6.21 — `symbol`. Waehlt sym8 fuer len <= 255, sym32 sonst.
///
/// # Errors
/// * `NonAsciiSymbol` wenn der String non-ASCII enthaelt.
/// * `LengthTooLarge` wenn `s.len() > u32::MAX`.
pub fn encode_symbol(s: &str) -> Result<Vec<u8>, TypeError> {
    if !s.is_ascii() {
        return Err(TypeError::NonAsciiSymbol);
    }
    let bytes = s.as_bytes();
    let len = bytes.len();
    if len > u32::MAX as usize {
        return Err(TypeError::LengthTooLarge);
    }
    if len <= u8::MAX as usize {
        let mut out = Vec::with_capacity(2 + len);
        out.push(codes::SYM8);
        #[allow(clippy::cast_possible_truncation)]
        out.push(len as u8);
        out.extend_from_slice(bytes);
        Ok(out)
    } else {
        let mut out = Vec::with_capacity(5 + len);
        out.push(codes::SYM32);
        #[allow(clippy::cast_possible_truncation)]
        out.extend_from_slice(&(len as u32).to_be_bytes());
        out.extend_from_slice(bytes);
        Ok(out)
    }
}

/// Decodiert einen einzigen `AmqpValue` ab `bytes[0]`. Liefert
/// `Ok((value, consumed_bytes))`.
///
/// # Errors
/// Siehe [`TypeError`].
pub fn decode_value(bytes: &[u8]) -> Result<(AmqpValue, usize), TypeError> {
    if bytes.is_empty() {
        return Err(TypeError::Truncated);
    }
    let code = bytes[0];
    match code {
        codes::NULL => Ok((AmqpValue::Null, 1)),
        codes::BOOLEAN_TRUE => Ok((AmqpValue::Boolean(true), 1)),
        codes::BOOLEAN_FALSE => Ok((AmqpValue::Boolean(false), 1)),
        codes::BOOLEAN => {
            if bytes.len() < 2 {
                return Err(TypeError::Truncated);
            }
            Ok((AmqpValue::Boolean(bytes[1] != 0), 2))
        }
        codes::ULONG0 => Ok((AmqpValue::Ulong(0), 1)),
        codes::SMALLULONG => {
            if bytes.len() < 2 {
                return Err(TypeError::Truncated);
            }
            Ok((AmqpValue::Ulong(u64::from(bytes[1])), 2))
        }
        codes::ULONG => {
            if bytes.len() < 9 {
                return Err(TypeError::Truncated);
            }
            let mut buf = [0u8; 8];
            buf.copy_from_slice(&bytes[1..9]);
            Ok((AmqpValue::Ulong(u64::from_be_bytes(buf)), 9))
        }
        codes::SMALLLONG => {
            if bytes.len() < 2 {
                return Err(TypeError::Truncated);
            }
            #[allow(clippy::cast_possible_wrap)]
            Ok((AmqpValue::Long(i64::from(bytes[1] as i8)), 2))
        }
        codes::LONG => {
            if bytes.len() < 9 {
                return Err(TypeError::Truncated);
            }
            let mut buf = [0u8; 8];
            buf.copy_from_slice(&bytes[1..9]);
            Ok((AmqpValue::Long(i64::from_be_bytes(buf)), 9))
        }
        codes::VBIN8 => {
            if bytes.len() < 2 {
                return Err(TypeError::Truncated);
            }
            let len = usize::from(bytes[1]);
            if bytes.len() < 2 + len {
                return Err(TypeError::Truncated);
            }
            Ok((AmqpValue::Binary(bytes[2..2 + len].to_vec()), 2 + len))
        }
        codes::VBIN32 => {
            if bytes.len() < 5 {
                return Err(TypeError::Truncated);
            }
            let len = u32::from_be_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]) as usize;
            if bytes.len() < 5 + len {
                return Err(TypeError::Truncated);
            }
            Ok((AmqpValue::Binary(bytes[5..5 + len].to_vec()), 5 + len))
        }
        codes::STR8 => decode_str8(bytes, AmqpValue::String),
        codes::STR32 => decode_str32(bytes, AmqpValue::String),
        codes::SYM8 => decode_str8(bytes, AmqpValue::Symbol),
        codes::SYM32 => decode_str32(bytes, AmqpValue::Symbol),
        other => Err(TypeError::UnsupportedFormatCode(other)),
    }
}

fn decode_str8(
    bytes: &[u8],
    wrap: fn(String) -> AmqpValue,
) -> Result<(AmqpValue, usize), TypeError> {
    if bytes.len() < 2 {
        return Err(TypeError::Truncated);
    }
    let len = usize::from(bytes[1]);
    if bytes.len() < 2 + len {
        return Err(TypeError::Truncated);
    }
    let s = core::str::from_utf8(&bytes[2..2 + len])
        .map_err(|_| TypeError::InvalidUtf8)?
        .to_owned();
    Ok((wrap(s), 2 + len))
}

fn decode_str32(
    bytes: &[u8],
    wrap: fn(String) -> AmqpValue,
) -> Result<(AmqpValue, usize), TypeError> {
    if bytes.len() < 5 {
        return Err(TypeError::Truncated);
    }
    let len = u32::from_be_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]) as usize;
    if bytes.len() < 5 + len {
        return Err(TypeError::Truncated);
    }
    let s = core::str::from_utf8(&bytes[5..5 + len])
        .map_err(|_| TypeError::InvalidUtf8)?
        .to_owned();
    Ok((wrap(s), 5 + len))
}

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

    #[test]
    fn null_encodes_to_single_byte_0x40() {
        // Spec §1.6.1.
        assert_eq!(encode_null(), alloc::vec![0x40]);
    }

    #[test]
    fn boolean_uses_compact_format_codes() {
        // Spec §1.6.2 — 0x41 true, 0x42 false.
        assert_eq!(encode_boolean(true), alloc::vec![0x41]);
        assert_eq!(encode_boolean(false), alloc::vec![0x42]);
    }

    #[test]
    fn ulong_zero_uses_ulong0_format() {
        // Spec §1.6.6.
        assert_eq!(encode_ulong(0), alloc::vec![0x44]);
    }

    #[test]
    fn ulong_small_uses_smallulong_format() {
        // Spec §1.6.6 — smallulong = 0x53 + 1 byte.
        assert_eq!(encode_ulong(255), alloc::vec![0x53, 0xFF]);
    }

    #[test]
    fn ulong_large_uses_full_8_byte_format() {
        // Spec §1.6.6 — ulong = 0x80 + 8 bytes BE.
        let bytes = encode_ulong(0x1122_3344_5566_7788);
        assert_eq!(bytes[0], 0x80);
        assert_eq!(&bytes[1..], &0x1122_3344_5566_7788_u64.to_be_bytes());
    }

    #[test]
    fn long_small_uses_smalllong_format() {
        // Spec §1.6.10 — smalllong = 0x55 + 1 byte (signed).
        assert_eq!(encode_long(-1), alloc::vec![0x55, 0xFF]);
        assert_eq!(encode_long(127), alloc::vec![0x55, 0x7F]);
    }

    #[test]
    fn long_large_uses_full_8_byte_format() {
        let bytes = encode_long(i64::MIN);
        assert_eq!(bytes[0], 0x81);
    }

    #[test]
    fn binary_short_uses_vbin8_format() {
        // Spec §1.6.19 — vbin8 = 0xA0 + 1 byte len.
        let bytes = encode_binary(&[1, 2, 3]).expect("encode");
        assert_eq!(bytes, alloc::vec![0xA0, 0x03, 1, 2, 3]);
    }

    #[test]
    fn binary_long_uses_vbin32_format() {
        // Spec §1.6.19 — vbin32 = 0xB0 + 4 byte BE len.
        let data = alloc::vec![0xAA; 300];
        let bytes = encode_binary(&data).expect("encode");
        assert_eq!(bytes[0], 0xB0);
        assert_eq!(&bytes[1..5], &300u32.to_be_bytes());
    }

    #[test]
    fn string_short_uses_str8_format() {
        // Spec §1.6.20 — str8-utf8 = 0xA1 + 1 byte len + UTF-8.
        let bytes = encode_string("hi").expect("encode");
        assert_eq!(bytes, alloc::vec![0xA1, 0x02, b'h', b'i']);
    }

    #[test]
    fn string_unicode_round_trip() {
        let bytes = encode_string("Käfer").expect("encode");
        let (parsed, consumed) = decode_value(&bytes).expect("decode");
        assert_eq!(consumed, bytes.len());
        match parsed {
            AmqpValue::String(s) => assert_eq!(s, "Käfer"),
            _ => panic!("expected string"),
        }
    }

    #[test]
    fn symbol_rejects_non_ascii() {
        // Spec §1.6.21 — Symbols are ASCII.
        assert_eq!(encode_symbol("Käfer"), Err(TypeError::NonAsciiSymbol));
    }

    #[test]
    fn symbol_short_uses_sym8_format() {
        let bytes = encode_symbol("hello").expect("encode");
        assert_eq!(bytes[0], 0xA3);
        assert_eq!(bytes[1], 0x05);
        assert_eq!(&bytes[2..], b"hello");
    }

    #[test]
    fn round_trip_all_primitive_values() {
        let values = alloc::vec![
            (encode_null(), AmqpValue::Null),
            (encode_boolean(true), AmqpValue::Boolean(true)),
            (encode_boolean(false), AmqpValue::Boolean(false)),
            (encode_ulong(0), AmqpValue::Ulong(0)),
            (encode_ulong(42), AmqpValue::Ulong(42)),
            (
                encode_ulong(0x1234_5678_9ABC_DEF0),
                AmqpValue::Ulong(0x1234_5678_9ABC_DEF0)
            ),
            (encode_long(-100), AmqpValue::Long(-100)),
            (encode_long(i64::MIN), AmqpValue::Long(i64::MIN)),
            (
                encode_binary(&[1, 2, 3]).expect("ok"),
                AmqpValue::Binary(alloc::vec![1, 2, 3])
            ),
            (
                encode_binary(&alloc::vec![0u8; 500]).expect("ok"),
                AmqpValue::Binary(alloc::vec![0u8; 500])
            ),
            (
                encode_string("foo").expect("ok"),
                AmqpValue::String("foo".into())
            ),
            (
                encode_symbol("bar").expect("ok"),
                AmqpValue::Symbol("bar".into())
            ),
        ];
        for (bytes, expected) in values {
            let (parsed, consumed) = decode_value(&bytes).expect("decode");
            assert_eq!(parsed, expected);
            assert_eq!(consumed, bytes.len());
        }
    }

    #[test]
    fn unsupported_format_code_yields_error() {
        // 0xFF ist Reserved/unsupported in unserer Subset-Implementation.
        assert_eq!(
            decode_value(&[0xFF]),
            Err(TypeError::UnsupportedFormatCode(0xFF))
        );
    }

    #[test]
    fn truncated_inputs_yield_error() {
        assert_eq!(decode_value(&[]), Err(TypeError::Truncated));
        assert_eq!(decode_value(&[0xA0]), Err(TypeError::Truncated)); // vbin8 ohne len.
        assert_eq!(decode_value(&[0xA0, 5, 1]), Err(TypeError::Truncated)); // vbin8 truncated body.
        assert_eq!(decode_value(&[0x80, 0, 0, 0]), Err(TypeError::Truncated)); // ulong truncated.
    }

    #[test]
    fn invalid_utf8_in_str_yields_error() {
        // str8 mit invalid UTF-8 byte 0xFF.
        assert_eq!(
            decode_value(&[0xA1, 0x01, 0xFF]),
            Err(TypeError::InvalidUtf8)
        );
    }

    #[test]
    fn format_code_categorizes_correctly() {
        // Spec §1.2 Subcategories.
        assert!(matches!(FormatCode::from_byte(0x40), FormatCode::Fixed(_)));
        assert!(matches!(FormatCode::from_byte(0x80), FormatCode::Fixed(_)));
        assert!(matches!(
            FormatCode::from_byte(0xA0),
            FormatCode::Variable(_)
        ));
        assert!(matches!(
            FormatCode::from_byte(0xB0),
            FormatCode::Variable(_)
        ));
        assert!(matches!(
            FormatCode::from_byte(0xC0),
            FormatCode::Compound(_)
        ));
        assert!(matches!(
            FormatCode::from_byte(0xD0),
            FormatCode::Compound(_)
        ));
        assert!(matches!(FormatCode::from_byte(0xE0), FormatCode::Array(_)));
        assert!(matches!(FormatCode::from_byte(0xF0), FormatCode::Array(_)));
    }
}