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 = r#"hello = Hello
127world = World"#;
128 let resource = parser::parse(content.to_string()).unwrap();
129 let keys = extract_message_keys(&resource);
130
131 assert!(keys.contains("hello"));
132 assert!(keys.contains("world"));
133 assert_eq!(keys.len(), 2);
134 }
135
136 #[test]
137 fn test_extract_variables() {
138 let content = "hello = Hello { $name }, you have { $count } messages";
139 let resource = parser::parse(content.to_string()).unwrap();
140
141 if let ast::Entry::Message(msg) = &resource.body[0] {
142 let vars = extract_variables_from_message(msg);
143 assert!(vars.contains("name"));
144 assert!(vars.contains("count"));
145 assert_eq!(vars.len(), 2);
146 } else {
147 panic!("Expected a message");
148 }
149 }
150
151 #[test]
152 fn test_extract_variables_from_select() {
153 let content = r#"count = { $num ->
154 [one] One item
155 *[other] { $num } items
156}"#;
157 let resource = parser::parse(content.to_string()).unwrap();
158
159 if let ast::Entry::Message(msg) = &resource.body[0] {
160 let vars = extract_variables_from_message(msg);
161 assert!(vars.contains("num"));
162 } else {
163 panic!("Expected a message");
164 }
165 }
166}