Skip to main content

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