ch_config/config/
error.rs1use std::io;
2use std::path::PathBuf;
3
4use thiserror::Error;
5
6#[derive(Debug, Error)]
8pub enum ConfigError {
9 #[error("invalid configuration field `{field}`: {message}")]
11 Validation {
12 field: &'static str,
14 message: &'static str,
16 },
17
18 #[error("failed to load environment file `{path}`")]
20 Environment {
21 path: PathBuf,
23 #[source]
25 source: EnvironmentFileError,
26 },
27
28 #[error("configuration variable `{variable}` conflicts with `{variable}_FILE`")]
30 VariableConflict {
31 variable: &'static str,
33 },
34
35 #[error("configuration variable `{variable}` must be valid Unicode")]
37 VariableUnicode {
38 variable: String,
40 },
41
42 #[error("configuration variable `{variable}` {message}")]
44 VariableValue {
45 variable: &'static str,
47 message: &'static str,
49 },
50
51 #[error("failed to read configuration value selected by `{variable}_FILE`")]
53 VariableFile {
54 variable: &'static str,
56 #[source]
58 source: io::Error,
59 },
60}
61
62#[derive(Debug, Error)]
64pub enum EnvironmentFileError {
65 #[error("environment file could not be read")]
67 Io(#[source] io::Error),
68 #[error("invalid dotenv syntax at character index {index}")]
70 Syntax {
71 index: usize,
73 },
74 #[error("referenced environment variable is unavailable")]
76 Variable,
77 #[error("environment file could not be loaded")]
79 Other,
80}
81
82impl EnvironmentFileError {
83 pub(crate) fn from_dotenv(error: dotenvy::Error) -> Self {
84 match error {
85 dotenvy::Error::LineParse(_, index) => Self::Syntax { index },
86 dotenvy::Error::Io(source) => Self::Io(source),
87 dotenvy::Error::EnvVar(_) => Self::Variable,
88 _ => Self::Other,
89 }
90 }
91}
92
93impl ConfigError {
94 pub(crate) const fn validation(field: &'static str, message: &'static str) -> Self {
95 Self::Validation { field, message }
96 }
97
98 pub(crate) fn environment(path: PathBuf, error: dotenvy::Error) -> Self {
99 Self::Environment {
100 path,
101 source: EnvironmentFileError::from_dotenv(error),
102 }
103 }
104}
105
106pub type Result<T> = std::result::Result<T, ConfigError>;