qubit-codec 0.17.2

Core codec traits and buffer conversion primitives for Rust
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
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Tests the safe type-erased bidirectional string value-codec descriptor.

use std::any::Any;
use std::any::TypeId;
use std::any::type_name;
use std::convert::Infallible;
use std::io::Error;
use std::rc::Rc;

use qubit_codec::ValueCodecExecutionError;
use qubit_codec::ValueDecoder;
use qubit_codec::ValueEncoder;
use qubit_codec::ValueStringCodecDescriptor;

/// String codec that rejects `u32::MAX` on encode and non-numeric text on
/// decode, so both failure directions stay reachable from one descriptor.
#[derive(Default)]
struct MirrorU32StringCodec;

impl ValueEncoder<u32> for MirrorU32StringCodec {
    type Output = String;
    type Error = Error;

    fn encode(&mut self, input: &u32) -> Result<Self::Output, Self::Error> {
        if *input == u32::MAX {
            Err(Error::other("mirror string encode rejected u32::MAX"))
        } else {
            Ok(input.to_string())
        }
    }
}

impl ValueDecoder<str> for MirrorU32StringCodec {
    type Output = u32;
    type Error = Error;

    fn decode(&mut self, input: &str) -> Result<Self::Output, Self::Error> {
        input
            .parse()
            .map_err(|_| Error::other("mirror string decode needs a decimal number"))
    }
}

/// String codec for `String` used to check ownership of the erased output.
#[derive(Default)]
struct MirrorTextStringCodec;

impl ValueEncoder<String> for MirrorTextStringCodec {
    type Output = String;
    type Error = Error;

    fn encode(&mut self, input: &String) -> Result<Self::Output, Self::Error> {
        Ok(input.clone())
    }
}

/// Stateful string codec used to verify that descriptors create fresh codecs.
#[derive(Default)]
struct FreshStringCodec {
    calls: usize,
}

impl ValueEncoder<String> for FreshStringCodec {
    type Output = String;
    type Error = Error;

    fn encode(&mut self, input: &String) -> Result<String, Error> {
        self.calls += 1;
        Ok(format!("{}:{input}", self.calls))
    }
}

impl ValueDecoder<str> for FreshStringCodec {
    type Output = String;
    type Error = Error;

    fn decode(&mut self, input: &str) -> Result<String, Error> {
        self.calls += 1;
        Ok(format!("{}:{input}", self.calls))
    }
}

impl ValueDecoder<str> for MirrorTextStringCodec {
    type Output = String;
    type Error = Error;

    fn decode(&mut self, input: &str) -> Result<Self::Output, Self::Error> {
        Ok(input.to_owned())
    }
}

static U32_DESCRIPTOR: ValueStringCodecDescriptor = ValueStringCodecDescriptor::of::<MirrorU32StringCodec, u32>();
static TEXT_DESCRIPTOR: ValueStringCodecDescriptor = ValueStringCodecDescriptor::of::<MirrorTextStringCodec, String>();
static FRESH_STRING: ValueStringCodecDescriptor = ValueStringCodecDescriptor::of::<FreshStringCodec, String>();

/// Borrowing codec used to verify owned descriptor values and preserved errors.
#[derive(Default)]
struct ReviewBorrowedCodec;

impl ValueEncoder<[u8]> for ReviewBorrowedCodec {
    type Output = String;
    type Error = Error;

    fn encode(&mut self, input: &[u8]) -> Result<String, Error> {
        String::from_utf8(input.to_vec()).map_err(Error::other)
    }
}

impl ValueDecoder<str> for ReviewBorrowedCodec {
    type Output = Vec<u8>;
    type Error = Error;

    fn decode(&mut self, input: &str) -> Result<Vec<u8>, Error> {
        if input == "reject" {
            return Err(Error::other("review decode rejected"));
        }
        Ok(input.as_bytes().to_vec())
    }
}

