qubit-codec 0.17.4

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
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
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Framework-level failures reported by safe transcode APIs.

use thiserror::Error;

use super::capacity_error::CapacityError;
use super::transcode_contract_error::TranscodeContractError;
use crate::Codec;
use crate::codec::assert_unit_bounds;

/// Framework-level failure reported by a transcode operation.
///
/// These failures are part of the safe public transcode API. They describe
/// caller-supplied buffer ranges, capacity planning, complete-input shape, and
/// stream lifecycle misuse that the transcode layer can detect without
/// interpreting a concrete codec error.
///
/// # Examples
///
/// ```
/// use qubit_codec::TranscodeFailure;
///
/// let failure = TranscodeFailure::invalid_input_index(4, 3);
/// assert_eq!(failure.to_string(), "invalid input index 4 for input length 3");
/// assert_eq!(TranscodeFailure::ensure_input_index(3, 4), Err(failure));
/// ```
#[derive(Clone, Copy, Debug, Eq, Error, Hash, PartialEq)]
#[non_exhaustive]
#[must_use]
pub enum TranscodeFailure {
    /// The caller supplied an input index outside the input slice.
    #[error("invalid input index {index} for input length {input_len}")]
    InvalidInputIndex {
        /// Invalid input index supplied by the caller.
        index: usize,
        /// Length of the input slice.
        input_len: usize,
    },

    /// The caller supplied an output index outside the output slice.
    #[error("invalid output index {index} for output length {output_len}")]
    InvalidOutputIndex {
        /// Invalid output index supplied by the caller.
        index: usize,
        /// Length of the output slice.
        output_len: usize,
    },

    /// The caller supplied an output range extending beyond the output slice.
    #[error("invalid output range at index {output_index} with length {range_len} for output length {output_len}")]
    InvalidOutputRange {
        /// Absolute output index where the range starts.
        output_index: usize,
        /// Length of the caller-supplied output range.
        range_len: usize,
        /// Length of the output slice.
        output_len: usize,
    },

    /// The output slice cannot hold all required output.
    #[error("insufficient output at index {output_index}: required {required} units, available {available}")]
    InsufficientOutput {
        /// Absolute output index where writing would start.
        output_index: usize,
        /// Output units required from `output_index`.
        required: usize,
        /// Output units available from `output_index`.
        available: usize,
    },

    /// Output length arithmetic overflowed.
    #[error("output length arithmetic overflow")]
    OutputLengthOverflow,

    /// The allocator could not reserve the requested output storage.
    #[error("output allocation failed")]
    AllocationFailed,

    /// The complete input ended with an incomplete value.
    #[error(
        "incomplete input at index {input_index}: at least {required} units required to retry, {available} available"
    )]
    IncompleteInput {
        /// Absolute input index where the incomplete value starts.
        input_index: usize,
        /// Current minimum total input units required from `input_index`
        /// before retrying. A later retry may raise this lower bound.
        required: usize,
        /// Input units available from `input_index`.
        available: usize,
    },

    /// A transcoder returned progress inconsistent with the supplied buffers.
    #[error("invalid transcoder progress: {source}")]
    InvalidProgress {
        /// Contract error describing the invalid progress.
        #[source]
        source: TranscodeContractError,
    },

    /// The input contains exactly one decoded value plus trailing units.
    #[error("trailing input after value: consumed {consumed} units, remaining {remaining}")]
    TrailingInput {
        /// Units consumed by the decoded value.
        consumed: usize,
        /// Extra units left after the decoded value.
        remaining: usize,
    },

    /// A strict single-value decoder cannot expose codec lifecycle output.
    #[error(
        "strict single-value decoding does not support lifecycle output: reset bound {reset_bound}, finish bound {finish_bound}"
    )]
    UnsupportedDecodeLifecycleOutput {
        /// Maximum values the codec may emit while resetting decode state.
        reset_bound: usize,
        /// Maximum values the codec may emit while finishing decode state.
        finish_bound: usize,
    },

    /// `transcode` was called after the logical stream was finished.
    #[error("transcode called after finish without an intervening reset")]
    TranscodeAfterFinish,

    /// `transcode` was called before the first successful reset.
    #[error("transcode called before reset")]
    TranscodeBeforeReset,

    /// `finish` was called after the logical stream was already finished.
    #[error("finish called twice without an intervening reset")]
    FinishAfterFinish,

    /// `finish` was called before the first successful reset.
    #[error("finish called before reset")]
    FinishBeforeReset,

    /// Reset or finish failed after codec, hook, or buffered state may have
    /// changed. Continuing could repeat side effects or observe inconsistent
    /// state; a successful reset is required before further use.
    #[error("transcoder lifecycle is poisoned; a successful reset is required")]
    LifecyclePoisoned,
}

