Skip to main content

qubit_codec/value/
value_codec_execution_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
9//! Type-erased value-codec execution errors.
10
11use std::any::TypeId;
12
13use thiserror::Error;
14
15/// Failure produced by a type-erased value-codec invocation.
16///
17/// Registry lookups erase the concrete encoder and decoder types, so the
18/// mismatch and failure they report are carried here as diagnostics instead of
19/// a generic type parameter.
20///
21/// # Examples
22///
23/// ```
24/// use qubit_codec::ValueCodecExecutionError;
25/// use std::any::TypeId;
26///
27/// let error = ValueCodecExecutionError::TypeMismatch {
28///     expected_type: "u32",
29///     actual_type: TypeId::of::<String>(),
30/// };
31/// assert_eq!(
32///     error.to_string(),
33///     format!("value codec for u32 received incompatible type {:?}", TypeId::of::<String>())
34/// );
35/// ```
36#[derive(Debug, Error)]
37#[must_use]
38pub enum ValueCodecExecutionError {
39    /// The supplied value has the wrong Rust type.
40    #[error("value codec for {expected_type} received incompatible type {actual_type:?}")]
41    TypeMismatch {
42        /// Expected diagnostic Rust type name.
43        expected_type: &'static str,
44        /// Actual process-local Rust type identity.
45        actual_type: TypeId,
46    },
47    /// The typed encoder failed.
48    #[error("value codec {codec_type} failed to encode: {source}")]
49    EncodeFailed {
50        /// Diagnostic Rust codec type name.
51        codec_type: &'static str,
52        /// Typed encoder source error.
53        #[source]
54        source: Box<dyn std::error::Error>,
55    },
56    /// The typed decoder failed.
57    #[error("value codec {codec_type} failed to decode: {source}")]
58    DecodeFailed {
59        /// Diagnostic Rust codec type name.
60        codec_type: &'static str,
61        /// Typed decoder source error.
62        #[source]
63        source: Box<dyn std::error::Error>,
64    },
65}