summaryrefslogtreecommitdiffstats
path: root/third_party/rust/fluent-bundle/src/resolver/errors.rs
blob: 831d8474a5aab3c4a7190fee27ccb213022498cf (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use fluent_syntax::ast::InlineExpression;
use std::error::Error;

#[derive(Debug, PartialEq, Clone)]
pub enum ReferenceKind {
    Function {
        id: String,
    },
    Message {
        id: String,
        attribute: Option<String>,
    },
    Term {
        id: String,
        attribute: Option<String>,
    },
    Variable {
        id: String,
    },
}

impl<T> From<&InlineExpression<T>> for ReferenceKind
where
    T: ToString,
{
    fn from(exp: &InlineExpression<T>) -> Self {
        match exp {
            InlineExpression::FunctionReference { id, .. } => Self::Function {
                id: id.name.to_string(),
            },
            InlineExpression::MessageReference { id, attribute } => Self::Message {
                id: id.name.to_string(),
                attribute: attribute.as_ref().map(|i| i.name.to_string()),
            },
            InlineExpression::TermReference { id, attribute, .. } => Self::Term {
                id: id.name.to_string(),
                attribute: attribute.as_ref().map(|i| i.name.to_string()),
            },
            InlineExpression::VariableReference { id, .. } => Self::Variable {
                id: id.name.to_string(),
            },
            _ => unreachable!(),
        }
    }
}

#[derive(Debug, PartialEq, Clone)]
pub enum ResolverError {
    Reference(ReferenceKind),
    NoValue(String),
    MissingDefault,
    Cyclic,
    TooManyPlaceables,
}

impl std::fmt::Display for ResolverError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Reference(exp) => match exp {
                ReferenceKind::Function { id } => write!(f, "Unknown function: {}()", id),
                ReferenceKind::Message {
                    id,
                    attribute: None,
                } => write!(f, "Unknown message: {}", id),
                ReferenceKind::Message {
                    id,
                    attribute: Some(attribute),
                } => write!(f, "Unknown attribute: {}.{}", id, attribute),
                ReferenceKind::Term {
                    id,
                    attribute: None,
                } => write!(f, "Unknown term: -{}", id),
                ReferenceKind::Term {
                    id,
                    attribute: Some(attribute),
                } => write!(f, "Unknown attribute: -{}.{}", id, attribute),
                ReferenceKind::Variable { id } => write!(f, "Unknown variable: ${}", id),
            },
            Self::NoValue(id) => write!(f, "No value: {}", id),
            Self::MissingDefault => f.write_str("No default"),
            Self::Cyclic => f.write_str("Cyclical dependency detected"),
            Self::TooManyPlaceables => f.write_str("Too many placeables"),
        }
    }
}

impl<T> From<&InlineExpression<T>> for ResolverError
where
    T: ToString,
{
    fn from(exp: &InlineExpression<T>) -> Self {
        Self::Reference(exp.into())
    }
}

impl Error for ResolverError {}