benday-core 0.2.1

Spec-to-chart rendering core for benday: terminal charts from a Vega-Lite-style JSON spec
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! Data ingestion: resolve the spec's inline data and/or a piped data
//! document into a normalized `Table`. Pure — the CLI does the I/O.
//!
//! Strictness boundary: the SPEC is agent-authored intent, so its data object
//! is strict (`deny_unknown_fields`, over in `spec.rs`). The stdin document is
//! producer-shaped payload (e.g. an MCP `structuredContent` envelope), so it
//! is tolerant: known fields are used, unknown fields (query provenance etc.)
//! are ignored.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

use crate::error::Error;
use crate::spec::{Column, Data, FieldType, Spec};

pub type Row = Map<String, Value>;

/// A data document piped to stdin: a columnar envelope or a bare row array.
/// Tolerant by design — no `deny_unknown_fields`.
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum DataDoc {
    Envelope {
        columns: Vec<EnvColumn>,
        rows: Vec<Vec<Value>>,
        #[serde(default)]
        truncated: Option<bool>,
        #[serde(default)]
        total_rows: Option<u64>,
    },
    Rows(Vec<Row>),
}

/// Envelope column: tolerant twin of `spec::Column` (producers may add keys).
#[derive(Debug, Deserialize)]
pub struct EnvColumn {
    pub name: String,
    #[serde(default, rename = "type")]
    pub ty: Option<String>,
}

