qua 0.75.0

Structure-aware query CLI for the Quarb engine
//! A pipe after a record field's value is refused with its cure: the
//! field is an expression, and its pipe tail stands in parentheses
//! of its own. The refusal was the closing parenthesis's alone,
//! "expected ')' to close the record".

use std::path::PathBuf;

fn fixture(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("quarb-recpipe-{}-{tag}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(
        dir.join("h.json"),
        r#"{"line":"Nay, answer me. Stand and unfold yourself."}"#,
    )
    .unwrap();
    dir
}

fn qua(tag: &str, query: &str) -> (String, String, bool) {
    let dir = fixture(tag);
    let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
        .env("QUARB_LOCALE", "C")
        .current_dir(&dir)
        .args([query, "h.json"])
        .stdin(std::process::Stdio::null())
        .output()
        .unwrap();
    (
        String::from_utf8_lossy(&out.stdout).trim().to_string(),
        String::from_utf8_lossy(&out.stderr).trim().to_string(),
        out.status.success(),
    )
}

#[test]
fn a_pipe_after_a_fields_value_names_its_parentheses() {
    let cure = "a pipe after a field's value stands in the field's own parentheses: \
                '%(name = (value | command))'; the record's fields are separated by ';' and it \
                closes at ')'";
    for q in [
        // the plain pipe, and the aggregation pipe
        r#"= %(a = 1; says = (::line | words)[..3] | join(" "))"#,
        r#"= %(a = 1; says = (::line | words) @| count)"#,
        // in the first field as in a later one
        r#"= %(says = ::line | upper; a = 1)"#,
    ] {
        let (out, err, ran) = qua("refused", q);
        assert!(!ran, "{q} ran: {out}");
        assert!(err.contains(cure), "{q}: {err}");
    }
    // the field's own parentheses, as the refusal says
    let (out, err, ran) = qua(
        "cured",
        r#"^ | %(a = 1; says = ((::line | words)[..3] | join(" ")))"#,
    );
    assert!(ran, "{err}");
    assert_eq!(out, r#"%(a = 1; says = "Nay, answer me.")"#);
    // a field that is a command by its bare word keeps its tail, as
    // it did
    let (out, err, ran) = qua("word", r#"= @(3; 1; 2) | %(n = count; top = sort | last)"#);
    assert!(ran, "{err}");
    assert_eq!(out, "%(n = 3; top = 3)");
    // and a record that is not closed says so, as it did
    let (_, err, ran) = qua("open", "= %(a = 1; b = 2");
    assert!(!ran);
    assert!(err.contains("')' to close the record"), "{err}");
}