1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::parser::ParserError;
use std::error::Error;
use std::fmt::{self, Display};
use unic_langid_impl::LanguageIdentifierError;

#[derive(Debug)]
pub enum LocaleError {
    Unknown,
    ParserError(ParserError),
    LanguageIdentifierError(LanguageIdentifierError),
}

impl From<ParserError> for LocaleError {
    fn from(error: ParserError) -> LocaleError {
        LocaleError::ParserError(error)
    }
}

impl From<LanguageIdentifierError> for LocaleError {
    fn from(error: LanguageIdentifierError) -> LocaleError {
        LocaleError::LanguageIdentifierError(error)
    }
}

impl Error for LocaleError {
    fn description(&self) -> &str {
        match self {
            LocaleError::Unknown => "Unknown error",
            LocaleError::ParserError(p) => p.description(),
            LocaleError::LanguageIdentifierError(_) => "LangId Error",
        }
    }
}

impl Display for LocaleError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.description())
    }
}