orcs_runtime/config/
error.rs1use std::path::PathBuf;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
8pub enum ConfigError {
9 #[error("failed to read config file '{path}': {source}")]
11 ReadFile {
12 path: PathBuf,
13 #[source]
14 source: std::io::Error,
15 },
16
17 #[error("failed to parse config file '{path}': {source}")]
19 ParseToml {
20 path: PathBuf,
21 #[source]
22 source: toml::de::Error,
23 },
24
25 #[error("failed to serialize config: {0}")]
27 Serialize(#[from] toml::ser::Error),
28
29 #[error("failed to write config file '{path}': {source}")]
31 WriteFile {
32 path: PathBuf,
33 #[source]
34 source: std::io::Error,
35 },
36
37 #[error("invalid value for environment variable '{name}': {message}")]
39 InvalidEnvVar { name: String, message: String },
40
41 #[error("failed to create config directory '{path}': {source}")]
43 CreateDir {
44 path: PathBuf,
45 #[source]
46 source: std::io::Error,
47 },
48
49 #[error("profile '{name}' not found (searched: {searched:?})")]
51 ProfileNotFound {
52 name: String,
53 searched: Vec<PathBuf>,
54 },
55}
56
57impl ConfigError {
58 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 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 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 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 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}