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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use crate::resolver::ResolverError;
use fluent_syntax::parser::ParserError;
use std::error::Error;

#[derive(Debug, PartialEq, Clone)]
pub enum EntryKind {
    Message,
    Term,
    Function,
}

impl std::fmt::Display for EntryKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Message => f.write_str("message"),
            Self::Term => f.write_str("term"),
            Self::Function => f.write_str("function"),
        }
    }
}

/// Core error type for Fluent runtime system.
///
/// It contains three main types of errors that may come up
/// during runtime use of the fluent-bundle crate.
#[derive(Debug, PartialEq, Clone)]
pub enum FluentError {
    /// An error which occurs when
    /// [`FluentBundle::add_resource`](crate::bundle::FluentBundle::add_resource)
    /// adds entries that are already registered in a given [`FluentBundle`](crate::FluentBundle).
    ///
    /// # Example
    ///
    /// ```
    /// use fluent_bundle::{FluentBundle, FluentResource};
    /// use unic_langid::langid;
    ///
    /// let ftl_string = String::from("intro = Welcome, { $name }.");
    /// let res1 = FluentResource::try_new(ftl_string)
    ///     .expect("Could not parse an FTL string.");
    ///
    /// let ftl_string = String::from("intro = Hi, { $name }.");
    /// let res2 = FluentResource::try_new(ftl_string)
    ///     .expect("Could not parse an FTL string.");
    ///
    /// let langid_en = langid!("en-US");
    /// let mut bundle = FluentBundle::new(vec![langid_en]);
    ///
    /// bundle.add_resource(&res1)
    ///     .expect("Failed to add FTL resources to the bundle.");
    ///
    /// assert!(bundle.add_resource(&res2).is_err());
    /// ```
    Overriding {
        kind: EntryKind,
        id: String,
    },
    ParserError(ParserError),
    ResolverError(ResolverError),
}

impl std::fmt::Display for FluentError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Overriding { kind, id } => {
                write!(f, "Attempt to override an existing {}: \"{}\".", kind, id)
            }
            Self::ParserError(err) => write!(f, "Parser error: {}", err),
            Self::ResolverError(err) => write!(f, "Resolver error: {}", err),
        }
    }
}

impl Error for FluentError {}

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

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