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