Skip to main content

fel_core/
interpolation.rs

1//! Helpers for `{{expression}}` message interpolation (locale spec §3.3.1).
2#![allow(missing_docs)]
3#![allow(clippy::missing_docs_in_private_items)]
4
5use crate::ast::{Expr, UnaryOp};
6
7/// Functions that read response or instance data while spelling no `$` or `@` sigil: the row before, the
8/// row after, the enclosing row, and a named secondary instance. An expression that calls one reads data,
9/// so a `null` from it is an absence to render, not an author's typo (locale §3.3.1 rule 3a).
10const DATA_READING_FUNCTIONS: [&str; 4] = ["prev", "next", "parent", "instance"];
11
12/// True when the AST reads instance data: a `$field`, an `@context`, or a repeat navigation call
13/// (locale §3.3.1 rule 3a).
14///
15/// The rule keeps a `null` result literal only for an expression that reads none — an author's typo or a
16/// broken operator sequence — so that a missing `$field` still renders as empty text. Navigation across
17/// rows belongs on the reading side: `{{prev().total}}` is null on the first row for a good reason, and a
18/// respondent should read an empty space there, not the template.
19pub fn expr_references_instance_data(expr: &Expr) -> bool {
20    match expr {
21        Expr::FieldRef { .. } | Expr::ContextRef { .. } => true,
22        Expr::FunctionCall { name, args } => {
23            DATA_READING_FUNCTIONS.contains(&name.as_str())
24                || args.iter().any(expr_references_instance_data)
25        }
26        Expr::Null
27        | Expr::Boolean(_)
28        | Expr::Number(_)
29        | Expr::String(_)
30        | Expr::DateLiteral(_)
31        | Expr::DateTimeLiteral(_)
32        | Expr::VarRef { .. } => false,
33        Expr::Array(elems) => elems.iter().any(expr_references_instance_data),
34        Expr::Object(entries) => entries.iter().any(|(_, v)| expr_references_instance_data(v)),
35        Expr::UnaryOp { operand, .. } => expr_references_instance_data(operand),
36        Expr::PostfixAccess { expr, .. } => expr_references_instance_data(expr),
37        Expr::BinaryOp { left, right, .. }
38        | Expr::Membership {
39            value: left,
40            container: right,
41            ..
42        }
43        | Expr::NullCoalesce { left, right } => {
44            expr_references_instance_data(left) || expr_references_instance_data(right)
45        }
46        Expr::Ternary {
47            condition,
48            then_branch,
49            else_branch,
50        }
51        | Expr::IfThenElse {
52            condition,
53            then_branch,
54            else_branch,
55        } => {
56            expr_references_instance_data(condition)
57                || expr_references_instance_data(then_branch)
58                || expr_references_instance_data(else_branch)
59        }
60        Expr::LetBinding { value, body, .. } => {
61            expr_references_instance_data(value) || expr_references_instance_data(body)
62        }
63    }
64}
65
66/// True when the AST is only literals and unary `not`/`!`/`-` on such (locale §3.3.1).
67pub fn expr_is_interpolation_static_literal(expr: &Expr) -> bool {
68    match expr {
69        Expr::Null
70        | Expr::Boolean(_)
71        | Expr::Number(_)
72        | Expr::String(_)
73        | Expr::DateLiteral(_)
74        | Expr::DateTimeLiteral(_) => true,
75        Expr::Array(elems) => elems.iter().all(expr_is_interpolation_static_literal),
76        Expr::Object(entries) => entries
77            .iter()
78            .all(|(_, v)| expr_is_interpolation_static_literal(v)),
79        Expr::UnaryOp {
80            op: UnaryOp::Not | UnaryOp::Neg,
81            operand,
82            ..
83        } => expr_is_interpolation_static_literal(operand),
84        Expr::VarRef { .. }
85        | Expr::ContextRef { .. }
86        | Expr::FunctionCall { .. }
87        | Expr::PostfixAccess { .. }
88        | Expr::BinaryOp { .. }
89        | Expr::Ternary { .. }
90        | Expr::IfThenElse { .. }
91        | Expr::Membership { .. }
92        | Expr::NullCoalesce { .. }
93        | Expr::LetBinding { .. }
94        | Expr::FieldRef { .. } => false,
95    }
96}