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/// Domain error reported by codec-backed converter adapters.
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 while framework buffer failures remain in
22/// [`crate::TranscodeError`].
23#[derive(Clone, Copy, Debug, Eq, Error, Hash, PartialEq)]
24pub enum CodecConvertError<D, E> {
25 /// Source-unit decoding failed.
26 #[error("codec conversion decode error: {0}")]
27 Decode(#[source] CodecDecodeError<D>),
28
29 /// Target-unit encoding failed.
30 #[error("codec conversion encode error: {0}")]
31 Encode(#[source] CodecEncodeError<E>),
32}
33
34impl<D, E> CodecConvertError<D, E> {
35 /// Creates a conversion error from a decode-side failure.
36 ///
37 /// # Parameters
38 ///
39 /// - `source`: Decode error reported while reading source units.
40 ///
41 /// # Returns
42 ///
43 /// Returns a decode-side conversion error.
44 #[inline(always)]
45 #[must_use]
46 pub const fn decode(source: CodecDecodeError<D>) -> Self {
47 Self::Decode(source)
48 }
49
50 /// Creates a conversion error from an encode-side failure.
51 ///
52 /// # Parameters
53 ///
54 /// - `source`: Encode error reported while writing target units.
55 ///
56 /// # Returns
57 ///
58 /// Returns an encode-side conversion error.
59 #[inline(always)]
60 #[must_use]
61 pub const fn encode(source: CodecEncodeError<E>) -> Self {
62 Self::Encode(source)
63 }
64}