fluent_fallback/
errors.rs

1use fluent_bundle::FluentError;
2use std::error::Error;
3use unic_langid::LanguageIdentifier;
4
5#[derive(Debug, Eq, PartialEq)]
6pub enum LocalizationError {
7    Bundle {
8        error: FluentError,
9    },
10    Resolver {
11        id: String,
12        locale: LanguageIdentifier,
13        errors: Vec<FluentError>,
14    },
15    MissingMessage {
16        id: String,
17        locale: Option<LanguageIdentifier>,
18    },
19    MissingValue {
20        id: String,
21        locale: Option<LanguageIdentifier>,
22    },
23    SyncRequestInAsyncMode,
24}
25
26impl From<FluentError> for LocalizationError {
27    fn from(error: FluentError) -> Self {
28        Self::Bundle { error }
29    }
30}
31
32impl std::fmt::Display for LocalizationError {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        match self {
35            Self::Bundle { error } => write!(f, "[fluent][bundle] error: {}", error),
36            Self::Resolver { id, locale, errors } => {
37                let errors: Vec<String> = errors.iter().map(|err| err.to_string()).collect();
38                write!(
39                    f,
40                    "[fluent][resolver] errors in {}/{}: {}",
41                    locale,
42                    id,
43                    errors.join(", ")
44                )
45            }
46            Self::MissingMessage {
47                id,
48                locale: Some(locale),
49            } => write!(f, "[fluent] Missing message in locale {}: {}", locale, id),
50            Self::MissingMessage { id, locale: None } => {
51                write!(f, "[fluent] Couldn't find a message: {}", id)
52            }
53            Self::MissingValue {
54                id,
55                locale: Some(locale),
56            } => write!(
57                f,
58                "[fluent] Message has no value in locale {}: {}",
59                locale, id
60            ),
61            Self::MissingValue { id, locale: None } => {
62                write!(f, "[fluent] Couldn't find a message with value: {}", id)
63            }
64            Self::SyncRequestInAsyncMode => {
65                write!(f, "Triggered synchronous format while in async mode")
66            }
67        }
68    }
69}
70
71impl Error for LocalizationError {}