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!(
37                    f,
38                    "failed to read language file for '{}': {}",
39                    locale, cause
40                )
41            }
42            LangError::Parse { locale, cause } => {
43                write!(
44                    f,
45                    "failed to parse language file for '{}': {}",
46                    locale, cause
47                )
48            }
49            LangError::NotLoaded { locale } => {
50                write!(f, "locale '{}' has not been loaded", locale)
51            }
52            LangError::InvalidLocale { locale } => {
53                write!(
54                    f,
55                    "locale '{}' is invalid; expected a single locale name without path separators",
56                    locale
57                )
58            }
59        }
60    }
61}
62
63impl std::error::Error for LangError {}