es_fluent_cli/ftl/
parse.rs1use anyhow::Result;
7use fluent_syntax::{ast, parser};
8use std::collections::HashSet;
9use std::fs;
10use std::path::Path;
11
12pub fn parse_ftl_file(ftl_path: &Path) -> Result<ast::Resource<String>> {
17 if !ftl_path.exists() {
18 return Ok(ast::Resource { body: Vec::new() });
19 }
20
21 let content = fs::read_to_string(ftl_path)?;
22
23 if content.trim().is_empty() {
24 return Ok(ast::Resource { body: Vec::new() });
25 }
26
27 match parser::parse(content) {
28 Ok(res) => Ok(res),
29 Err((res, _)) => Ok(res), }
31}
32
33pub fn extract_message_keys(resource: &ast::Resource<String>) -> HashSet<String> {
35 resource
36 .body
37 .iter()
38 .filter_map(|entry| {
39 if let ast::Entry::Message(msg) = entry {
40 Some(msg.id.name.clone())
41 } else {
42 None
43 }
44 })
45 .collect()
46}
47
48pub fn extract_variables_from_message(msg: &ast::Message<String>) -> HashSet<String> {
50 let mut variables = HashSet::new();
51 if let Some(ref value) = msg.value {
52 extract_variables_from_pattern(value, &mut variables);
53 }
54 for attr in &msg.attributes {
55 extract_variables_from_pattern(&attr.value, &mut variables);
56 }
57 variables
58}
59
60pub fn extract_variables_from_pattern(
62 pattern: &ast::Pattern<String>,
63 variables: &mut HashSet<String>,
64) {
65 for element in &pattern.elements {
66 if let ast::PatternElement::Placeable { expression } = element {
67 extract_variables_from_expression(expression, variables);
68 }
69 }
70}
71
72fn extract_variables_from_expression(
74 expression: &ast::Expression<String>,
75 variables: &mut HashSet<String>,
76) {
77 match expression {
78 ast::Expression::Inline(inline) => {
79 extract_variables_from_inline(inline, variables);
80 },
81 ast::Expression::Select { selector, variants } => {
82 extract_variables_from_inline(selector, variables);
83 for variant in variants {
84 extract_variables_from_pattern(&variant.value, variables);
85 }
86 },
87 }
88}
89
90fn extract_variables_from_inline(
92 inline: &ast::InlineExpression<String>,
93 variables: &mut HashSet<String>,
94) {
95 match inline {
96 ast::InlineExpression::VariableReference { id } => {
97 variables.insert(id.name.clone());
98 },
99 ast::InlineExpression::FunctionReference { arguments, .. } => {
100 for arg in &arguments.positional {
101 extract_variables_from_inline(arg, variables);
102 }
103 for arg in &arguments.named {
104 extract_variables_from_inline(&arg.value, variables);
105 }
106 },
107 ast::InlineExpression::Placeable { expression } => {
108 extract_variables_from_expression(expression, variables);
109 },
110 _ => {},
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn test_parse_ftl_file_nonexistent() {
120 let result = parse_ftl_file(Path::new("/nonexistent/path.ftl")).unwrap();
121 assert!(result.body.is_empty());
122 }
123
124 #[test]
125 fn test_extract_message_keys() {
126 let content = "hello = Hello\nworld = World";
127 let resource = parser::parse(content.to_string()).unwrap();
128 let keys = extract_message_keys(&resource);
129
130 assert!(keys.contains("hello"));
131 assert!(keys.contains("world"));
132 assert_eq!(keys.len(), 2);
133 }
134
135 #[test]
136 fn test_extract_variables() {
137 let content = "hello = Hello { $name }, you have { $count } messages";
138 let resource = parser::parse(content.to_string()).unwrap();
139
140 if let ast::Entry::Message(msg) = &resource.body[0] {
141 let vars = extract_variables_from_message(msg);
142 assert!(vars.contains("name"));
143 assert!(vars.contains("count"));
144 assert_eq!(vars.len(), 2);
145 } else {
146 panic!("Expected a message");
147 }
148 }
149
150 #[test]
151 fn test_extract_variables_from_select() {
152 let content = r#"count = { $num ->
153 [one] One item
154 *[other] { $num } items
155}"#;
156 let resource = parser::parse(content.to_string()).unwrap();
157
158 if let ast::Entry::Message(msg) = &resource.body[0] {
159 let vars = extract_variables_from_message(msg);
160 assert!(vars.contains("num"));
161 } else {
162 panic!("Expected a message");
163 }
164 }
165}