impl TranscodeFailure {
    /// Creates an invalid-input-index error.
    ///
    /// # Parameters
    ///
    /// - `index`: Absolute input index rejected by the caller boundary.
    /// - `len`: Total number of input units.
    ///
    /// # Returns
    ///
    /// Returns a failure retaining the supplied index and input length.
    #[inline]
    pub const fn invalid_input_index(index: usize, len: usize) -> Self {
        Self::InvalidInputIndex { index, input_len: len }
    }

    /// Creates an invalid-output-index error.
    ///
    /// # Parameters
    ///
    /// - `index`: Absolute output index rejected by the caller boundary.
    /// - `len`: Total number of writable output units.
    ///
    /// # Returns
    ///
    /// Returns a failure retaining the supplied index and output length.
    #[inline]
    pub const fn invalid_output_index(index: usize, len: usize) -> Self {
        Self::InvalidOutputIndex { index, output_len: len }
    }

    /// Creates an invalid-output-range error.
    ///
    /// # Parameters
    ///
    /// - `output_index`: Absolute start of the requested output range.
    /// - `range_len`: Requested range length.
    /// - `output_len`: Total output slice length.
    ///
    /// # Returns
    ///
    /// Returns a failure describing the range that exceeds the output slice.
    #[inline]
    pub const fn invalid_output_range(output_index: usize, range_len: usize, output_len: usize) -> Self {
        Self::InvalidOutputRange {
            output_index,
            range_len,
            output_len,
        }
    }

    /// Creates an insufficient-output error.
    ///
    /// # Parameters
    ///
    /// - `output_index`: Absolute start of the writable output.
    /// - `required`: Units needed from that index.
    /// - `available`: Units actually available from that index.
    ///
    /// # Returns
    ///
    /// Returns a failure retaining the required and available capacities.
    #[inline]
    pub const fn insufficient_output(output_index: usize, required: usize, available: usize) -> Self {
        Self::InsufficientOutput {
            output_index,
            required,
            available,
        }
    }

    /// Creates an output-length-overflow error.
    ///
    /// # Returns
    ///
    /// Returns the framework marker for unrepresentable output capacity.
    #[inline]
    pub const fn output_length_overflow() -> Self {
        Self::OutputLengthOverflow
    }

    /// Creates an output-allocation failure.
    ///
    /// # Returns
    ///
    /// Returns the framework marker for a failed output allocation.
    #[inline]
    pub const fn allocation_failed() -> Self {
        Self::AllocationFailed
    }

    /// Creates an incomplete-input error.
    ///
    /// # Parameters
    ///
    /// - `input_index`: Absolute index where the incomplete value starts.
    /// - `required`: Current minimum total units needed from that index.
    /// - `available`: Units currently available from that index.
    ///
    /// # Returns
    ///
    /// Returns a failure preserving the incomplete tail and retry requirement.
    #[inline]
    pub const fn incomplete_input(input_index: usize, required: usize, available: usize) -> Self {
        Self::IncompleteInput {
            input_index,
            required,
            available,
        }
    }

    /// Creates an invalid-progress error.
    ///
    /// # Parameters
    ///
    /// - `source`: Contract violation detected in a transcoder report.
    ///
    /// # Returns
    ///
    /// Returns a framework failure that retains the contract error as its
    /// source.
    #[inline]
    pub const fn invalid_progress(source: TranscodeContractError) -> Self {
        Self::InvalidProgress { source }
    }

    /// Creates a trailing-input error.
    ///
    /// # Parameters
    ///
    /// - `consumed`: Units used by the decoded value.
    /// - `remaining`: Extra units after that value.
    ///
    /// # Returns
    ///
    /// Returns a failure describing the otherwise successful single-value
    /// decode.
    #[inline]
    pub const fn trailing_input(consumed: usize, remaining: usize) -> Self {
        Self::TrailingInput { consumed, remaining }
    }

