qua 0.75.0

Structure-aware query CLI for the Quarb engine
//! How `unique`, `frequencies` and `ttr` tell values apart (spec: the
//! keyed commands; the user's rulings of 2026-11-29, relayed by lang:
//! "compare as they are", and of the option "OK, drop --text and -t
//! on unique and frequencies"). A text is a text to them, whatever it
//! writes: `"2"`, `"2."` and `"02"` are three values, and the number
//! 2 a fourth, though `=` holds between it and each. The texts a
//! command meets are read as a kind under `--as=KIND`: `number`, and
//! the five are one; `text`, and every value is told by its text, the
//! integer 2 and the text "2" one. (They were told by the value
//! comparison, under which a text that writes a number was that
//! number, and counting the distinct words of a text came out short
//! of `sort -u`; `--text` was the way round it for a day.)

use quarb::{QueryResult, Value};

fn doc() -> quarb_json::JsonAdapter {
    quarb_json::JsonAdapter::parse(
        r#"{"books":[{"k":"a","title":"Emma"},
                     {"k":"a","title":"Ivanhoe"},
                     {"k":"b","title":"Persuasion"}]}"#,
    )
    .unwrap()
}

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

fn rows(q: &str) -> Vec<String> {
    run(q).unwrap_or_else(|e| panic!("{q}: {e}"))
}

fn one(q: &str) -> String {
    let out = rows(q);
    assert_eq!(out.len(), 1, "{q}: {out:?}");
    out.into_iter().next().unwrap()
}

const FIVE: &str = r#"= @(2; 2.0; "2"; "2."; "02")"#;

#[test]
fn the_three_commands_take_a_text_as_a_text() {
    // the two numbers are one, each text its own; as numbers the
    // five are one; by their texts the integer and "2" are one
    assert_eq!(one(&format!("{FIVE} | unique")), r#"@(2; "2"; "2."; "02")"#);
    assert_eq!(one(&format!("{FIVE} | unique(--as=number)")), "@(2)");
    assert_eq!(
        one(&format!("{FIVE} | unique(--as=text)")),
        r#"@(2; 2.0; "2."; "02")"#
    );
    assert_eq!(
        one(&format!("{FIVE} | frequencies")),
        r#"@%(@(value; n); %(2; 2); %("02"; 1); %("2"; 1); %("2."; 1))"#
    );
    assert_eq!(
        one(&format!("{FIVE} | frequencies(--as=number)")),
        "@(%(value = 2; n = 5))"
    );
    assert_eq!(
        one(&format!("{FIVE} | frequencies(--as=text)")),
        r#"@%(@(value; n); %(2; 2); %("02"; 1); %("2."; 1); %(2.0; 1))"#
    );
    assert_eq!(one(&format!("{FIVE} | ttr")), "0.8");
    assert_eq!(one(&format!("{FIVE} | ttr(--as=number)")), "0.2");
    assert_eq!(one(&format!("{FIVE} | ttr(--as=text)")), "0.8");
    // the words a text holds: a sign and a trailing point are kept
    assert_eq!(
        one(r#"= @("+1"; "1."; "1"; 1) | unique"#),
        r#"@("+1"; "1."; "1"; 1)"#
    );
    assert_eq!(
        one(r#"= @("+1"; "1."; "1"; 1) | unique(--as=number)"#),
        r#"@("+1")"#
    );
    // two quantities of one magnitude are one value; the text that
    // writes it is a text, and one with them under `--as=auto`
    assert_eq!(
        one(r#"= @(5_km; "5 km"; 5000_m) | unique"#),
        r#"@(5_km; "5 km")"#
    );
    assert_eq!(
        one(r#"= @(5_km; "5 km"; 5000_m) | unique(--as=auto)"#),
        "@(5_km)"
    );
    assert_eq!(
        one(r#"= @(5_km; "5 km"; 5000_m) | unique(--as=text)"#),
        r#"@(5_km; "5 km"; 5000_m)"#
    );
    // a null is no text and stays apart from the empty one
    assert_eq!(one(r#"= @(null; ""; null) | unique"#), r#"@(null; "")"#);
}

#[test]
fn a_structure_is_told_by_its_output_form_and_a_table_cell_by_cell() {
    // a text inside a structure is a text there too
    assert_eq!(
        one(r#"= @(@(1; 2); @(1; "2"); @(1; 2)) | unique"#),
        r#"@(@(1; 2); @(1; "2"))"#
    );
    assert_eq!(
        one(r#"= @(%(a = 2); %(a = "2"); %(a = 2)) | unique"#),
        r#"@%(@(a); %(2); %("2"))"#
    );
    // a table's rows: one row by value, two by the cells' texts
    let t = "= #(@(a; b); %(1; 5_km); %(1; 5000_m); %(1; 5_km))";
    assert_eq!(one(&format!("{t} | unique")), "#(@(a; b);\n  %(1; 5_km))");
    assert_eq!(
        one(&format!("{t} | unique(--as=text)")),
        "#(@(a; b);\n  %(1; 5_km);\n  %(1; 5000_m))"
    );
}

#[test]
fn over_threads_a_text_is_a_text_and_a_node_what_it_is() {
    assert_eq!(
        rows(&format!("{FIVE} | ... @| unique")),
        ["2", "2", "2.", "02"]
    );
    assert_eq!(rows(&format!("{FIVE} | ... @| unique(--as=number)")), ["2"]);
    assert_eq!(
        rows(&format!("{FIVE} | ... @| unique(--as=text)")),
        ["2", "2.0", "2.", "02"]
    );
    assert_eq!(rows("/books/*::k @| unique"), ["a", "b"]);
    // three nodes are three, whatever they read as
    assert_eq!(one("/books/* @| unique @| count"), "3");
    assert_eq!(one("/books/* @| unique(--as=text) @| count"), "3");
}

#[test]
fn the_retired_option_is_no_option() {
    for q in [
        "= @(1; 2) | unique(--text)",
        "= @(1; 2) | frequencies(-t)",
        "= @(1; 2) | ttr(--text)",
    ] {
        let e = run(q).unwrap_err().to_string();
        assert!(e.contains("has no option"), "{q}: {e}");
    }
}