qubit_codec/codec/codec_encode_error.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2026 Haixing Hu.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Generic encode error used by codec-backed encoder adapters.
11
12use thiserror::Error;
13
14/// Error reported by codec-backed buffered encoder adapters.
15///
16/// The wrapped codec remains responsible for domain-specific encode failures.
17/// This type adds adapter-level failures that cannot be represented by the
18/// wrapped codec itself, such as a buffered encoder receiving an invalid input
19/// or output start index.
20#[derive(Clone, Copy, Debug, Eq, Error, Hash, PartialEq)]
21pub enum CodecEncodeError<E> {
22 /// The wrapped codec reported an encode error.
23 #[error("codec encode error at input index {input_index}: {source}")]
24 Encode {
25 /// Error returned by the wrapped codec.
26 #[source]
27 source: E,
28 /// Absolute input index of the value being encoded.
29 input_index: usize,
30 },
31
32 /// The caller supplied an input index outside the input slice.
33 #[error("invalid input index {index} for input length {len}")]
34 InvalidInputIndex {
35 /// Invalid input index supplied by the caller.
36 index: usize,
37 /// Length of the input slice.
38 len: usize,
39 },
40
41 /// The caller supplied an output index outside the output slice.
42 #[error("invalid output index {index} for output length {len}")]
43 InvalidOutputIndex {
44 /// Invalid output index supplied by the caller.
45 index: usize,
46 /// Length of the output slice.
47 len: usize,
48 },
49}
50
51impl<E> CodecEncodeError<E> {
52 /// Creates an error wrapping a codec-specific encode error.
53 ///
54 /// # Parameters
55 ///
56 /// - `source`: Error returned by the wrapped codec.
57 /// - `input_index`: Absolute input index of the value being encoded.
58 ///
59 /// # Returns
60 ///
61 /// Returns a codec encode error wrapper.
62 #[must_use]
63 #[inline(always)]
64 pub const fn encode(source: E, input_index: usize) -> Self {
65 Self::Encode { source, input_index }
66 }
67
68 /// Creates an invalid-input-index error.
69 ///
70 /// # Parameters
71 ///
72 /// - `index`: Invalid input index supplied by the caller.
73 /// - `len`: Length of the input slice.
74 ///
75 /// # Returns
76 ///
77 /// Returns an invalid-input-index error.
78 #[must_use]
79 #[inline(always)]
80 pub const fn invalid_input_index(index: usize, len: usize) -> Self {
81 Self::InvalidInputIndex { index, len }
82 }
83
84 /// Creates an invalid-output-index error.
85 ///
86 /// # Parameters
87 ///
88 /// - `index`: Invalid output index supplied by the caller.
89 /// - `len`: Length of the output slice.
90 ///
91 /// # Returns
92 ///
93 /// Returns an invalid-output-index error.
94 #[must_use]
95 #[inline(always)]
96 pub const fn invalid_output_index(index: usize, len: usize) -> Self {
97 Self::InvalidOutputIndex { index, len }
98 }
99}