impl From<&Column> for EnvColumn {
    fn from(c: &Column) -> Self {
        EnvColumn {
            name: c.name.clone(),
            ty: c.ty.clone(),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum DataSource {
    InlineValues,
    InlineColumns,
    StdinValues,
    StdinColumns,
}

#[derive(Debug, Serialize)]
pub struct DataProvenance {
    pub source: DataSource,
    pub truncated: Option<bool>,
    pub total_rows: Option<u64>,
}

/// Normalized data ready for the compiler: row-major rows (as the compiler
/// has always consumed), declared column types, and where it all came from.
#[derive(Debug)]
pub struct Table {
    pub rows: Vec<Row>,
    pub declared: HashMap<String, FieldType>,
    pub provenance: DataProvenance,
}

/// Parse a stdin data document. Wraps serde's unhelpful untagged-enum error
/// with the two accepted shapes.
pub fn parse_data_doc(s: &str) -> Result<DataDoc, Error> {
    serde_json::from_str(s).map_err(|e| {
        Error::Data(format!(
            "cannot parse stdin as a data document; expected \
             {{\"columns\":[{{\"name\",\"type\"?}}...],\"rows\":[[...]...]}} or a JSON array \
             of row objects: {e}"
        ))
    })
}

/// Map a declared column type (BigQuery + common SQL spellings + the
/// JSON-Schema-ish envelope vocabulary of mcp-dataconnector,
/// case-insensitive) to a field type. Unknown names fall back to nominal —
/// NOT an error: producers grow types, and nominal is
/// safe-wrong-in-the-obvious-way. But the fallback OVERRIDES inference
/// (declared beats inference), so a missing vocabulary entry makes a
/// declaring producer chart WORSE than a silent one — dogfooding hit exactly
/// this with "number" (2026-07-05); when a real producer's type string lands
/// in the fallback, add it here. BOOLEAN/BOOL are recognized-nominal
/// (a deliberate two-category axis), listed so they read as vocabulary, not
/// fallback drift. DATE/DATETIME/TIMESTAMP/TIME map to temporal: benday owns
/// time layout (true positions on a calendar scale) when SQL is absent — see
/// docs/plans/2026-07-05-temporal-family-design.md for why this reverses the
/// old "no temporal scale" doctrine. An explicit `"ordinal"` on the encoding
/// restores the evenly-spaced behavior per chart.
pub fn declared_field_type(t: &str) -> FieldType {
    match t.to_ascii_uppercase().as_str() {
        "INT64" | "INTEGER" | "INT" | "SMALLINT" | "BIGINT" | "FLOAT64" | "FLOAT" | "DOUBLE"
        | "NUMERIC" | "BIGNUMERIC" | "DECIMAL" | "REAL" | "NUMBER" => FieldType::Quantitative,
        "DATE" | "DATETIME" | "TIMESTAMP" | "TIME" => FieldType::Temporal,
        "BOOLEAN" | "BOOL" => FieldType::Nominal,
        _ => FieldType::Nominal,
    }
}

/// Resolve spec + optional stdin document into a Table. Owns ALL precedence
/// and data-shape errors so the corpus can pin them.
pub fn resolve(spec: &Spec, stdin: Option<DataDoc>) -> Result<Table, Error> {
    match (&spec.data, stdin) {
        (Some(_), Some(_)) => Err(Error::Spec(
            "data provided twice: the spec has inline `data` and a data document \
             arrived on stdin; remove one"
                .into(),
        )),
        (Some(data), None) => resolve_inline(data),
        (None, Some(doc)) => resolve_stdin(doc),
        (None, None) => Err(Error::Spec(
            "no data: the spec has no `data` and nothing arrived on stdin; add \
             data.values or data.columns+rows to the spec, or pipe a data document"
                .into(),
        )),
    }
}

/// Resolve the spec's inline `data` object. Exactly one form is allowed:
/// `values` (tidy row objects) or `columns` + `rows` (columnar). Any other
/// combination is a spec error — serde can't express either/or without
/// mangling the error paths, so it's checked here.
fn resolve_inline(data: &Data) -> Result<Table, Error> {
    match (&data.values, &data.columns, &data.rows) {
        // Inline `values`: row-major already, no declared types.
        (Some(values), None, None) => finish(
            values.clone(),
            HashMap::new(),
            DataSource::InlineValues,
            None,
            None,
        ),
        (None, Some(columns), Some(rows)) => {
            // Reuse the envelope's zip/validation. The spec's `Column` is the
            // strict twin of `EnvColumn`; same fields, so convert and share.
            let env: Vec<EnvColumn> = columns.iter().map(EnvColumn::from).collect();
            let (rows, declared) = columnar_to_rows(&env, rows)?;
            finish(rows, declared, DataSource::InlineColumns, None, None)
        }
        _ => Err(Error::Spec(
            "data must contain either `values`, or `columns` and `rows`".into(),
        )),
    }
}

/// Resolve a stdin data document (envelope or bare rows) into a Table.
fn resolve_stdin(doc: DataDoc) -> Result<Table, Error> {
    match doc {
        DataDoc::Envelope {
            columns,
            rows,
            truncated,
            total_rows,
        } => {
            let (rows, declared) = columnar_to_rows(&columns, &rows)?;
            finish(
                rows,
                declared,
                DataSource::StdinColumns,
                truncated,
                total_rows,
            )
        }
        DataDoc::Rows(rows) => finish(rows, HashMap::new(), DataSource::StdinValues, None, None),
    }
}

/// Zip a columnar envelope into row-major objects, keyed in column order, and
/// collect declared column types. Shared shape for inline and stdin columnar.
fn columnar_to_rows(
    columns: &[EnvColumn],
    rows: &[Vec<Value>],
) -> Result<(Vec<Row>, HashMap<String, FieldType>), Error> {
    let mut declared = HashMap::new();
    let mut seen = std::collections::HashSet::new();
    for col in columns {
        if !seen.insert(col.name.as_str()) {
            return Err(Error::Data(format!(
                "duplicate column name \"{}\"",
                col.name
            )));
        }
        if let Some(ty) = &col.ty {
            declared.insert(col.name.clone(), declared_field_type(ty));
        }
    }

    let want = columns.len();
    let mut out = Vec::with_capacity(rows.len());
    for (i, row) in rows.iter().enumerate() {
        if row.len() != want {
            return Err(Error::Data(format!(
                "row {i} has {} values but {want} columns are declared",
                row.len()
            )));
        }
        let mut obj = Row::new();
        for (col, val) in columns.iter().zip(row) {
            obj.insert(col.name.clone(), val.clone());
        }
        out.push(obj);
    }
    Ok((out, declared))
}

/// Apply the empty-rows rule (any form) and assemble the Table.
fn finish(
    rows: Vec<Row>,
    declared: HashMap<String, FieldType>,
    source: DataSource,
    truncated: Option<bool>,
    total_rows: Option<u64>,
) -> Result<Table, Error> {
    if rows.is_empty() {
        return Err(Error::Data(
            "data has no rows; provide at least one row".into(),
        ));
    }
    Ok(Table {
        rows,
        declared,
        provenance: DataProvenance {
            source,
            truncated,
            total_rows,
        },
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn spec_with_values() -> Spec {
        serde_json::from_str(
            r#"{"data":{"values":[{"x":1}]},"mark":"bar",
               "encoding":{"x":{"field":"x"},"y":{"field":"y"}}}"#,
        )
        .expect("fixture spec parses")
    }

    fn spec_with_empty_values() -> Spec {
        serde_json::from_str(
            r#"{"data":{"values":[]},"mark":"bar",
               "encoding":{"x":{"field":"x"},"y":{"field":"y"}}}"#,
        )
        .expect("fixture spec parses")
    }

    #[test]
    fn parses_bare_row_array() {
        let doc = parse_data_doc(r#"[{"a": 1}, {"a": 2}]"#).expect("bare array parses");
        match doc {
            DataDoc::Rows(rows) => assert_eq!(rows.len(), 2),
            other => panic!("expected Rows, got {other:?}"),
        }
    }

    #[test]
    fn envelope_tolerates_unknown_keys() {
        // The producer emits a `query` provenance block benday ignores.
        let doc = parse_data_doc(
            r#"{"columns":[{"name":"day","type":"STRING"},{"name":"n","type":"INT64"}],
                "rows":[["mon",32]],
                "query":{"job_id":"abc","note":"ignored"}}"#,
        )
        .expect("envelope with extra keys parses");
        match doc {
            DataDoc::Envelope { columns, rows, .. } => {
                assert_eq!(columns.len(), 2);
                assert_eq!(rows.len(), 1);
            }
            other => panic!("expected Envelope, got {other:?}"),
        }
    }

    #[test]
    fn envelope_truncated_and_total_rows_reach_provenance() {
        let doc = parse_data_doc(
            r#"{"columns":[{"name":"n"}],"rows":[[1]],"truncated":true,"total_rows":123}"#,
        )
        .expect("envelope parses");
        let table = resolve_stdin(doc).expect("resolves");
        assert_eq!(table.provenance.source, DataSource::StdinColumns);
        assert_eq!(table.provenance.truncated, Some(true));
        assert_eq!(table.provenance.total_rows, Some(123));
    }

    #[test]
    fn bare_rows_provenance_has_no_envelope_fields() {
        let doc = parse_data_doc(r#"[{"a":1}]"#).expect("parses");
        let table = resolve_stdin(doc).expect("resolves");
        assert_eq!(table.provenance.source, DataSource::StdinValues);
        assert_eq!(table.provenance.truncated, None);
        assert_eq!(table.provenance.total_rows, None);
        assert!(table.declared.is_empty());
    }

    #[test]
    fn columnar_zips_rows_in_column_order() {
        let doc = parse_data_doc(
            r#"{"columns":[{"name":"day","type":"STRING"},{"name":"n","type":"INT64"}],
                "rows":[["mon",32],["tue",78]]}"#,
        )
        .expect("parses");
        let table = resolve_stdin(doc).expect("resolves");
        assert_eq!(table.rows.len(), 2);
        assert_eq!(table.rows[0].get("day"), Some(&json!("mon")));
        assert_eq!(table.rows[0].get("n"), Some(&json!(32)));
        assert_eq!(table.rows[1].get("day"), Some(&json!("tue")));
        assert_eq!(table.rows[1].get("n"), Some(&json!(78)));
        // keys land in declared column order
        let keys: Vec<&String> = table.rows[0].keys().collect();
        assert_eq!(keys, vec!["day", "n"]);
        assert_eq!(table.declared.get("day"), Some(&FieldType::Nominal));
        assert_eq!(table.declared.get("n"), Some(&FieldType::Quantitative));
    }

    #[test]
    fn duplicate_column_name_errors() {
        let doc = parse_data_doc(r#"{"columns":[{"name":"a"},{"name":"a"}],"rows":[[1,2]]}"#)
            .expect("parses");
        let err = resolve_stdin(doc).expect_err("duplicate must error");
        insta::assert_snapshot!(err.to_string(), @r###"duplicate column name "a""###);
    }

    #[test]
    fn row_length_mismatch_errors() {
        let doc =
            parse_data_doc(r#"{"columns":[{"name":"a"},{"name":"b"}],"rows":[[1,2],[1,2,3]]}"#)
                .expect("parses");
        let err = resolve_stdin(doc).expect_err("length mismatch must error");
        insta::assert_snapshot!(
            err.to_string(),
            @"row 1 has 3 values but 2 columns are declared"
        );
    }

    #[test]
    fn empty_rows_errors_columnar() {
        let doc = parse_data_doc(r#"{"columns":[{"name":"a"}],"rows":[]}"#).expect("parses");
        let err = resolve_stdin(doc).expect_err("empty rows must error");
        insta::assert_snapshot!(err.to_string(), @"data has no rows; provide at least one row");
    }

    #[test]
    fn empty_rows_errors_bare_array() {
        let doc = parse_data_doc(r#"[]"#).expect("parses");
        let err = resolve_stdin(doc).expect_err("empty rows must error");
        insta::assert_snapshot!(err.to_string(), @"data has no rows; provide at least one row");
    }

    #[test]
    fn empty_rows_errors_inline_values() {
        let err = resolve(&spec_with_empty_values(), None).expect_err("empty inline must error");
        insta::assert_snapshot!(err.to_string(), @"data has no rows; provide at least one row");
    }

    #[test]
    fn inline_values_resolve_to_inline_provenance() {
        let table = resolve(&spec_with_values(), None).expect("resolves");
        assert_eq!(table.provenance.source, DataSource::InlineValues);
        assert_eq!(table.provenance.truncated, None);
        assert_eq!(table.provenance.total_rows, None);
        assert!(table.declared.is_empty());
        assert_eq!(table.rows.len(), 1);
    }

    #[test]
    fn data_provided_twice_errors() {
        let doc = parse_data_doc(r#"[{"a":1}]"#).expect("parses");
        let err = resolve(&spec_with_values(), Some(doc)).expect_err("data twice must error");
        insta::assert_snapshot!(
            err.to_string(),
            @"data provided twice: the spec has inline `data` and a data document arrived on stdin; remove one"
        );
    }

    #[test]
    fn inline_columnar_resolves_with_declared_types() {
        let spec: Spec = serde_json::from_str(
            r#"{"data":{"columns":[{"name":"day","type":"STRING"},{"name":"n","type":"INT64"}],
                       "rows":[["mon",32],["tue",78]]},
                "mark":"bar","encoding":{"x":{"field":"day"},"y":{"field":"n"}}}"#,
        )
        .expect("inline columnar spec parses");
        let table = resolve(&spec, None).expect("resolves");
        assert_eq!(table.provenance.source, DataSource::InlineColumns);
        assert_eq!(table.rows.len(), 2);
        assert_eq!(table.rows[0].get("day"), Some(&json!("mon")));
        assert_eq!(table.rows[0].get("n"), Some(&json!(32)));
        assert_eq!(table.declared.get("day"), Some(&FieldType::Nominal));
        assert_eq!(table.declared.get("n"), Some(&FieldType::Quantitative));
    }

    #[test]
    fn inline_data_both_forms_errors() {
        let spec: Spec = serde_json::from_str(
            r#"{"data":{"values":[{"a":1}],"columns":[{"name":"a"}],"rows":[[1]]},
                "mark":"bar","encoding":{"x":{"field":"a"},"y":{"field":"a"}}}"#,
        )
        .expect("spec parses");
        let err = resolve(&spec, None).expect_err("both forms must error");
        insta::assert_snapshot!(
            err.to_string(),
            @"data must contain either `values`, or `columns` and `rows`"
        );
    }

    #[test]
    fn no_data_anywhere_errors() {
        let spec: Spec = serde_json::from_str(
            r#"{"mark":"bar","encoding":{"x":{"field":"a"},"y":{"field":"b"}}}"#,
        )
        .expect("spec without data parses");
        let err = resolve(&spec, None).expect_err("no data must error");
        insta::assert_snapshot!(
            err.to_string(),
            @"no data: the spec has no `data` and nothing arrived on stdin; add data.values or data.columns+rows to the spec, or pipe a data document"
        );
    }

    #[test]
    fn declared_field_type_mapping() {
        // Quantitative spellings.
        for t in [
            "INT64",
            "INTEGER",
            "INT",
            "SMALLINT",
            "BIGINT",
            "FLOAT64",
            "FLOAT",
            "DOUBLE",
            "NUMERIC",
            "BIGNUMERIC",
            "DECIMAL",
            "REAL",
            // JSON-Schema-ish vocabulary (mcp-dataconnector envelope):
            // "number" declares Decimal/Float aggregates like SUM(volume).
            "NUMBER",
        ] {
            assert_eq!(declared_field_type(t), FieldType::Quantitative, "{t}");
        }
        // Date/time spellings map to temporal.
        for t in ["DATE", "DATETIME", "TIMESTAMP", "TIME"] {
            assert_eq!(declared_field_type(t), FieldType::Temporal, "{t}");
        }
        // Strings, booleans, and unknowns land on nominal — booleans are
        // RECOGNIZED (deliberate two-category axis), not fallback drift.
        assert_eq!(declared_field_type("STRING"), FieldType::Nominal);
        assert_eq!(declared_field_type("BOOL"), FieldType::Nominal);
        assert_eq!(declared_field_type("BOOLEAN"), FieldType::Nominal);
        assert_eq!(declared_field_type("boolean"), FieldType::Nominal);
        assert_eq!(declared_field_type("number"), FieldType::Quantitative);
        assert_eq!(declared_field_type("whatever"), FieldType::Nominal);
        // Case-insensitive.
        assert_eq!(declared_field_type("int64"), FieldType::Quantitative);
        assert_eq!(declared_field_type("Date"), FieldType::Temporal);
        assert_eq!(declared_field_type("String"), FieldType::Nominal);
    }
}