Skip to main content

bare_config/
error.rs

1//! Error types for configuration operations.
2//!
3//! This module provides the `ConfigError` enum and `ConfigResult` type
4//! for handling errors in configuration operations.
5
6use thiserror::Error;
7
8/// Error type for configuration operations.
9///
10/// This enum represents all possible errors that can occur
11/// when working with configuration content.
12#[derive(Error, Debug)]
13pub enum ConfigError {
14    /// Error parsing a configuration key.
15    ///
16    /// This error occurs when the key string format is invalid
17    /// or cannot be parsed into a valid Key.
18    #[error("Invalid key format: {0}")]
19    InvalidKey(String),
20
21    /// Error accessing a configuration value.
22    ///
23    /// This error occurs when the specified key does not exist
24    /// in the configuration.
25    #[error("Key not found: {0}")]
26    KeyNotFound(String),
27
28    /// Error parsing configuration content.
29    ///
30    /// This error occurs when the configuration string
31    /// cannot be parsed into a valid configuration structure.
32    #[error("Parse error: {0}")]
33    ParseError(String),
34
35    /// Error serializing configuration content.
36    ///
37    /// This error occurs when the configuration structure
38    /// cannot be serialized to a string.
39    #[error("Serialization error: {0}")]
40    SerializationError(String),
41
42    /// Error converting between types.
43    ///
44    /// This error occurs when a value cannot be converted
45    /// to the requested type.
46    #[error("Type conversion error: {0}")]
47    TypeConversionError(String),
48
49    /// Error when performing insert operation.
50    ///
51    /// This error occurs when trying to insert a value
52    /// that already exists.
53    #[error("Key already exists: {0}")]
54    KeyAlreadyExists(String),
55
56    /// Error when performing update operation.
57    ///
58    /// This error occurs when trying to update a value
59    /// that does not exist.
60    #[error("Key does not exist: {0}")]
61    KeyDoesNotExist(String),
62
63    /// Error when performing delete operation.
64    ///
65    /// This error occurs when trying to delete a value
66    /// that does not exist.
67    #[error("Cannot delete: {0}")]
68    DeleteError(String),
69
70    /// Generic error for other error cases.
71    ///
72    /// This error is used for errors that don't fit
73    /// into the other categories.
74    #[error("Error: {0}")]
75    Other(String),
76}
77
78/// Result type for configuration operations.
79///
80/// This is a convenience type alias for `Result<T, ConfigError>`.
81pub type ConfigResult<T> = Result<T, ConfigError>;
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn test_invalid_key_error() {
89        let error = ConfigError::InvalidKey("empty key".to_string());
90        assert_eq!(error.to_string(), "Invalid key format: empty key");
91    }
92
93    #[test]
94    fn test_key_not_found_error() {
95        let error = ConfigError::KeyNotFound("database.url".to_string());
96        assert_eq!(error.to_string(), "Key not found: database.url");
97    }
98
99    #[test]
100    fn test_parse_error() {
101        let error = ConfigError::ParseError("invalid JSON".to_string());
102        assert_eq!(error.to_string(), "Parse error: invalid JSON");
103    }
104
105    #[test]
106    fn test_serialization_error() {
107        let error = ConfigError::SerializationError("circular reference".to_string());
108        assert_eq!(error.to_string(), "Serialization error: circular reference");
109    }
110
111    #[test]
112    fn test_type_conversion_error() {
113        let error = ConfigError::TypeConversionError("cannot convert string to number".to_string());
114        assert_eq!(
115            error.to_string(),
116            "Type conversion error: cannot convert string to number"
117        );
118    }
119
120    #[test]
121    fn test_key_already_exists_error() {
122        let error = ConfigError::KeyAlreadyExists("database.url".to_string());
123        assert_eq!(error.to_string(), "Key already exists: database.url");
124    }
125
126    #[test]
127    fn test_key_does_not_exist_error() {
128        let error = ConfigError::KeyDoesNotExist("database.url".to_string());
129        assert_eq!(error.to_string(), "Key does not exist: database.url");
130    }
131
132    #[test]
133    fn test_delete_error() {
134        let error = ConfigError::DeleteError("cannot delete root".to_string());
135        assert_eq!(error.to_string(), "Cannot delete: cannot delete root");
136    }
137
138    #[test]
139    fn test_other_error() {
140        let error = ConfigError::Other("unknown error".to_string());
141        assert_eq!(error.to_string(), "Error: unknown error");
142    }
143
144    #[test]
145    fn test_config_result() {
146        let result: ConfigResult<i32> = Ok(42);
147        assert_eq!(result.expect("test should succeed"), 42);
148
149        let result: ConfigResult<i32> = Err(ConfigError::KeyNotFound("test".to_string()));
150        assert!(result.is_err());
151    }
152
153    #[test]
154    fn test_error_debug() {
155        let error = ConfigError::InvalidKey("test key".to_string());
156        let debug_str = format!("{:?}", error);
157        assert!(debug_str.contains("InvalidKey"));
158        assert!(debug_str.contains("test key"));
159    }
160}