Skip to main content

orcs_runtime/config/
error.rs

1//! Configuration errors.
2
3use std::path::PathBuf;
4use thiserror::Error;
5
6/// Configuration error type.
7#[derive(Debug, Error)]
8pub enum ConfigError {
9    /// Failed to read config file.
10    #[error("failed to read config file '{path}': {source}")]
11    ReadFile {
12        path: PathBuf,
13        #[source]
14        source: std::io::Error,
15    },
16
17    /// Failed to parse TOML.
18    #[error("failed to parse config file '{path}': {source}")]
19    ParseToml {
20        path: PathBuf,
21        #[source]
22        source: toml::de::Error,
23    },
24
25    /// Failed to serialize config.
26    #[error("failed to serialize config: {0}")]
27    Serialize(#[from] toml::ser::Error),
28
29    /// Failed to write config file.
30    #[error("failed to write config file '{path}': {source}")]
31    WriteFile {
32        path: PathBuf,
33        #[source]
34        source: std::io::Error,
35    },
36
37    /// Invalid environment variable value.
38    #[error("invalid value for environment variable '{name}': {message}")]
39    InvalidEnvVar { name: String, message: String },
40
41    /// Failed to create config directory.
42    #[error("failed to create config directory '{path}': {source}")]
43    CreateDir {
44        path: PathBuf,
45        #[source]
46        source: std::io::Error,
47    },
48
49    /// Profile not found.
50    #[error("profile '{name}' not found (searched: {searched:?})")]
51    ProfileNotFound {
52        name: String,
53        searched: Vec<PathBuf>,
54    },
55}
56
57impl ConfigError {
58    /// Creates a read file error.
59    pub fn read_file(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
60        Self::ReadFile {
61            path: path.into(),
62            source,
63        }
64    }
65
66    /// Creates a parse TOML error.
67    pub fn parse_toml(path: impl Into<PathBuf>, source: toml::de::Error) -> Self {
68        Self::ParseToml {
69            path: path.into(),
70            source,
71        }
72    }
73
74    /// Creates a write file error.
75    pub fn write_file(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
76        Self::WriteFile {
77            path: path.into(),
78            source,
79        }
80    }
81
82    /// Creates an invalid env var error.
83    pub fn invalid_env_var(name: impl Into<String>, message: impl Into<String>) -> Self {
84        Self::InvalidEnvVar {
85            name: name.into(),
86            message: message.into(),
87        }
88    }
89
90    /// Creates a create dir error.
91    pub fn create_dir(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
92        Self::CreateDir {
93            path: path.into(),
94            source,
95        }
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn error_display() {
105        let err = ConfigError::invalid_env_var("ORCS_DEBUG", "expected bool");
106        assert!(err.to_string().contains("ORCS_DEBUG"));
107        assert!(err.to_string().contains("expected bool"));
108    }
109}