qua 0.73.1

Structure-aware query CLI for the Quarb engine
//! Core fields (spec draft.164): the single colon reads a core
//! field on a node as on a record; `:props` / `:properties` list the
//! node's property projections, `:meta` / `:metadata` its adapter
//! metadata projections, `:fields` the core field projections —
//! lists of PROJECTORS, printed as written and coercing to
//! text as the bare key; `| fields` reads the field projections of
//! any topic; an unknown core field refuses naming them; `| %` on a
//! node reads the record of its properties, `| @` is refused.

use quarb::{QueryResult, Value};

fn doc() -> quarb_json::JsonAdapter {
    quarb_json::JsonAdapter::parse(r#"{"a":{"title":"Iliad","year":-750},"b":[1,2]}"#).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 refuse(q: &str) -> String {
    match run(q) {
        Err(e) => e.to_string(),
        Ok(out) => panic!("{q} passed: {out:?}"),
    }
}

#[test]
fn the_inspection_fields_list_the_three_ladders_as_projections() {
    assert_eq!(rows("/a:props"), ["@(::title; ::year)"]);
    assert_eq!(rows("/a:properties"), ["@(::title; ::year)"]);
    // a JSON array names its items by position, from one, as `::`
    // reads them
    assert_eq!(rows("/b:props"), ["@(::1; ::2)"]);
    // the adapter's `:::` keys the node answers
    let meta = rows("/a:meta");
    assert!(meta[0].contains(":::type"), "{meta:?}");
    assert_eq!(rows("/a:metadata"), meta);
    // the core fields themselves, the same list on every node
    let fields = rows("/a:fields");
    for k in [
        ":name;", ":id;", ":index;", ":traits;", ":kind;", ":props;", ":meta;", ":fields;",
    ] {
        assert!(fields[0].contains(k), "{k} in {fields:?}");
    }
    assert_eq!(rows("/b:fields"), fields);
    // the kind: the first trait, the word that says what the node
    // is in its adapter's model (spec: Traits); never null
    assert_eq!(rows("/a:kind"), ["object"]);
    assert_eq!(rows("/b:kind"), ["array"]);
    assert_eq!(rows("/a/*:kind"), rows("/a/* | :traits | first"));
    assert_eq!(
        rows("/a/*[:kind = \"number\"] | :name"),
        rows("/a/*<number> | :name")
    );
    // the canonical spellings print short
    for (q, canon) in [("/a:properties", "/a:props"), ("/a:metadata", "/a:meta")] {
        assert_eq!(quarb::Query::parse(q, &doc()).unwrap().canonical(), canon);
    }
}

#[test]
fn a_projector_prints_as_written_and_coerces_to_its_key() {
    assert_eq!(rows("/a:props[1] | type"), ["projector"]);
    assert_eq!(rows("/a:props | join(\", \")"), ["title, year"]);
    assert_eq!(rows("/a:props[1] | ($_ = \"title\")"), ["true"]);
    assert_eq!(rows("/a:props[1] | upper"), ["TITLE"]);
    // the list pasted after the node reads what it names
    assert_eq!(rows("/a | @(::title; ::year)"), [r#"@("Iliad"; -750)"#]);
}

#[test]
fn the_fields_command_reads_any_topic() {
    assert_eq!(rows("= %(a = 1; b = 2) | fields"), ["@(:a; :b)"]);
    assert_eq!(
        rows("= %('my key' = 1; b = 2) | fields"),
        ["@(:'my key'; :b)"]
    );
    assert_eq!(
        rows("= #(%(a = 1; b = 2); %(a = 3; b = 4)) | fields"),
        ["@(:a; :b)"]
    );
    assert_eq!(rows("= #(%(1; 2)) | fields"), ["@(:1; :2)"]);
    assert_eq!(rows("/a | fields"), rows("/a:fields"));
    // over a stream of records, the union in first-seen order
    assert_eq!(
        rows("= @(%(a = 1); %(b = 2; a = 3)) | ... @| fields"),
        ["@(:a; :b)"]
    );
    // keys keeps answering text; header the typed projectors
    assert_eq!(rows("= %(a = 1; b = 2) | keys"), [r#"@("a"; "b")"#]);
    // a list of one is a list
    assert_eq!(rows("= %(a = 1) | fields"), ["@(:a)"]);
    assert_eq!(rows("= %(a = 1) | keys"), [r#"@("a")"#]);
    assert_eq!(rows("= %(a = 1) | values"), ["@(1)"]);
    assert_eq!(rows("= #(%(a = 1)) | header"), ["@(:a<*; int>)"]);
    assert_eq!(rows("= @(@(1)) | flatten"), ["@(1)"]);
    let e = refuse("= 7 | fields");
    assert!(e.contains("fields reads a record's fields"), "{e}");
}

#[test]
fn an_unknown_core_field_is_refused_naming_the_core_fields() {
    let e = refuse("/a:zzz");
    assert!(
        e.contains("':zzz' is no core field") && e.contains("props") && e.contains("'::zzz'"),
        "{e}"
    );
    let e = refuse("/* | $$_:zzz");
    assert!(e.contains("':zzz' is no core field"), "{e}");
    // on a record the single colon is a field read: a miss is null;
    // a value that is no record, null among them, answers null too
    assert_eq!(rows("= %(a = 1) | :zzz"), ["null"]);
    assert_eq!(rows("= null | :zzz"), ["null"]);
    assert_eq!(rows(r#"= "x" | :zzz"#), ["null"]);
    assert_eq!(rows("= 3 | :zzz"), ["null"]);
    assert_eq!(rows("= 3 | %(cmos = (:: | :genus))"), ["%(cmos = null)"]);
    // the node's core field reads where the topic is the node
    assert_eq!(rows("/a | :name"), ["a"]);
    // any other value topic reads the POSITION's core field, an
    // unknown key null: the thread still stands at its node
    assert_eq!(rows("/a | :name | :index"), ["1"]);
    assert_eq!(rows("/a | ::title | :depth"), ["1"]);
    assert_eq!(rows("/* | :name | :zzz"), ["null", "null"]);
    // a list topic reads the field of each item
    assert_eq!(rows("= @(%(n = 1); %(n = 2)) | :n"), ["@(1; 2)"]);
    assert_eq!(rows("= %(a = 1) | :a"), ["1"]);
    // the known ones stay
    assert_eq!(rows("/a:name"), ["a"]);
    assert_eq!(rows("/a:index"), ["1"]);
}

/// A projector after the pipe reads its items on the node the thread
/// stands at (ruling 2026-11-21): a one-colon item that is no core
/// field is refused there too, in a record and in a table, alone or
/// beside other items. It answered null in silence (`%(title =
/// null)`) where `| :title` and `%(t = :title)` refused; the
/// property is `::title`.
#[test]
fn an_unknown_core_field_is_refused_in_a_projector_over_a_node() {
    for q in [
        "/a | %(:title)",
        "/a | %(:title; t = :title)",
        "/a | %(x = 1; :title)",
        "/a | %(:name; :title)",
        "/a | #(:title)",
    ] {
        let e = refuse(q);
        assert!(
            e.contains("':title' is no core field") && e.contains("a property is '::title'"),
            "{q}: {e}"
        );
    }
    let e = refuse("/* | %(:zzz; ::title)");
    assert!(e.contains("':zzz' is no core field"), "{e}");
    // the core fields and the properties read as they did
    assert_eq!(
        rows("/a | %(:name; ::title)"),
        [r#"%(name = "a"; title = "Iliad")"#]
    );
    assert_eq!(
        rows("/* | %(:name; :index)"),
        [r#"%(name = "a"; index = 1)"#, r#"%(name = "b"; index = 2)"#]
    );
    assert_eq!(
        rows("/a | %(:name) ++ %(n = 2)"),
        [r#"%(name = "a"; n = 2)"#]
    );
    // a value topic is no node: the position's core field, an
    // unknown key null, as the plain read answers there
    assert_eq!(rows("/a::title | %(:name)"), [r#"%(name = "a")"#]);
    assert_eq!(rows("/a::title | %(:zzz)"), ["%(zzz = null)"]);
    // a record topic: its field, a miss null
    assert_eq!(
        rows("/a | %(::title; ::year) | %(:title)"),
        [r#"%(title = "Iliad")"#]
    );
    assert_eq!(rows("/a | %(::title; ::year) | %(:zzz)"), ["%(zzz = null)"]);
}

#[test]
fn the_record_reading_of_a_node_and_no_row_reading() {
    assert_eq!(rows("/a | %"), [r#"%(title = "Iliad"; year = -750)"#]);
    assert_eq!(rows("/a | % | :title"), ["Iliad"]);
    let e = refuse("/a | @");
    assert!(
        e.contains("a node has no row reading") && e.contains("'| %'"),
        "{e}"
    );
    // the record then reads as a record: its fields counted
    assert_eq!(rows("/a | % | count"), ["2"]);
}