Skip to main content

lang_lib/
error.rs

1use std::fmt;
2
3/// Errors produced by `lang-lib`.
4#[derive(Debug)]
5pub enum LangError {
6    /// The language file could not be read from disk.
7    Io {
8        /// The locale that failed to load.
9        locale: String,
10        /// The underlying I/O error message.
11        cause: String,
12    },
13    /// The language file was not valid TOML or contained non-string values.
14    Parse {
15        /// The locale whose file could not be parsed.
16        locale: String,
17        /// A description of the parse failure.
18        cause: String,
19    },
20    /// A locale was requested that has never been loaded.
21    NotLoaded {
22        /// The locale that was not found.
23        locale: String,
24    },
25    /// A locale identifier was rejected before any file access occurred.
26    InvalidLocale {
27        /// The locale that was rejected.
28        locale: String,
29    },
30}
31
32impl fmt::Display for LangError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            LangError::Io { locale, cause } => {
36                write!(f, "failed to read language file for '{locale}': {cause}")
37            }
38            LangError::Parse { locale, cause } => {
39                write!(f, "failed to parse language file for '{locale}': {cause}")
40            }
41            LangError::NotLoaded { locale } => {
42                write!(f, "locale '{locale}' has not been loaded")
43            }
44            LangError::InvalidLocale { locale } => {
45                write!(
46                    f,
47                    "locale '{locale}' is invalid; expected a single locale name without path separators"
48                )
49            }
50        }
51    }
52}
53
54impl std::error::Error for LangError {}