qua 0.75.0

Structure-aware query CLI for the Quarb engine
//! A run of steps prints so that it reads back as the run it was.
//! The printer writes each step alone, and a step up with no name is
//! the bare axis `\`, the canonical spelling: two of them glued into
//! `\\`, the ancestor axis, which is another query. `--expand` of
//! `/../..` printed a query that answers every ancestor, and the
//! thread tree drew the level so (lang's finding of 2026-11-28, from
//! the review of the recipes). Where a run is joined, a step up with
//! no name before another step up keeps its star.

use quarb::{Defs, QueryResult, Value};

fn doc() -> quarb_json::JsonAdapter {
    quarb_json::JsonAdapter::parse(
        r#"{"books":[
            {"title":"Iliad","tags":["epic","verse"]},
            {"title":"Histories","tags":["history"]}]}"#,
    )
    .unwrap()
}

fn printed(q: &str) -> String {
    quarb::expand(q, &Defs::default()).unwrap_or_else(|e| panic!("{q}: {e}"))
}

fn answers(q: &str) -> Vec<String> {
    let run = quarb::Run::ambient();
    let out = quarb::Query::parse(q, &doc())
        .unwrap_or_else(|e| panic!("{q}: {e}"))
        .run(&doc(), &run)
        .unwrap_or_else(|e| panic!("{q}: {e}"));
    match out.result {
        QueryResult::Values(vs) => vs.iter().map(Value::display_form).collect(),
        QueryResult::Nodes(ns) => ns.iter().map(|n| format!("{n:?}")).collect(),
    }
}

#[test]
fn two_steps_up_keep_their_star_and_read_back() {
    for (written, canonical) in [
        // two parent steps, in either spelling
        (
            "//books/* | /tags/* | /../.. | :path",
            r"//books/* | /tags/* | \*\ | :path",
        ),
        (
            r"//books/* | /tags/* | \*\* | :path",
            r"//books/* | /tags/* | \*\ | :path",
        ),
        // a lone one is the bare axis, and the ancestors are theirs
        (
            "//books/* | /tags/* | /.. | :path",
            r"//books/* | /tags/* | \ | :path",
        ),
        (
            r"//books/* | /tags/* | \\ | :path",
            r"//books/* | /tags/* | \\ | :path",
        ),
        // the ancestors and then a parent, a parent and then the
        // ancestors
        (
            r"//books/*/tags/*\\\* @| count",
            r"//books/*/tags/*\\*\ @| count",
        ),
        (
            r"//books/*/tags/*\*\\ @| count",
            r"//books/*/tags/*\*\\ @| count",
        ),
        // in a group and in a predicate
        (
            r"//books/*/tags/*(\*\*) | :path",
            r"//books/*/tags/*(\*\) | :path",
        ),
        (
            r"//books/*/tags/*[\*\*::title]::",
            r"//books/*/tags/*[\*\::title]::",
        ),
    ] {
        let once = printed(written);
        assert_eq!(once, canonical, "{written}");
        // the printed text is a fixpoint, and answers what was written
        assert_eq!(printed(&once), once, "{written}");
        assert_eq!(answers(&once), answers(written), "{written}");
    }
    // the two steps reach the book; the ancestors reach more
    assert_eq!(
        answers("//books/*[::title = \"Histories\"] | /tags/* | /../.. | :path"),
        ["/books/2"]
    );
    assert_eq!(
        answers(r#"//books/*[::title = "Histories"] | /tags/* | \\ | :path"#),
        ["/books/2/tags", "/books/2", "/books", "/"]
    );
}

#[test]
fn the_thread_tree_draws_the_two_steps_as_they_read() {
    let tree = answers("//books/*[::title = \"Histories\"] | /tags/* | /../.. @| threads");
    assert_eq!(tree.len(), 1);
    assert!(tree[0].contains(r"└─ \*\"), "{}", tree[0]);
    assert!(!tree[0].contains(r"\\"), "{}", tree[0]);
}