    /// Creates an unsupported decode-lifecycle-output error.
    ///
    /// # Parameters
    ///
    /// - `reset_bound`: Maximum values produced by decode reset.
    /// - `finish_bound`: Maximum values produced by decode finish.
    ///
    /// # Returns
    ///
    /// Returns a failure retaining the lifecycle output bounds rejected by
    /// strict decoding.
    #[inline]
    pub const fn unsupported_decode_lifecycle_output(reset_bound: usize, finish_bound: usize) -> Self {
        Self::UnsupportedDecodeLifecycleOutput {
            reset_bound,
            finish_bound,
        }
    }

    /// Validates that `input_index` is within an input slice.
    ///
    /// # Parameters
    ///
    /// - `input_len`: Total input slice length.
    /// - `input_index`: Absolute start index, including the end boundary.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when the index is at most the input length.
    ///
    /// # Errors
    ///
    /// Returns [`Self::InvalidInputIndex`] when the index exceeds the length.
    #[inline]
    pub fn ensure_input_index(input_len: usize, input_index: usize) -> Result<(), Self> {
        if input_index > input_len {
            return Err(Self::invalid_input_index(input_index, input_len));
        }
        Ok(())
    }

    /// Validates that enough input units are available from `input_index`.
    ///
    /// # Parameters
    ///
    /// - `input_len`: Total input slice length.
    /// - `input_index`: Absolute start of the input region.
    /// - `min_required`: Minimum readable units required from that start.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when the start index and minimum readable capacity are
    /// valid.
    ///
    /// # Errors
    ///
    /// Returns [`Self::InvalidInputIndex`] for an invalid start, or
    /// [`Self::IncompleteInput`] when fewer than `min_required` units remain.
    #[inline]
    pub fn ensure_min_input(input_len: usize, input_index: usize, min_required: usize) -> Result<(), Self> {
        Self::ensure_input_index(input_len, input_index)?;
        let available = input_len - input_index;
        if available < min_required {
            return Err(Self::incomplete_input(input_index, min_required, available));
        }
        Ok(())
    }

    /// Validates a consumed input count and rejects trailing input.
    ///
    /// # Parameters
    ///
    /// - `consumed`: Number of input units consumed by the decoded value.
    /// - `input_len`: Total number of input units supplied.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when `consumed == input_len`.
    ///
    /// # Errors
    ///
    /// Returns [`TranscodeFailure::InvalidInputIndex`] when `consumed` exceeds
    /// `input_len`, or [`TranscodeFailure::TrailingInput`] when unconsumed
    /// input remains.
    #[inline]
    pub fn ensure_no_trailing_input(consumed: usize, input_len: usize) -> Result<(), Self> {
        Self::ensure_input_index(input_len, consumed)?;
        let remaining = input_len - consumed;
        if remaining != 0 {
            return Err(Self::trailing_input(consumed, remaining));
        }
        Ok(())
    }

    /// Validates that `output_index` is within an output slice.
    ///
    /// # Parameters
    ///
    /// - `output_len`: Total output slice length.
    /// - `output_index`: Absolute start index, including the end boundary.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when the index is at most the output length.
    ///
    /// # Errors
    ///
    /// Returns [`Self::InvalidOutputIndex`] when the index exceeds the length.
    #[inline]
    pub fn ensure_output_index(output_len: usize, output_index: usize) -> Result<(), Self> {
        if output_index > output_len {
            return Err(Self::invalid_output_index(output_index, output_len));
        }
        Ok(())
    }

    /// Validates input and output start indices for a transcode call.
    ///
    /// # Parameters
    ///
    /// - `input_len`: Total input slice length.
    /// - `input_index`: Absolute starting input index.
    /// - `output_len`: Total output slice length.
    /// - `output_index`: Absolute starting output index.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when both indices lie within their slices or at their
    /// ends.
    ///
    /// # Errors
    ///
    /// Returns [`Self::InvalidInputIndex`] first for invalid input, otherwise
    /// [`Self::InvalidOutputIndex`] for invalid output.
    #[inline]
    pub fn ensure_transcode_indices(
        input_len: usize,
        input_index: usize,
        output_len: usize,
        output_index: usize,
    ) -> Result<(), Self> {
        Self::ensure_input_index(input_len, input_index)?;
        Self::ensure_output_index(output_len, output_index)
    }

