ferrite_config/
error.rs

1//! Error handling for the Ferrite configuration system.
2//!
3//! This module defines the error types that can occur during configuration
4//! operations. It uses the thiserror crate to provide detailed, context-aware
5//! error messages that help users understand and fix configuration issues.
6
7use std::path::PathBuf;
8use thiserror::Error;
9
10/// Represents all possible errors that can occur in the configuration system.
11/// Each variant provides specific context about what went wrong.
12#[derive(Error, Debug)]
13pub enum ConfigError {
14    /// Indicates an error occurred while reading or writing configuration
15    /// files
16    #[error("Failed to access configuration file: {0}")]
17    IoError(#[from] std::io::Error),
18
19    /// Indicates the configuration file couldn't be parsed as valid TOML
20    #[error("Failed to parse configuration TOML: {0}")]
21    TomlError(#[from] toml::de::Error),
22
23    /// Indicates an error occurred while serializing configuration to TOML
24    #[error("Failed to serialize configuration to TOML: {0}")]
25    TomlSerError(#[from] toml::ser::Error),
26
27    /// Indicates the configuration file wasn't found at the expected location
28    #[error("Configuration file not found at: {0}")]
29    FileNotFound(PathBuf),
30
31    /// Indicates a validation error in the configuration values
32    #[error("Invalid configuration value: {0}")]
33    ValidationError(String),
34
35    /// Indicates an unsupported configuration file version was detected
36    #[error(
37        "Unsupported configuration version {found} (supported: {supported})"
38    )]
39    VersionError { found: String, supported: String },
40
41    /// Indicates a failure to create or access the configuration directory
42    #[error("Failed to access configuration directory: {0}")]
43    DirectoryError(PathBuf),
44
45    /// Indicates an error in color value parsing or validation
46    #[error("Invalid color value: {0}")]
47    ColorError(String),
48
49    /// Indicates an invalid keyboard or mouse button configuration
50    #[error("Invalid input configuration: {0}")]
51    InputError(String),
52
53    #[error("Parse error: {0}")]
54    ParseError(String),
55
56    #[error("Environment configuration error: {0}")]
57    EnvError(String),
58
59    #[error("Invalid configuration path: {0}")]
60    InvalidPath(String),
61}
62
63/// Type alias for Result types using our ConfigError
64pub type Result<T> = std::result::Result<T, ConfigError>;
65
66/// Provides testing utilities for error handling
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_error_display() {
73        // Test that error messages are human-readable
74        let err = ConfigError::ValidationError(
75            "Zoom level must be positive".to_string(),
76        );
77        assert_eq!(
78            err.to_string(),
79            "Invalid configuration value: Zoom level must be positive"
80        );
81
82        let err = ConfigError::VersionError {
83            found:     "0.2".to_string(),
84            supported: "0.1".to_string(),
85        };
86        assert_eq!(
87            err.to_string(),
88            "Unsupported configuration version 0.2 (supported: 0.1)"
89        );
90    }
91}