summaryrefslogtreecommitdiffstats
path: root/third_party/rust/fluent-fallback/src/errors.rs
blob: 9170fb1cb9b8c98d2275b413a57a6b573a709666 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use fluent_bundle::FluentError;
use std::error::Error;
use unic_langid::LanguageIdentifier;

#[derive(Debug, PartialEq)]
pub enum LocalizationError {
    Bundle {
        error: FluentError,
    },
    Resolver {
        id: String,
        locale: LanguageIdentifier,
        errors: Vec<FluentError>,
    },
    MissingMessage {
        id: String,
        locale: Option<LanguageIdentifier>,
    },
    MissingValue {
        id: String,
        locale: Option<LanguageIdentifier>,
    },
    SyncRequestInAsyncMode,
}

impl From<FluentError> for LocalizationError {
    fn from(error: FluentError) -> Self {
        Self::Bundle { error }
    }
}

impl std::fmt::Display for LocalizationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Bundle { error } => write!(f, "[fluent][bundle] error: {}", error),
            Self::Resolver { id, locale, errors } => {
                let errors: Vec<String> = errors.iter().map(|err| err.to_string()).collect();
                write!(
                    f,
                    "[fluent][resolver] errors in {}/{}: {}",
                    locale,
                    id,
                    errors.join(", ")
                )
            }
            Self::MissingMessage {
                id,
                locale: Some(locale),
            } => write!(f, "[fluent] Missing message in locale {}: {}", locale, id),
            Self::MissingMessage { id, locale: None } => {
                write!(f, "[fluent] Couldn't find a message: {}", id)
            }
            Self::MissingValue {
                id,
                locale: Some(locale),
            } => write!(
                f,
                "[fluent] Message has no value in locale {}: {}",
                locale, id
            ),
            Self::MissingValue { id, locale: None } => {
                write!(f, "[fluent] Couldn't find a message with value: {}", id)
            }
            Self::SyncRequestInAsyncMode => {
                write!(f, "Triggered synchronous format while in async mode")
            }
        }
    }
}

impl Error for LocalizationError {}