Skip to main content

qubit_value/
value_error.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! # Value Processing Error Types
11//!
12//! Defines various errors that may occur during value processing.
13//!
14
15use qubit_datatype::{
16    DataConversionError,
17    DataType,
18};
19use thiserror::Error;
20
21/// Value processing error type
22///
23/// Defines various error conditions that may occur during value operations.
24///
25/// # Features
26///
27/// - Type mismatch error
28/// - No value error
29/// - Type conversion failure error
30/// - Conversion error
31///
32/// # Example
33///
34/// ```rust
35/// use qubit_value::ValueError;
36///
37/// let error = ValueError::NoValue;
38/// assert_eq!(error.to_string(), "No value");
39/// ```
40///
41///
42#[derive(Debug, Error, PartialEq, Eq)]
43pub enum ValueError {
44    /// No value
45    #[error("No value")]
46    NoValue,
47
48    /// Type mismatch
49    #[error("Type mismatch: expected {expected}, actual {actual}")]
50    TypeMismatch {
51        /// Expected data type
52        expected: DataType,
53        /// Actual data type
54        actual: DataType,
55    },
56
57    /// Type conversion failed
58    #[error("Type conversion failed: from {from} to {to}")]
59    ConversionFailed {
60        /// Source data type
61        from: DataType,
62        /// Target data type
63        to: DataType,
64    },
65
66    /// Conversion error (with detailed information)
67    #[error("Conversion error: {0}")]
68    ConversionError(String),
69
70    /// Index out of bounds
71    #[error("Index out of bounds: index {index}, length {len}")]
72    IndexOutOfBounds {
73        /// Accessed index
74        index: usize,
75        /// Actual length
76        len: usize,
77    },
78
79    /// JSON serialization error
80    #[error("JSON serialization error: {0}")]
81    JsonSerializationError(String),
82
83    /// JSON deserialization error
84    #[error("JSON deserialization error: {0}")]
85    JsonDeserializationError(String),
86}
87
88/// Value processing result type
89pub type ValueResult<T> = Result<T, ValueError>;
90
91/// Maps a shared `qubit_datatype` conversion error into this crate's error type.
92///
93/// # Parameters
94///
95/// * `error` - The error returned by the shared data conversion layer.
96///
97/// # Returns
98///
99/// Returns the corresponding [`ValueError`] variant.
100pub(crate) fn map_data_conversion_error(error: DataConversionError) -> ValueError {
101    match error {
102        DataConversionError::NoValue => ValueError::NoValue,
103        DataConversionError::ConversionFailed { from, to } => {
104            ValueError::ConversionFailed { from, to }
105        }
106        DataConversionError::ConversionError(message) => ValueError::ConversionError(message),
107        DataConversionError::JsonSerializationError(message) => {
108            ValueError::JsonSerializationError(message)
109        }
110        DataConversionError::JsonDeserializationError(message) => {
111            ValueError::JsonDeserializationError(message)
112        }
113    }
114}