#![allow(clippy::missing_docs_in_private_items)]
use std::collections::{HashMap, HashSet};
use std::sync::LazyLock;
use regex::Regex;
use serde_json::{Map, Value};
static RE_TRAILING_INDEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[\d+\]$").expect("valid regex"));
static RE_PATH_SEGMENT: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"([^\[.\]]+\[\d+\]|[^\[.\]]+)").expect("valid regex"));
static RE_REPEAT_SEG: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(.+)\[(\d+)\]$").expect("valid regex"));
static RE_INDEX_BRACKET: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[(\d+)\]").expect("valid regex"));
static RE_REPEAT_ALIAS: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(.*)\[(\d+)\]\.([^\[.\]]+)$").expect("valid regex"));
#[derive(Debug, Clone)]
pub struct PrepareHostInput<'a> {
pub expression: &'a str,
pub current_item_path: &'a str,
pub replace_self_ref: bool,
pub repeat_counts: &'a HashMap<String, u32>,
pub field_paths: &'a [String],
}
#[derive(Debug, Clone)]
struct RepeatAncestor {
group_path: String,
index: usize,
}
fn is_ident_char(c: char) -> bool {
c.is_ascii_alphanumeric() || c == '_'
}
fn is_ident_start(c: char) -> bool {
c.is_ascii_alphabetic() || c == '_'
}
struct QuoteAwareCursor<'a> {
chars: &'a [char],
idx: usize,
quote: Option<char>,
}
impl<'a> QuoteAwareCursor<'a> {
fn new(chars: &'a [char]) -> Self {
Self {
chars,
idx: 0,
quote: None,
}
}
fn len(&self) -> usize {
self.chars.len()
}
fn idx(&self) -> usize {
self.idx
}
fn at(&self, i: usize) -> Option<char> {
self.chars.get(i).copied()
}
fn step_quote(&mut self, out: &mut String) -> bool {
let Some(c) = self.at(self.idx) else {
return false;
};
if let Some(q) = self.quote {
out.push(c);
if c == '\\' && self.idx + 1 < self.len() {
out.push(self.chars[self.idx + 1]);
self.idx += 2;
return true;
}
if c == q {
self.quote = None;
}
self.idx += 1;
return true;
}
if c == '"' || c == '\'' {
self.quote = Some(c);
out.push(c);
self.idx += 1;
return true;
}
false
}
fn push_current(&mut self, out: &mut String) {
if let Some(c) = self.at(self.idx) {
out.push(c);
self.idx += 1;
}
}
fn advance_to(&mut self, next: usize) {
self.idx = next;
}
}
fn current_field_leaf(item_path: &str) -> String {
let segments = path_segments(item_path);
let Some(last) = segments.last().map(String::as_str) else {
return String::new();
};
RE_TRAILING_INDEX.replace(last, "").to_string()
}
fn path_segments(path: &str) -> Vec<String> {
RE_PATH_SEGMENT
.find_iter(path)
.map(|m| m.as_str().to_string())
.collect()
}
fn get_repeat_ancestors(path: &str, repeats: &HashMap<String, u32>) -> Vec<RepeatAncestor> {
let mut ancestors = Vec::new();
let mut current = String::new();
for segment in path_segments(path) {
if let Some(caps) = RE_REPEAT_SEG.captures(&segment) {
let g1 = caps.get(1).expect("group 1").as_str();
let idx: usize = caps
.get(2)
.and_then(|m| m.as_str().parse().ok())
.unwrap_or(0);
let new_base = if current.is_empty() {
g1.to_string()
} else {
format!("{current}.{g1}")
};
if repeats.contains_key(&new_base) {
ancestors.push(RepeatAncestor {
group_path: new_base.clone(),
index: idx,
});
}
current = format!("{new_base}[{idx}]");
} else {
current = if current.is_empty() {
segment.clone()
} else {
format!("{current}.{segment}")
};
}
}
ancestors
}
fn to_fel_indexed_path(path: &str) -> String {
RE_INDEX_BRACKET
.replace_all(path, |caps: ®ex::Captures| {
let n: i64 = caps
.get(1)
.and_then(|m| m.as_str().parse().ok())
.unwrap_or(0);
format!("[{}]", n + 1)
})
.to_string()
}
fn to_repeat_wildcard_path(alias: &str) -> String {
match alias.rfind('.') {
Some(i) => format!("{}[*].{}", &alias[..i], &alias[i + 1..]),
None => format!("{alias}[*]"),
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RepeatAliases {
aliases: Vec<String>,
}
impl RepeatAliases {
pub fn from_field_paths<'a>(paths: impl IntoIterator<Item = &'a str>) -> Self {
let mut seen = HashSet::new();
let mut aliases = Vec::new();
for p in paths {
if let Some(caps) = RE_REPEAT_ALIAS.captures(p) {
let base = caps.get(1).expect("base").as_str();
let field = caps.get(3).expect("field").as_str();
let alias = format!("{base}.{field}");
if seen.insert(alias.clone()) {
aliases.push(alias);
}
}
}
aliases.sort_by_key(|a| std::cmp::Reverse(a.len()));
Self { aliases }
}
pub fn as_slice(&self) -> &[String] {
&self.aliases
}
}
fn replace_bare_current_field_refs(expr: &str, current_field: &str) -> String {
if current_field.is_empty() || !expr.contains('$') {
return expr.to_string();
}
let chars: Vec<char> = expr.chars().collect();
let mut out = String::with_capacity(expr.len() + current_field.len());
let mut cur = QuoteAwareCursor::new(&chars);
while cur.idx() < cur.len() {
if cur.step_quote(&mut out) {
continue;
}
let i = cur.idx();
let c = chars[i];
if c == '$' {
let prev_ok = i == 0 || !is_ident_char(chars[i - 1]);
let next_ok = i + 1 >= chars.len() || !is_ident_char(chars[i + 1]);
if prev_ok && next_ok {
out.push('$');
out.push_str(current_field);
cur.advance_to(i + 1);
continue;
}
}
cur.push_current(&mut out);
}
out
}
fn resolve_qualified_group_refs(expression: &str, repeat_ancestors: &[RepeatAncestor]) -> String {
if repeat_ancestors.is_empty() {
return expression.to_string();
}
let mut replacements: Vec<(String, String, bool)> = Vec::new();
for (idx, anc) in repeat_ancestors.iter().enumerate() {
let group_name = anc
.group_path
.rsplit('.')
.next()
.unwrap_or(anc.group_path.as_str())
.to_string();
let concrete_prefix = format!("{}[{}]", anc.group_path, anc.index);
let is_innermost = idx == repeat_ancestors.len() - 1;
replacements.push((group_name, concrete_prefix, is_innermost));
}
replacements.sort_by(|a, b| b.0.len().cmp(&a.0.len()));
let mut result = expression.to_string();
for (group_name, concrete_prefix, is_innermost) in replacements {
result = replace_qualified_group_ref_outside_quotes(
&result,
&group_name,
&concrete_prefix,
is_innermost,
);
}
result
}
fn replace_qualified_group_ref_outside_quotes(
expr: &str,
group_name: &str,
concrete_prefix: &str,
is_innermost: bool,
) -> String {
if group_name.is_empty() {
return expr.to_string();
}
let chars: Vec<char> = expr.chars().collect();
let group_chars: Vec<char> = group_name.chars().collect();
let mut out = String::with_capacity(expr.len());
let mut cur = QuoteAwareCursor::new(&chars);
while cur.idx() < cur.len() {
if cur.step_quote(&mut out) {
continue;
}
let i = cur.idx();
let has_group = i + 1 + group_chars.len() < chars.len()
&& chars[i] == '$'
&& chars[i + 1..i + 1 + group_chars.len()] == group_chars[..];
let dot_idx = i + 1 + group_chars.len();
if has_group && chars[dot_idx] == '.' && dot_idx + 1 < chars.len() {
let field_start = dot_idx + 1;
if is_ident_start(chars[field_start]) {
let mut j = field_start + 1;
while j < chars.len() && is_ident_char(chars[j]) {
j += 1;
}
let field: String = chars[field_start..j].iter().collect();
if is_innermost {
out.push_str(&field);
} else {
out.push_str(&format!("{}.{field}", to_fel_indexed_path(concrete_prefix)));
}
cur.advance_to(j);
continue;
}
}
cur.push_current(&mut out);
}
out
}
fn is_blocked_implicit_prefix(c: char) -> bool {
is_ident_char(c) || matches!(c, '$' | '@' | '.')
}
fn suffix_continues_alias(c: Option<char>) -> bool {
c.is_some_and(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '[')
}
fn slice_eq_chars(chars: &[char], start: usize, needle: &[char]) -> bool {
start + needle.len() <= chars.len() && chars[start..start + needle.len()] == needle[..]
}
fn replace_implicit_repeat_alias(expr: &str, alias: &str, wildcard: &str) -> String {
if alias.is_empty() {
return expr.to_string();
}
let chars: Vec<char> = expr.chars().collect();
let needle: Vec<char> = alias.chars().collect();
let mut out = String::with_capacity(expr.len());
let mut i = 0;
while i < chars.len() {
if slice_eq_chars(&chars, i, &needle)
&& (i == 0 || !is_blocked_implicit_prefix(chars[i - 1]))
&& !suffix_continues_alias(chars.get(i + needle.len()).copied())
{
out.push_str(wildcard);
i += needle.len();
} else {
out.push(chars[i]);
i += 1;
}
}
out
}
fn replace_explicit_dollar_repeat_alias(expr: &str, alias: &str, wildcard: &str) -> String {
if alias.is_empty() {
return expr.to_string();
}
let chars: Vec<char> = expr.chars().collect();
let needle: Vec<char> = alias.chars().collect();
let mut out = String::with_capacity(expr.len());
let mut i = 0;
while i < chars.len() {
if chars.get(i) == Some(&'$')
&& slice_eq_chars(&chars, i + 1, &needle)
&& !suffix_continues_alias(chars.get(i + 1 + needle.len()).copied())
{
out.push_str(wildcard);
i += 1 + needle.len();
} else {
out.push(chars[i]);
i += 1;
}
}
out
}
fn apply_repeat_alias_pass(expr: &str, alias: &str, wildcard_with_dollar: &str) -> String {
let after_implicit = replace_implicit_repeat_alias(expr, alias, wildcard_with_dollar);
replace_explicit_dollar_repeat_alias(&after_implicit, alias, wildcard_with_dollar)
}
#[derive(Debug, Clone)]
pub struct PrepareHostOptions {
pub expression: String,
pub current_item_path: String,
pub replace_self_ref: bool,
pub repeat_counts: HashMap<String, u32>,
pub field_paths: Vec<String>,
}
pub fn host_options_from_json(obj: &Map<String, Value>) -> Result<PrepareHostOptions, String> {
let expression = obj
.get("expression")
.and_then(|x| x.as_str())
.ok_or("expression (string) is required")?
.to_string();
let current_item_path = obj
.get("currentItemPath")
.or_else(|| obj.get("current_item_path"))
.and_then(|x| x.as_str())
.unwrap_or("")
.to_string();
let replace_self_ref = obj
.get("replaceSelfRef")
.or_else(|| obj.get("replace_self_ref"))
.and_then(|x| x.as_bool())
.unwrap_or(false);
let mut repeat_counts: HashMap<String, u32> = HashMap::new();
if let Some(rc) = obj
.get("repeatCounts")
.or_else(|| obj.get("repeat_counts"))
.and_then(|x| x.as_object())
{
for (k, val) in rc.iter() {
if let Some(n) = val
.as_u64()
.or_else(|| val.as_i64().filter(|&i| i >= 0).map(|i| i as u64))
&& n <= u32::MAX as u64
{
repeat_counts.insert(k.clone(), n as u32);
}
}
}
let mut field_paths: Vec<String> = Vec::new();
if let Some(arr) = obj
.get("fieldPaths")
.or_else(|| obj.get("field_paths"))
.and_then(|x| x.as_array())
{
for p in arr {
if let Some(s) = p.as_str() {
field_paths.push(s.to_string());
}
}
} else if let Some(vmap) = obj
.get("valuesByPath")
.or_else(|| obj.get("values_by_path"))
.and_then(|x| x.as_object())
{
field_paths.extend(vmap.keys().cloned());
}
Ok(PrepareHostOptions {
expression,
current_item_path,
replace_self_ref,
repeat_counts,
field_paths,
})
}
pub fn prepare(opts: &PrepareHostOptions) -> String {
prepare_for_host(PrepareHostInput {
expression: &opts.expression,
current_item_path: &opts.current_item_path,
replace_self_ref: opts.replace_self_ref,
repeat_counts: &opts.repeat_counts,
field_paths: &opts.field_paths,
})
}
pub fn prepare_for_host(input: PrepareHostInput<'_>) -> String {
prepare_with_aliases(
input.expression,
input.current_item_path,
input.replace_self_ref,
input.repeat_counts,
&RepeatAliases::from_field_paths(input.field_paths.iter().map(String::as_str)),
)
}
pub fn prepare_with_aliases(
expression: &str,
current_item_path: &str,
replace_self_ref: bool,
repeat_counts: &HashMap<String, u32>,
aliases: &RepeatAliases,
) -> String {
let leaf = current_field_leaf(current_item_path);
let mut normalized = expression.to_string();
if replace_self_ref && !leaf.is_empty() {
normalized = replace_bare_current_field_refs(&normalized, &leaf);
}
let ancestors = get_repeat_ancestors(current_item_path, repeat_counts);
normalized = resolve_qualified_group_refs(&normalized, &ancestors);
for alias in aliases.as_slice() {
let wildcard = format!("${}", to_repeat_wildcard_path(alias));
normalized = apply_repeat_alias_pass(&normalized, alias, &wildcard);
}
normalized
}
#[cfg(test)]
mod tests {
use super::*;
fn prep(
expr: &str,
path: &str,
replace: bool,
repeats: &[(&str, u32)],
paths: &[&str],
) -> String {
let rc: HashMap<String, u32> = repeats.iter().map(|(k, v)| (k.to_string(), *v)).collect();
let fp: Vec<String> = paths.iter().map(|s| (*s).to_string()).collect();
prepare_for_host(PrepareHostInput {
expression: expr,
current_item_path: path,
replace_self_ref: replace,
repeat_counts: &rc,
field_paths: &fp,
})
}
#[test]
fn bare_dollar_replaces_with_current_field_leaf() {
assert_eq!(prep("$ * 2", "items[0].qty", true, &[], &[]), "$qty * 2");
}
#[test]
fn qualified_innermost_repeat_becomes_sibling_field() {
let out = prep(
"$line_items.qty * $line_items.price",
"line_items[0].total",
false,
&[("line_items", 2)],
&[],
);
assert_eq!(out, "qty * price");
}
#[test]
fn qualified_outer_repeat_uses_fel_one_based_indices() {
let out = prep(
"$orders.discount + $items.qty",
"orders[0].items[1].line_total",
false,
&[("orders", 1), ("orders[0].items", 3)],
&[],
);
assert_eq!(out, "orders[1].discount + qty");
}
#[test]
fn repeat_alias_implicit_and_explicit() {
let out = prep(
"rows.score + $rows.score + x.rows.score",
"",
false,
&[],
&["rows[0].score", "rows[1].score"],
);
assert_eq!(out, "$rows[*].score + $rows[*].score + x.rows.score");
}
#[test]
fn prebuilt_aliases_match_per_call_inference_and_sort_longest_first() {
let aliases = RepeatAliases::from_field_paths([
"rows[0].s",
"rows[1].s",
"rows[0].score",
"plain",
"rows[0].sub[2].x",
]);
assert_eq!(
aliases.as_slice(),
&[
"rows[0].sub.x".to_string(),
"rows.score".to_string(),
"rows.s".to_string()
]
);
let counts = HashMap::new();
assert_eq!(
prepare_with_aliases("rows.score + $rows.s", "", false, &counts, &aliases),
prep(
"rows.score + $rows.s",
"",
false,
&[],
&[
"rows[0].s",
"rows[1].s",
"rows[0].score",
"plain",
"rows[0].sub[2].x"
],
),
);
}
#[test]
fn escaped_quote_inside_literal_preserves_dollar_group_ref() {
let out = prep(
"if($line_items.qty > 0, 'it\\'s $line_items.qty', \"ok\")",
"line_items[0].total",
false,
&[("line_items", 2)],
&[],
);
assert!(
out.contains("$line_items.qty") || out.contains("line_items.qty"),
"rewrites outside quotes only; got: {out}"
);
assert!(out.contains("it's") || out.contains("it\\'s"));
}
#[test]
fn qualified_repeat_reference_inside_string_literal_is_not_rewritten() {
let out = prep(
"if($line_items.qty > 0, '$line_items.qty', \"x $line_items.qty y\")",
"line_items[0].total",
false,
&[("line_items", 2)],
&[],
);
assert_eq!(
out,
"if(qty > 0, '$line_items.qty', \"x $line_items.qty y\")"
);
}
}