/// String borrowing codec used to exercise borrowed `str` descriptor inputs.
#[derive(Default)]
struct ReviewBorrowedTextCodec;

impl ValueEncoder<str> for ReviewBorrowedTextCodec {
    type Output = String;
    type Error = Error;

    fn encode(&mut self, input: &str) -> Result<String, Error> {
        Ok(input.to_owned())
    }
}

impl ValueDecoder<str> for ReviewBorrowedTextCodec {
    type Output = String;
    type Error = Error;

    fn decode(&mut self, input: &str) -> Result<String, Error> {
        Ok(input.to_owned())
    }
}

static REVIEW_BORROWED: ValueStringCodecDescriptor =
    ValueStringCodecDescriptor::of_borrowed::<ReviewBorrowedCodec, Vec<u8>, [u8]>();

/// Local-only codec proving descriptors do not require thread-safe values.
#[derive(Default)]
struct LocalValueCodec {
    _local: Rc<()>,
}

impl ValueEncoder<Rc<u32>> for LocalValueCodec {
    type Output = String;
    type Error = Infallible;

    fn encode(&mut self, input: &Rc<u32>) -> Result<String, Infallible> {
        Ok(input.to_string())
    }
}

impl ValueDecoder<str> for LocalValueCodec {
    type Output = Rc<u32>;
    type Error = Infallible;

    fn decode(&mut self, _input: &str) -> Result<Rc<u32>, Infallible> {
        Ok(Rc::new(7))
    }
}

#[test]
fn test_metadata_matches_the_types_selected_at_construction() {
    assert_eq!(U32_DESCRIPTOR.codec_type_id(), TypeId::of::<MirrorU32StringCodec>());
    assert_eq!(
        U32_DESCRIPTOR.codec_type_name(),
        type_name::<MirrorU32StringCodec>(),
        "the codec name must be diagnostic metadata, not a stable ID"
    );
    assert_eq!(U32_DESCRIPTOR.value_type_id(), TypeId::of::<u32>());
    assert_eq!(
        U32_DESCRIPTOR.value_type_name(),
        "u32",
        "a primitive value type must report its plain diagnostic name"
    );
}

#[test]
fn test_debug_output_lists_both_names_and_is_non_exhaustive() {
    let rendered = format!("{U32_DESCRIPTOR:?}");

    for fragment in [
        "ValueStringCodecDescriptor",
        "codec_type_name",
        type_name::<MirrorU32StringCodec>(),
        "value_type_name",
        "u32",
        "..",
    ] {
        assert!(
            rendered.contains(fragment),
            "the Debug output must contain {fragment:?}, but rendered {rendered:?}"
        );
    }
}

#[test]
fn test_type_mismatch_reports_the_declared_and_actual_types() {
    let error = U32_DESCRIPTOR
        .encode(&42_u64)
        .expect_err("a `u64` must not be accepted by a `u32` descriptor");

    match error {
        ValueCodecExecutionError::TypeMismatch {
            expected_type,
            actual_type,
        } => {
            assert_eq!(
                expected_type,
                type_name::<u32>(),
                "the mismatch must name the value type declared by the descriptor"
            );
            assert_eq!(
                actual_type,
                TypeId::of::<u64>(),
                "the mismatch must report the process-local identity of the value supplied"
            );
        }
        other => panic!("expected a TypeMismatch error, got {other:?}"),
    }
}

#[test]
fn test_encode_failure_names_the_codec_and_keeps_the_source_error() {
    let error = U32_DESCRIPTOR
        .encode(&u32::MAX)
        .expect_err("the fixture codec must reject u32::MAX");

    match error {
        ValueCodecExecutionError::EncodeFailed { codec_type, source } => {
            assert_eq!(
                codec_type,
                type_name::<MirrorU32StringCodec>(),
                "an encode failure must name the concrete codec that failed"
            );
            assert_eq!(
                source.to_string(),
                "mirror string encode rejected u32::MAX",
                "the typed encoder error must remain reachable through the erasure layer"
            );
        }
        other => panic!("expected an EncodeFailed error, got {other:?}"),
    }
}

