Skip to main content

qubit_config/
error.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! # Configuration Error Types
10//!
11//! Defines all possible error types in the configuration system.
12//!
13//! # Author
14//!
15//! Haixing Hu
16
17use thiserror::Error;
18
19use qubit_common::DataType;
20use qubit_value::ValueError;
21
22/// Configuration error type
23///
24/// Defines all possible error scenarios in the configuration system.
25///
26/// # Examples
27///
28/// ```rust,ignore
29/// use qubit_config::{Config, ConfigError, ConfigResult};
30/// fn get_port(config: &Config) -> ConfigResult<i32> { unimplemented!() }
31/// ```
32///
33/// # Author
34///
35/// Haixing Hu
36///
37#[derive(Debug, Error)]
38pub enum ConfigError {
39    /// Property not found
40    #[error("Property not found: {0}")]
41    PropertyNotFound(String),
42
43    /// Property has no value
44    #[error("Property '{0}' has no value")]
45    PropertyHasNoValue(String),
46
47    /// Type mismatch
48    #[error("Type mismatch: expected {expected}, actual {actual}")]
49    TypeMismatch {
50        /// Expected type
51        expected: DataType,
52        /// Actual type
53        actual: DataType,
54    },
55
56    /// Type conversion failed
57    #[error("Type conversion failed: {0}")]
58    ConversionError(String),
59
60    /// Index out of bounds
61    #[error("Index out of bounds: index {index}, length {len}")]
62    IndexOutOfBounds {
63        /// Index being accessed
64        index: usize,
65        /// Actual length
66        len: usize,
67    },
68
69    /// Variable substitution failed
70    #[error("Variable substitution failed: {0}")]
71    SubstitutionError(String),
72
73    /// Variable substitution depth exceeded
74    #[error("Variable substitution depth exceeded maximum limit: {0}")]
75    SubstitutionDepthExceeded(usize),
76
77    /// Configuration merge failed
78    #[error("Configuration merge failed: {0}")]
79    MergeError(String),
80
81    /// Property is final and cannot be overridden
82    #[error("Property '{0}' is final and cannot be overridden")]
83    PropertyIsFinal(String),
84
85    /// IO error
86    #[error("IO error: {0}")]
87    IoError(#[from] std::io::Error),
88
89    /// Parse error
90    #[error("Parse error: {0}")]
91    ParseError(String),
92
93    /// Other error
94    #[error("Configuration error: {0}")]
95    Other(String),
96}
97
98/// Result type for configuration operations
99///
100/// Used for all operations in the configuration system that may return errors.
101pub type ConfigResult<T> = Result<T, ConfigError>;
102
103impl From<ValueError> for ConfigError {
104    fn from(err: ValueError) -> Self {
105        match err {
106            ValueError::NoValue => ConfigError::PropertyHasNoValue("".to_string()),
107            ValueError::TypeMismatch { expected, actual } => {
108                ConfigError::TypeMismatch { expected, actual }
109            }
110            ValueError::ConversionFailed { from, to } => {
111                ConfigError::ConversionError(format!("From {from} to {to}"))
112            }
113            ValueError::ConversionError(msg) => ConfigError::ConversionError(msg),
114            ValueError::IndexOutOfBounds { index, len } => {
115                ConfigError::IndexOutOfBounds { index, len }
116            }
117        }
118    }
119}