qubit_codec/transcode/transcode_contract_error.rs
1// =============================================================================
2// Copyright (c) 2026 Haixing Hu.
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6// Licensed under the Apache License, Version 2.0.
7// =============================================================================
8use thiserror::Error;
9
10/// Error reported when a transcoder returns inconsistent progress.
11///
12/// This error represents a broken [`crate::Transcoder`] implementation rather
13/// than malformed input data. Buffered drivers call
14/// [`crate::TranscodeProgress::validate`] before trusting progress counters for
15/// unchecked buffer cursor movement.
16#[derive(Clone, Copy, Debug, Eq, Error, Hash, PartialEq)]
17pub enum TranscodeContractError {
18 /// The transcoder consumed more input units than the caller supplied.
19 #[error(
20 "transcoder consumed {read} units but only {available} were available"
21 )]
22 OverRead {
23 /// Input units reported as consumed.
24 read: usize,
25 /// Input units available to the transcode call.
26 available: usize,
27 },
28
29 /// The transcoder wrote more output units than the caller supplied.
30 #[error(
31 "transcoder wrote {written} units but only {available} output slots were available"
32 )]
33 OverWritten {
34 /// Output units reported as written.
35 written: usize,
36 /// Output slots available to the transcode call.
37 available: usize,
38 },
39
40 /// Progress could not be represented as an absolute index.
41 #[error(
42 "transcoder progress overflow: index {index} plus advanced {advanced}"
43 )]
44 ProgressIndexOverflow {
45 /// Absolute index supplied to the transcode call.
46 index: usize,
47 /// Relative progress reported by the transcoder.
48 advanced: usize,
49 },
50
51 /// A status reported an index that does not match relative progress.
52 #[error("transcoder reported status index {reported}, expected {expected}")]
53 StatusIndexMismatch {
54 /// Index reported by the status.
55 reported: usize,
56 /// Index implied by the progress counter.
57 expected: usize,
58 },
59
60 /// A status requested input or output that is already available.
61 #[error(
62 "transcoder reported required {required} with available {available}"
63 )]
64 SatisfiedNeed {
65 /// Required units reported by the status.
66 required: usize,
67 /// Available units reported by the status.
68 available: usize,
69 },
70}