qubit_codec/codec/codec_convert_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// =============================================================================
8//! Generic conversion error used by codec converter adapters.
9
10use thiserror::Error;
11
12use super::{
13 codec_decode_error::CodecDecodeError,
14 codec_encode_error::CodecEncodeError,
15};
16
17/// Error reported by codec-backed buffered converters.
18///
19/// A converter first decodes source units into a logical value and then encodes
20/// that value into target units. This error keeps those two failure sources
21/// explicit instead of hiding them behind an implicit conversion. The encode
22/// branch wraps [`CodecEncodeError`] so callers can distinguish codec encode
23/// failures from adapter-level output-index errors.
24#[derive(Clone, Copy, Debug, Eq, Error, Hash, PartialEq)]
25pub enum CodecConvertError<D, E> {
26 /// Source-unit decoding failed.
27 #[error("codec conversion decode error: {source}")]
28 Decode {
29 /// Decode error reported by the decoder side of the converter.
30 #[source]
31 source: CodecDecodeError<D>,
32 },
33
34 /// Target-unit encoding failed.
35 #[error("codec conversion encode error: {source}")]
36 Encode {
37 /// Encode error reported by the encoder side of the converter.
38 #[source]
39 source: CodecEncodeError<E>,
40 },
41}
42
43impl<D, E> CodecConvertError<D, E> {
44 /// Creates a conversion error from a decode-side failure.
45 ///
46 /// # Parameters
47 ///
48 /// - `source`: Decode error reported while reading source units.
49 ///
50 /// # Returns
51 ///
52 /// Returns a decode-side conversion error.
53 #[must_use]
54 #[inline(always)]
55 pub const fn decode(source: CodecDecodeError<D>) -> Self {
56 Self::Decode { source }
57 }
58
59 /// Creates a conversion error from an encode-side failure.
60 ///
61 /// # Parameters
62 ///
63 /// - `source`: Encode error reported while writing target units.
64 ///
65 /// # Returns
66 ///
67 /// Returns an encode-side conversion error.
68 #[must_use]
69 #[inline(always)]
70 pub const fn encode(source: CodecEncodeError<E>) -> Self {
71 Self::Encode { source }
72 }
73}