1use crate::resolver::ResolverError;
2use fluent_syntax::parser::ParserError;
3use std::error::Error;
4
5#[derive(Debug, PartialEq, Clone)]
6pub enum EntryKind {
7 Message,
8 Term,
9 Function,
10}
11
12impl std::fmt::Display for EntryKind {
13 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14 match self {
15 Self::Message => f.write_str(data:"message"),
16 Self::Term => f.write_str(data:"term"),
17 Self::Function => f.write_str(data:"function"),
18 }
19 }
20}
21
22/// Core error type for Fluent runtime system.
23///
24/// It contains three main types of errors that may come up
25/// during runtime use of the fluent-bundle crate.
26#[derive(Debug, PartialEq, Clone)]
27pub enum FluentError {
28 /// An error which occurs when
29 /// [`FluentBundle::add_resource`](crate::bundle::FluentBundle::add_resource)
30 /// adds entries that are already registered in a given [`FluentBundle`](crate::FluentBundle).
31 ///
32 /// # Example
33 ///
34 /// ```
35 /// use fluent_bundle::{FluentBundle, FluentResource};
36 /// use unic_langid::langid;
37 ///
38 /// let ftl_string = String::from("intro = Welcome, { $name }.");
39 /// let res1 = FluentResource::try_new(ftl_string)
40 /// .expect("Could not parse an FTL string.");
41 ///
42 /// let ftl_string = String::from("intro = Hi, { $name }.");
43 /// let res2 = FluentResource::try_new(ftl_string)
44 /// .expect("Could not parse an FTL string.");
45 ///
46 /// let langid_en = langid!("en-US");
47 /// let mut bundle = FluentBundle::new(vec![langid_en]);
48 ///
49 /// bundle.add_resource(&res1)
50 /// .expect("Failed to add FTL resources to the bundle.");
51 ///
52 /// assert!(bundle.add_resource(&res2).is_err());
53 /// ```
54 Overriding {
55 kind: EntryKind,
56 id: String,
57 },
58 ParserError(ParserError),
59 ResolverError(ResolverError),
60}
61
62impl std::fmt::Display for FluentError {
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 match self {
65 Self::Overriding { kind: &EntryKind, id: &String } => {
66 write!(f, "Attempt to override an existing {}: \"{}\".", kind, id)
67 }
68 Self::ParserError(err: &ParserError) => write!(f, "Parser error: {}", err),
69 Self::ResolverError(err: &ResolverError) => write!(f, "Resolver error: {}", err),
70 }
71 }
72}
73
74impl Error for FluentError {}
75
76impl From<ResolverError> for FluentError {
77 fn from(error: ResolverError) -> Self {
78 Self::ResolverError(error)
79 }
80}
81
82impl From<ParserError> for FluentError {
83 fn from(error: ParserError) -> Self {
84 Self::ParserError(error)
85 }
86}
87