summaryrefslogtreecommitdiffstats
path: root/intl/l10n/rust/fluent-ffi/src/text_elements.rs
blob: 0ffeffd4c73b0d3bae00ee047cf837fa7b8cf4a7 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

use fluent::FluentResource;
use fluent_syntax::ast;
use nsstring::nsCString;
use thin_vec::ThinVec;

#[repr(C)]
pub struct TextElementInfo {
    id: nsCString,
    attr: nsCString,
    text: nsCString,
}

struct TextElementsCollector<'a> {
    current_id: Option<String>,
    current_attr: Option<String>,
    elements: &'a mut ThinVec<TextElementInfo>,
}

impl<'a> TextElementsCollector<'a> {
    pub fn new(elements: &'a mut ThinVec<TextElementInfo>) -> Self {
        Self {
            current_id: None,
            current_attr: None,
            elements: elements,
        }
    }

    fn collect_inline_expression(&mut self, x: &ast::InlineExpression<&str>) {
        match x {
            ast::InlineExpression::StringLiteral { .. } => {}
            ast::InlineExpression::NumberLiteral { .. } => {}
            ast::InlineExpression::FunctionReference { arguments, .. } => {
                self.collect_call_arguments(arguments);
            }
            ast::InlineExpression::MessageReference { .. } => {}
            ast::InlineExpression::TermReference { arguments, .. } => {
                if let Some(y) = arguments {
                    self.collect_call_arguments(y);
                }
            }
            ast::InlineExpression::VariableReference { .. } => {}
            ast::InlineExpression::Placeable { expression } => {
                self.collect_expression(expression.as_ref());
            }
        }
    }

    fn collect_named_argument(&mut self, x: &ast::NamedArgument<&str>) {
        self.collect_inline_expression(&x.value);
    }

    fn collect_call_arguments(&mut self, x: &ast::CallArguments<&str>) {
        for y in x.positional.iter() {
            self.collect_inline_expression(y);
        }
        for y in x.named.iter() {
            self.collect_named_argument(y);
        }
    }

    fn collect_variant(&mut self, x: &ast::Variant<&str>) {
        self.collect_pattern(&x.value);
    }

    fn collect_expression(&mut self, x: &ast::Expression<&str>) {
        match x {
            ast::Expression::Select { selector, variants } => {
                self.collect_inline_expression(selector);
                for y in variants.iter() {
                    self.collect_variant(y);
                }
            }
            ast::Expression::Inline(i) => {
                self.collect_inline_expression(i);
            }
        }
    }

    fn collect_pattern_element(&mut self, x: &ast::PatternElement<&str>) {
        match x {
            ast::PatternElement::TextElement { value } => {
                self.elements.push(TextElementInfo {
                    id: self
                        .current_id
                        .as_ref()
                        .map_or_else(|| nsCString::new(), nsCString::from),
                    attr: self
                        .current_attr
                        .as_ref()
                        .map_or_else(|| nsCString::new(), nsCString::from),
                    text: nsCString::from(*value),
                });
            }
            ast::PatternElement::Placeable { expression } => {
                self.collect_expression(expression);
            }
        }
    }

    fn collect_pattern(&mut self, x: &ast::Pattern<&str>) {
        for y in x.elements.iter() {
            self.collect_pattern_element(y);
        }
    }

    fn collect_attribute(&mut self, x: &ast::Attribute<&str>) {
        self.current_attr = Some(x.id.name.to_string());

        self.collect_pattern(&x.value);
    }

    fn collect_message(&mut self, x: &ast::Message<&str>) {
        self.current_id = Some(x.id.name.to_string());
        self.current_attr = None;

        if let Some(ref y) = x.value {
            self.collect_pattern(y);
        }
        for y in x.attributes.iter() {
            self.collect_attribute(y);
        }
    }

    fn collect_term(&mut self, x: &ast::Term<&str>) {
        self.current_id = Some(x.id.name.to_string());
        self.current_attr = None;

        self.collect_pattern(&x.value);
        for y in x.attributes.iter() {
            self.collect_attribute(y);
        }
    }

    pub fn collect_entry(&mut self, x: &ast::Entry<&str>) {
        match x {
            ast::Entry::Message(m) => {
                self.collect_message(m);
            }
            ast::Entry::Term(t) => {
                self.collect_term(t);
            }
            ast::Entry::Comment(_) => {}
            ast::Entry::GroupComment(_) => {}
            ast::Entry::ResourceComment(_) => {}
            ast::Entry::Junk { .. } => {}
        }
    }
}

#[no_mangle]
pub extern "C" fn fluent_resource_get_text_elements(
    res: &FluentResource,
    elements: &mut ThinVec<TextElementInfo>,
) {
    let mut collector = TextElementsCollector::new(elements);

    for entry in res.entries() {
        collector.collect_entry(entry);
    }
}