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///
22/// # Examples
23///
24/// ```
25/// use std::any::TypeId;
26///
27/// use qubit_codec::ValueCodecExecutionError;
28///
29/// let error = ValueCodecExecutionError::TypeMismatch {
30/// expected_type: "u32",
31/// actual_type: TypeId::of::<String>(),
32/// };
33/// assert_eq!(
34/// error.to_string(),
35/// format!("value codec for u32 received incompatible type {:?}", TypeId::of::<String>())
36/// );
37/// ```
38#[derive(Debug, Error)]
39#[must_use]
40pub enum ValueCodecExecutionError {
41 /// The supplied value has the wrong Rust type.
42 #[error("value codec for {expected_type} received incompatible type {actual_type:?}")]
43 TypeMismatch {
44 /// Expected diagnostic Rust type name.
45 expected_type: &'static str,
46 /// Actual process-local Rust type identity.
47 actual_type: TypeId,
48 },
49 /// The typed encoder failed.
50 #[error("value codec {codec_type} failed to encode: {source}")]
51 EncodeFailed {
52 /// Diagnostic Rust codec type name.
53 codec_type: &'static str,
54 /// Typed encoder source error.
55 #[source]
56 source: Box<dyn Error>,
57 },
58 /// The typed decoder failed.
59 #[error("value codec {codec_type} failed to decode: {source}")]
60 DecodeFailed {
61 /// Diagnostic Rust codec type name.
62 codec_type: &'static str,
63 /// Typed decoder source error.
64 #[source]
65 source: Box<dyn Error>,
66 },
67}