#[test]
fn test_decode_failure_names_the_codec_and_keeps_the_source_error() {
    let error = U32_DESCRIPTOR
        .decode("not-a-number")
        .expect_err("the fixture codec must reject non-numeric text");

    match error {
        ValueCodecExecutionError::DecodeFailed { codec_type, source } => {
            assert_eq!(
                codec_type,
                type_name::<MirrorU32StringCodec>(),
                "a decode failure must name the concrete codec that failed"
            );
            assert_eq!(
                source.to_string(),
                "mirror string decode needs a decimal number",
                "the typed decoder error must survive erasure"
            );
        }
        other => panic!("expected a DecodeFailed error, got {other:?}"),
    }
}

#[test]
fn test_decoded_output_owns_its_data_and_outlives_the_input_buffer() {
    let decoded = {
        let input = String::from("mirror-owned-text");
        TEXT_DESCRIPTOR.decode(&input).expect("any text decodes into a String")
    };

    let owned = decoded
        .downcast_ref::<String>()
        .expect("the erased output must be downcastable to the declared value type");
    assert_eq!(
        owned, "mirror-owned-text",
        "the decoded box must own its string rather than borrow the input"
    );
}

#[test]
fn test_decoded_output_downcasts_to_the_declared_value_type_only() {
    let decoded = U32_DESCRIPTOR.decode("42").expect("decimal text decodes into a u32");

    assert!(decoded.downcast_ref::<u32>().is_some());
    assert!(
        decoded.downcast_ref::<i32>().is_none(),
        "the erased output must only downcast to the value type the descriptor declared"
    );
}

#[test]
fn test_encode_accepts_any_erased_handle_to_the_declared_type() {
    let value: Box<dyn Any> = Box::new(42_u32);
    let boxed = U32_DESCRIPTOR.encode(&*value).expect("a boxed `u32` is still a `u32`");
    let inline = U32_DESCRIPTOR.encode(&42_u32).expect("a stack `u32` is still a `u32`");

    assert_eq!(
        boxed, inline,
        "the erased entry point must behave identically for boxed and inline values"
    );
    assert_eq!(boxed, "42");
}

#[test]
fn test_each_operation_uses_a_fresh_default_codec() {
    let first = TEXT_DESCRIPTOR.encode(&String::from("mirror")).expect("encode");
    let second = TEXT_DESCRIPTOR.encode(&String::from("mirror")).expect("encode");

    assert_eq!(
        first, second,
        "the descriptor must keep no state between operations, so encoding twice must agree"
    );
    assert_eq!(first, "mirror");
}

#[test]
fn test_copying_a_descriptor_preserves_its_behaviour() {
    let copied = U32_DESCRIPTOR;

    assert_eq!(copied.codec_type_id(), U32_DESCRIPTOR.codec_type_id());
    assert_eq!(
        copied.encode(&5_u32).expect("encode"),
        U32_DESCRIPTOR.encode(&5_u32).expect("encode"),
        "a copied descriptor must stay executable"
    );
}

#[test]
fn test_encode_decode_round_trip_preserves_the_value() {
    let value = u32::from(u16::MAX);
    let encoded = U32_DESCRIPTOR.encode(&value).expect("encode");
    let decoded = U32_DESCRIPTOR
        .decode(&encoded)
        .expect("decode the text just produced")
        .downcast_ref::<u32>()
        .expect("the erased output must be the declared value type")
        .to_owned();

    assert_eq!(decoded, value, "a round trip must preserve the value");
}

