1use std::fmt::{self, Display};
2use std::path::PathBuf;
3
4pub type ConfigResult<T> = Result<T, ConfigError>;
5
6#[derive(Debug, Clone, Eq, PartialEq)]
7pub enum ConfigError {
8 DuplicateKey(String),
9 InvalidArray(String),
10 InvalidLocaleTag(String),
11 InvalidString(String),
12 MissingField(&'static str),
13 UnexpectedSection(String),
14 UnknownKey { section: String, key: String },
15 UnreadableDirectory(PathBuf),
16}
17
18impl Display for ConfigError {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 match self {
21 Self::DuplicateKey(key) => write!(f, "duplicate config key `{key}`"),
22 Self::InvalidArray(value) => write!(f, "invalid string array `{value}`"),
23 Self::InvalidLocaleTag(tag) => write!(f, "invalid locale tag `{tag}`"),
24 Self::InvalidString(value) => write!(f, "invalid string value `{value}`"),
25 Self::MissingField(field) => write!(f, "missing required config field `{field}`"),
26 Self::UnexpectedSection(section) => write!(f, "unexpected config section `{section}`"),
27 Self::UnknownKey { section, key } => {
28 write!(f, "unknown config key `{section}.{key}`")
29 }
30 Self::UnreadableDirectory(path) => {
31 write!(f, "could not read directory `{}`", path.display())
32 }
33 }
34 }
35}
36
37impl std::error::Error for ConfigError {}