1use fluent_syntax::ast::InlineExpression;
2use std::error::Error;
3
4#[derive(Debug, PartialEq, Clone)]
5pub enum ReferenceKind {
6 Function {
7 id: String,
8 },
9 Message {
10 id: String,
11 attribute: Option<String>,
12 },
13 Term {
14 id: String,
15 attribute: Option<String>,
16 },
17 Variable {
18 id: String,
19 },
20}
21
22impl<T> From<&InlineExpression<T>> for ReferenceKind
23where
24 T: ToString,
25{
26 fn from(exp: &InlineExpression<T>) -> Self {
27 match exp {
28 InlineExpression::FunctionReference { id: &Identifier, .. } => Self::Function {
29 id: id.name.to_string(),
30 },
31 InlineExpression::MessageReference { id: &Identifier, attribute: &Option> } => Self::Message {
32 id: id.name.to_string(),
33 attribute: attribute.as_ref().map(|i: &Identifier| i.name.to_string()),
34 },
35 InlineExpression::TermReference { id: &Identifier, attribute: &Option>, .. } => Self::Term {
36 id: id.name.to_string(),
37 attribute: attribute.as_ref().map(|i: &Identifier| i.name.to_string()),
38 },
39 InlineExpression::VariableReference { id: &Identifier, .. } => Self::Variable {
40 id: id.name.to_string(),
41 },
42 _ => unreachable!(),
43 }
44 }
45}
46
47#[derive(Debug, PartialEq, Clone)]
48pub enum ResolverError {
49 Reference(ReferenceKind),
50 NoValue(String),
51 MissingDefault,
52 Cyclic,
53 TooManyPlaceables,
54}
55
56impl std::fmt::Display for ResolverError {
57 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58 match self {
59 Self::Reference(exp) => match exp {
60 ReferenceKind::Function { id } => write!(f, "Unknown function: {}()", id),
61 ReferenceKind::Message {
62 id,
63 attribute: None,
64 } => write!(f, "Unknown message: {}", id),
65 ReferenceKind::Message {
66 id,
67 attribute: Some(attribute),
68 } => write!(f, "Unknown attribute: {}.{}", id, attribute),
69 ReferenceKind::Term {
70 id,
71 attribute: None,
72 } => write!(f, "Unknown term: -{}", id),
73 ReferenceKind::Term {
74 id,
75 attribute: Some(attribute),
76 } => write!(f, "Unknown attribute: -{}.{}", id, attribute),
77 ReferenceKind::Variable { id } => write!(f, "Unknown variable: ${}", id),
78 },
79 Self::NoValue(id) => write!(f, "No value: {}", id),
80 Self::MissingDefault => f.write_str("No default"),
81 Self::Cyclic => f.write_str("Cyclical dependency detected"),
82 Self::TooManyPlaceables => f.write_str("Too many placeables"),
83 }
84 }
85}
86
87impl<T> From<&InlineExpression<T>> for ResolverError
88where
89 T: ToString,
90{
91 fn from(exp: &InlineExpression<T>) -> Self {
92 Self::Reference(exp.into())
93 }
94}
95
96impl Error for ResolverError {}
97