#[test]
fn test_borrowed_descriptor_round_trip_preserves_owned_values() {
    let value = b"hello".to_vec();
    assert_eq!(REVIEW_BORROWED.value_type_id(), TypeId::of::<Vec<u8>>());
    let text = REVIEW_BORROWED.encode(&value).unwrap();
    assert_eq!(text, "hello");
    assert_eq!(
        *REVIEW_BORROWED.decode(&text).unwrap().downcast::<Vec<u8>>().unwrap(),
        value
    );
    let descriptor = ValueStringCodecDescriptor::of_borrowed::<ReviewBorrowedTextCodec, String, str>();
    assert_eq!(descriptor.encode(&String::from("text")).unwrap(), "text");
    assert_eq!(
        *descriptor.decode("text").unwrap().downcast::<String>().unwrap(),
        "text"
    );
}

#[test]
fn test_borrowed_descriptor_preserves_type_mismatch_and_source_errors() {
    assert!(matches!(REVIEW_BORROWED.encode(&String::from("wrong")),
        Err(ValueCodecExecutionError::TypeMismatch { actual_type, .. })
        if actual_type == TypeId::of::<String>()));
    match REVIEW_BORROWED.encode(&vec![0xff_u8]).unwrap_err() {
        ValueCodecExecutionError::EncodeFailed { source, .. } => {
            assert!(source.downcast_ref::<Error>().is_some())
        }
        other => panic!("unexpected error: {other:?}"),
    }
    match REVIEW_BORROWED.decode("reject").unwrap_err() {
        ValueCodecExecutionError::DecodeFailed { source, .. } => {
            assert!(source.downcast_ref::<Error>().is_some())
        }
        other => panic!("unexpected error: {other:?}"),
    }
}

#[test]
fn test_decode_of_empty_input_reports_the_codec_failure() {
    let error = U32_DESCRIPTOR
        .decode("")
        .expect_err("empty text is not a decimal number");

    assert!(
        matches!(error, ValueCodecExecutionError::DecodeFailed { .. }),
        "an empty string must surface as a decode failure, got {error:?}"
    );
}

#[test]
fn test_each_operation_uses_a_fresh_default_stateful_codec() {
    let mut direct = FreshStringCodec::default();
    assert_eq!(direct.encode(&String::from("payload")).unwrap(), "1:payload");
    assert_eq!(direct.encode(&String::from("payload")).unwrap(), "2:payload");

    let payload = String::from("payload");
    assert_eq!(FRESH_STRING.encode(&payload).unwrap(), "1:payload");
    assert_eq!(FRESH_STRING.encode(&payload).unwrap(), "1:payload");
    let decoded = FRESH_STRING.decode("payload").unwrap();
    assert_eq!(decoded.downcast_ref::<String>().unwrap(), "1:payload");
    let decoded_again = FRESH_STRING.decode("payload").unwrap();
    assert_eq!(decoded_again.downcast_ref::<String>().unwrap(), "1:payload");
    assert_eq!(FRESH_STRING.encode(&payload).unwrap(), "1:payload");
    let decoded_after_encode = FRESH_STRING.decode("payload").unwrap();
    assert_eq!(decoded_after_encode.downcast_ref::<String>().unwrap(), "1:payload");
    let copied = FRESH_STRING;
    assert_eq!(copied.encode(&payload).unwrap(), "1:payload");
    let copied_decoded = copied.decode("payload").unwrap();
    assert_eq!(copied_decoded.downcast_ref::<String>().unwrap(), "1:payload");
    let copied_decoded_again = copied.decode("payload").unwrap();
    assert_eq!(copied_decoded_again.downcast_ref::<String>().unwrap(), "1:payload");
}

#[test]
fn test_thread_safe_errors_do_not_require_thread_safe_codec_or_value() {
    let descriptor = ValueStringCodecDescriptor::of::<LocalValueCodec, Rc<u32>>();
    let value = Rc::new(7_u32);
    assert_eq!(descriptor.encode(&value).unwrap(), "7");
    let decoded = descriptor.decode("7").unwrap().downcast::<Rc<u32>>().unwrap();
    assert_eq!(**decoded, 7);
}