    /// Validates that an output slice can hold one-shot finalization output.
    ///
    /// # Parameters
    ///
    /// - `output_len`: Total output slice length.
    /// - `output_index`: Absolute start of the writable region.
    /// - `required`: Minimum output units needed from that start.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when the output region has at least `required` units.
    ///
    /// # Errors
    ///
    /// Returns [`Self::InvalidOutputIndex`] for an invalid start, or
    /// [`Self::InsufficientOutput`] when the remaining capacity is too small.
    #[inline]
    pub fn ensure_output_capacity(output_len: usize, output_index: usize, required: usize) -> Result<(), Self> {
        Self::ensure_output_index(output_len, output_index)?;
        let available = output_len - output_index;
        if available < required {
            return Err(Self::insufficient_output(output_index, required, available));
        }
        Ok(())
    }

    /// Validates an indexed output range and its minimum writable capacity.
    ///
    /// # Parameters
    ///
    /// - `output_len`: Total output slice length.
    /// - `output_index`: Absolute start of the writable range.
    /// - `range_len`: Length of the caller-selected writable range.
    /// - `required`: Minimum capacity required within the selected range.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when the selected range fits and has enough capacity.
    ///
    /// # Errors
    ///
    /// Returns [`Self::InvalidOutputIndex`] for an invalid start,
    /// [`Self::InvalidOutputRange`] when the range exceeds the slice, or
    /// [`Self::InsufficientOutput`] when the selected range is too short.
    #[inline]
    pub fn ensure_output_range(
        output_len: usize,
        output_index: usize,
        range_len: usize,
        required: usize,
    ) -> Result<(), Self> {
        Self::ensure_output_index(output_len, output_index)?;
        let available = output_len - output_index;
        if range_len > available {
            return Err(Self::invalid_output_range(output_index, range_len, output_len));
        }
        if range_len < required {
            return Err(Self::insufficient_output(output_index, required, range_len));
        }
        Ok(())
    }

    /// Rejects lifecycle output for a strict single-value decode.
    ///
    /// # Type Parameters
    ///
    /// - `C`: Codec whose decode lifecycle bounds are checked.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when both decode lifecycle output bounds are zero.
    ///
    /// # Errors
    ///
    /// Returns [`Self::UnsupportedDecodeLifecycleOutput`] when reset or finish
    /// may emit values.
    ///
    /// # Panics
    ///
    /// Panics at compile time when `C` violates the required codec unit bounds.
    #[inline]
    pub(crate) fn ensure_no_decode_lifecycle_output<C>() -> Result<(), Self>
    where
        C: Codec,
    {
        assert_unit_bounds::<C>();
        if C::MAX_DECODE_RESET_VALUES != 0 || C::MAX_DECODE_FINISH_VALUES != 0 {
            return Err(Self::unsupported_decode_lifecycle_output(
                C::MAX_DECODE_RESET_VALUES,
                C::MAX_DECODE_FINISH_VALUES,
            ));
        }
        Ok(())
    }
}

impl From<CapacityError> for TranscodeFailure {
    /// Converts capacity planning errors into transcode framework failures.
    ///
    /// # Parameters
    ///
    /// - `error`: Capacity-planning failure to translate.
    ///
    /// # Returns
    ///
    /// Returns the corresponding framework output-length-overflow failure.
    #[inline]
    fn from(error: CapacityError) -> Self {
        match error {
            CapacityError::OutputLengthOverflow => Self::OutputLengthOverflow,
        }
    }
}

impl From<TranscodeContractError> for TranscodeFailure {
    /// Converts a broken transcoder progress report into a framework failure.
    ///
    /// # Parameters
    ///
    /// - `error`: Progress-contract violation to retain as the error source.
    ///
    /// # Returns
    ///
    /// Returns [`Self::InvalidProgress`] carrying the original violation.
    #[inline]
    fn from(error: TranscodeContractError) -> Self {
        Self::invalid_progress(error)
    }
}