Skip to main content

amql_selector/
lib.rs

1//! Structural selector parser and matcher for annotation trees.
2
3mod matcher;
4pub mod selector;
5mod types;
6#[cfg(feature = "wasm")]
7mod index;
8
9use selector::parse_selector as parse_selector_impl;
10
11pub use types::{AttrName, TagName};
12
13pub use matcher::{filter_by_selector, filter_by_selector_indexed, matches_compound, Matchable};
14pub use selector::{
15    AttrOp, AttrPredicate, Combinator, CompoundSelector, Predicate, PredicateOp, PredicateValue,
16    SelectorAst,
17};
18
19use serde_json::Value as JsonValue;
20use std::borrow::Cow;
21
22/// Parse a CSS-like AQL selector string and return the AST.
23#[must_use = "parsing a selector is useless without inspecting the result"]
24pub fn parse_selector(selector: &str) -> Result<SelectorAst, String> {
25    parse_selector_impl(selector)
26}
27
28/// Convert a JSON value to a string representation.
29/// Returns `Cow::Borrowed` for the `String` variant to avoid allocation.
30pub(crate) fn json_value_to_string(v: &JsonValue) -> Cow<'_, str> {
31    match v {
32        JsonValue::String(s) => Cow::Borrowed(s.as_str()),
33        JsonValue::Bool(b) => Cow::Owned(b.to_string()),
34        JsonValue::Number(n) => Cow::Owned(n.to_string()),
35        JsonValue::Null => Cow::Borrowed(""),
36        other => Cow::Owned(other.to_string()),
37    }
38}
39