use std::collections::BTreeMap;
use anyhow::{bail, Result};
fn top_level_field_constraints(body: &str) -> Vec<(String, Vec<String>)> {
let mut out = Vec::new();
let bytes = body.as_bytes();
let mut depth = 0i32;
let mut i = 0;
while i < bytes.len() {
match bytes[i] {
b'"' => i = skip_string(body, i).unwrap_or(body.len()),
b'(' | b'[' => {
depth += 1;
i += 1;
}
b')' | b']' => {
depth -= 1;
i += 1;
}
b'a'..=b'z' | b'_' if depth == 0 => {
let start = i;
while i < bytes.len()
&& (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_')
{
i += 1;
}
if bytes.get(i) == Some(&b':') {
let field = body[start..i].to_string();
i += 1;
while i < bytes.len() && bytes[i].is_ascii_whitespace() {
i += 1;
}
let mut names = Vec::new();
let mut j = i;
let openers: &[u8] = if bytes.get(j) == Some(&b'[') {
j += 1;
b"("
} else {
b"("
};
let _ = openers;
while j < bytes.len() {
while j < bytes.len() && bytes[j].is_ascii_whitespace() {
j += 1;
}
if bytes.get(j) != Some(&b'(') {
break;
}
j += 1;
let ns = j;
while j < bytes.len()
&& (bytes[j].is_ascii_alphanumeric() || bytes[j] == b'_')
{
j += 1;
}
if j > ns {
names.push(body[ns..j].to_string());
}
let mut d = 1i32;
while j < bytes.len() && d > 0 {
match bytes[j] {
b'(' => d += 1,
b')' => d -= 1,
b'"' => {
j = skip_string(body, j).unwrap_or(body.len());
continue;
}
_ => {}
}
j += 1;
}
if !body[i..].starts_with('[') {
break;
}
}
if names.iter().any(|n| n == "_") {
names.clear();
}
out.push((field, names));
}
}
_ => i += 1,
}
}
out
}
pub fn expand(query: &str, facets: &BTreeMap<String, Vec<String>>) -> Result<String> {
expand_with_types(query, facets, None)
}
pub fn expand_with_types(
query: &str,
facets: &BTreeMap<String, Vec<String>>,
node_types: Option<&crate::node_types::NodeTypes>,
) -> Result<String> {
let mut out = String::with_capacity(query.len());
let bytes = query.as_bytes();
let mut i = 0;
while i < bytes.len() {
match bytes[i] {
b'"' => {
let end = skip_string(query, i)?;
out.push_str(&query[i..end]);
i = end;
}
b';' => {
let end = query[i..].find('\n').map_or(query.len(), |n| i + n);
out.push_str(&query[i..end]);
i = end;
}
b'(' => {
let name_start = i + 1;
let name_end = name_start
+ query[name_start..]
.find(|c: char| !(c.is_alphanumeric() || c == '_'))
.unwrap_or(query.len() - name_start);
let name = &query[name_start..name_end];
if let Some(members) = facets.get(name) {
if members.is_empty() {
bail!("facet `{name}` has no members");
}
let close = matching_paren(query, i)?;
let body = expand_with_types(&query[name_end..close], facets, node_types)?;
let needed = top_level_field_constraints(&body);
let compatible = |m: &str| -> bool {
let Some(nt) = node_types else { return true };
let Some(declared) = nt.fields.get(m) else { return true };
needed.iter().all(|(f, want)| {
let Some(have) = declared.get(f) else { return false };
if want.is_empty() {
return true;
}
want.iter().any(|w| {
have.contains(w)
|| have.iter().any(|h| nt.closure(h).contains(w))
})
})
};
let kept: Vec<&String> =
members.iter().filter(|m| compatible(m)).collect();
if kept.is_empty() {
bail!(
"facet `{name}`: no member satisfies the field constraint(s) {needed:?}"
);
}
out.push('[');
for (k, m) in kept.iter().enumerate() {
if k > 0 {
out.push(' ');
}
out.push('(');
out.push_str(m);
out.push_str(&body);
out.push(')');
}
out.push(']');
i = close + 1;
} else {
out.push('(');
i += 1;
}
}
_ => {
let ch = query[i..].chars().next().unwrap();
out.push(ch);
i += ch.len_utf8();
}
}
}
Ok(out)
}
fn skip_string(s: &str, start: usize) -> Result<usize> {
let bytes = s.as_bytes();
let mut i = start + 1;
while i < bytes.len() {
match bytes[i] {
b'\\' => i += 2,
b'"' => return Ok(i + 1),
_ => i += 1,
}
}
bail!("unterminated string literal in query");
}
fn matching_paren(s: &str, open: usize) -> Result<usize> {
let bytes = s.as_bytes();
let mut depth = 0usize;
let mut i = open;
while i < bytes.len() {
match bytes[i] {
b'"' => i = skip_string(s, i)? - 1,
b'(' => depth += 1,
b')' => {
depth -= 1;
if depth == 0 {
return Ok(i);
}
}
b';' => {
i = s[i..].find('\n').map_or(s.len(), |n| i + n);
}
_ => {}
}
i += 1;
}
bail!("unbalanced parentheses in query");
}