mcd-core 0.1.0-alpha.2

Core parser, validator, and exporter for Markdown CSV Document packages.
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Table and chart view parsing and validation.

use serde::{Deserialize, Serialize};

use crate::{
    directives::TableDisplay,
    errors::{Diagnostic, McdError, Result},
    package::McdPackage,
    schema::{ColumnType, TableSchema},
};

/// Parsed table view JSON.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TableView {
    /// Stable view id.
    pub id: String,
    /// Referenced table id.
    pub table: String,
    /// View display type.
    #[serde(default)]
    pub display: TableDisplay,
    /// Columns included in a table display.
    #[serde(default)]
    pub columns: Vec<ViewColumn>,
    /// Chart specification for chart displays.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub chart: Option<ChartSpec>,
    /// Optional style metadata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub style: Option<serde_json::Value>,
}

impl TableView {
    /// Parse a table view from a package entry.
    pub fn from_package(package: &McdPackage, path: &str) -> Result<Self> {
        let bytes = package.read(path).map_err(|_| {
            McdError::from_diagnostic(
                Diagnostic::error(
                    "view.file.missing",
                    format!("Declared table view file '{path}' is missing."),
                )
                .with_source(path.to_owned()),
            )
        })?;
        serde_json::from_slice::<Self>(bytes).map_err(McdError::from)
    }

    /// Validate this view against its table schema.
    pub fn validate(
        &self,
        expected_id: &str,
        table_id: &str,
        schema: &TableSchema,
        source: &str,
    ) -> Result<()> {
        if self.id != expected_id {
            return Err(view_error(
                "view.id.mismatch",
                format!(
                    "View id '{}' does not match manifest view id '{}'.",
                    self.id, expected_id
                ),
                source,
            ));
        }
        if self.table != table_id {
            return Err(view_error(
                "view.table.mismatch",
                format!(
                    "View '{}' references table '{}', but manifest attaches it to '{}'.",
                    self.id, self.table, table_id
                ),
                source,
            ));
        }

        match self.display {
            TableDisplay::Table => self.validate_table_columns(schema, source),
            TableDisplay::Chart => self.validate_chart(schema, source),
        }
    }

    fn validate_table_columns(&self, schema: &TableSchema, source: &str) -> Result<()> {
        for column in &self.columns {
            if !schema.has_column(&column.name) {
                return Err(view_error(
                    "view.column.unknown",
                    format!(
                        "View '{}' references unknown schema column '{}'.",
                        self.id, column.name
                    ),
                    source,
                ));
            }
            validate_format_declarations(
                column.format.as_deref(),
                column.currency.as_deref(),
                column.unit_label.as_deref(),
                column.percent,
                schema
                    .column(&column.name)
                    .map(|schema_column| schema_column.value_type),
                source,
            )?;
        }
        Ok(())
    }

    fn validate_chart(&self, schema: &TableSchema, source: &str) -> Result<()> {
        let Some(chart) = &self.chart else {
            return Err(view_error(
                "chart.spec.missing",
                format!("Chart view '{}' must include a chart object.", self.id),
                source,
            ));
        };
        chart.validate(schema, source)?;
        Ok(())
    }
}

/// One table view column.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ViewColumn {
    /// Referenced schema column name.
    pub name: String,
    /// Optional display label.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// Optional format declaration.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
    /// Currency code for currency-formatted numeric values.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub currency: Option<String>,
    /// Free-form display unit label for numeric values.
    #[serde(
        default,
        rename = "unitLabel",
        alias = "unit",
        skip_serializing_if = "Option::is_none"
    )]
    pub unit_label: Option<String>,
    /// Whether the value is a percentage.
    #[serde(default)]
    pub percent: bool,
}

/// Constrained chart specification.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ChartSpec {
    /// Chart type in the alpha subset.
    #[serde(rename = "type")]
    pub chart_type: ChartType,
    /// X-axis encoding.
    pub x: ChartEncoding,
    /// Y-axis encoding.
    pub y: ChartEncoding,
    /// Optional series encoding.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub series: Option<ChartEncoding>,
    /// Optional grouping encoding.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub grouping: Option<ChartEncoding>,
    /// Optional mark label declaration.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mark_labels: Option<MarkLabels>,
}

impl ChartSpec {
    fn validate(&self, schema: &TableSchema, source: &str) -> Result<()> {
        self.x.validate_known_column(schema, source)?;
        self.y.validate_known_column(schema, source)?;
        if let Some(series) = &self.series {
            series.validate_known_column(schema, source)?;
        }
        if let Some(grouping) = &self.grouping {
            grouping.validate_known_column(schema, source)?;
        }
        if let Some(mark_labels) = &self.mark_labels {
            mark_labels.validate(schema, source)?;
        }

        let y_type = column_type(schema, &self.y.column, source)?;
        if !y_type.is_numeric() {
            return Err(view_error(
                "chart.column.type.incompatible",
                format!(
                    "Chart y column '{}' must be integer or decimal.",
                    self.y.column
                ),
                source,
            ));
        }
        self.y.validate_format(schema, source)?;
        self.x.validate_format(schema, source)?;

        if self.chart_type == ChartType::Scatter {
            let x_type = column_type(schema, &self.x.column, source)?;
            if !x_type.is_numeric() && !x_type.is_temporal() {
                return Err(view_error(
                    "chart.column.type.incompatible",
                    format!(
                        "Scatter chart x column '{}' must be numeric or temporal.",
                        self.x.column
                    ),
                    source,
                ));
            }
        }

        Ok(())
    }
}

/// Supported alpha chart types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChartType {
    /// Bar chart.
    Bar,
    /// Line chart.
    Line,
    /// Area chart.
    Area,
    /// Scatter chart.
    Scatter,
}

/// A chart encoding.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ChartEncoding {
    /// Referenced schema column.
    pub column: String,
    /// Optional display label.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// Optional value format.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
    /// Currency code for currency-formatted numeric values.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub currency: Option<String>,
    /// Free-form display unit label.
    #[serde(
        default,
        rename = "unitLabel",
        alias = "unit",
        skip_serializing_if = "Option::is_none"
    )]
    pub unit_label: Option<String>,
    /// Whether the value is a percentage.
    #[serde(default)]
    pub percent: bool,
}

impl ChartEncoding {
    fn validate_known_column(&self, schema: &TableSchema, source: &str) -> Result<()> {
        if schema.has_column(&self.column) {
            Ok(())
        } else {
            Err(view_error(
                "chart.column.unknown",
                format!("Chart references unknown schema column '{}'.", self.column),
                source,
            ))
        }
    }

    fn validate_format(&self, schema: &TableSchema, source: &str) -> Result<()> {
        validate_format_declarations(
            self.format.as_deref(),
            self.currency.as_deref(),
            self.unit_label.as_deref(),
            self.percent,
            schema.column(&self.column).map(|column| column.value_type),
            source,
        )
    }
}

/// Mark label display and formatting declaration.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarkLabels {
    /// Whether labels are shown.
    #[serde(default)]
    pub show: bool,
    /// Optional label source column. Defaults to the chart y column when absent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub column: Option<String>,
    /// Optional value format.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
    /// Currency code for currency-formatted numeric values.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub currency: Option<String>,
    /// Free-form display unit label.
    #[serde(
        default,
        rename = "unitLabel",
        alias = "unit",
        skip_serializing_if = "Option::is_none"
    )]
    pub unit_label: Option<String>,
    /// Whether the value is a percentage.
    #[serde(default)]
    pub percent: bool,
}

impl MarkLabels {
    fn validate(&self, schema: &TableSchema, source: &str) -> Result<()> {
        if let Some(column) = &self.column {
            if !schema.has_column(column) {
                return Err(view_error(
                    "chart.column.unknown",
                    format!("Chart mark labels reference unknown schema column '{column}'."),
                    source,
                ));
            }
            validate_format_declarations(
                self.format.as_deref(),
                self.currency.as_deref(),
                self.unit_label.as_deref(),
                self.percent,
                schema
                    .column(column)
                    .map(|schema_column| schema_column.value_type),
                source,
            )?;
        }
        Ok(())
    }
}

fn validate_format_declarations(
    format: Option<&str>,
    currency: Option<&str>,
    _unit: Option<&str>,
    percent: bool,
    column_type: Option<ColumnType>,
    source: &str,
) -> Result<()> {
    if currency.is_some() && format != Some("currency") {
        return Err(view_error(
            "view.format.currency.inconsistent",
            "Currency declarations require format: currency.",
            source,
        ));
    }
    if percent && format.is_some_and(|format| format != "percent") {
        return Err(view_error(
            "view.format.percent.inconsistent",
            "Percent declarations must use format: percent when a format is declared.",
            source,
        ));
    }

    if matches!(format, Some("currency" | "number" | "percent")) {
        let Some(column_type) = column_type else {
            return Ok(());
        };
        if !column_type.is_numeric() {
            return Err(view_error(
                "view.format.type.incompatible",
                "Numeric, currency, and percent formats require integer or decimal columns.",
                source,
            ));
        }
    }

    if matches!(format, Some("date" | "datetime" | "time")) {
        let Some(column_type) = column_type else {
            return Ok(());
        };
        if !column_type.is_temporal() {
            return Err(view_error(
                "view.format.type.incompatible",
                "Date, datetime, and time formats require temporal columns.",
                source,
            ));
        }
    }

    Ok(())
}

fn column_type(schema: &TableSchema, column: &str, source: &str) -> Result<ColumnType> {
    schema
        .column(column)
        .map(|column| column.value_type)
        .ok_or_else(|| {
            view_error(
                "chart.column.unknown",
                format!("Chart references unknown schema column '{column}'."),
                source,
            )
        })
}

fn view_error(code: impl Into<String>, message: impl Into<String>, source: &str) -> McdError {
    McdError::from_diagnostic(Diagnostic::error(code, message).with_source(source.to_owned()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schema::{ColumnType, TableColumnSchema};

    fn schema() -> TableSchema {
        TableSchema {
            id: "revenue".to_owned(),
            primary_key: Vec::new(),
            foreign_keys: Vec::new(),
            columns: vec![
                TableColumnSchema {
                    name: "quarter".to_owned(),
                    value_type: ColumnType::String,
                    label: None,
                    unit: None,
                    nullable: false,
                    enum_values: Vec::new(),
                },
                TableColumnSchema {
                    name: "amount".to_owned(),
                    value_type: ColumnType::Decimal,
                    label: None,
                    unit: None,
                    nullable: false,
                    enum_values: Vec::new(),
                },
            ],
        }
    }

    #[test]
    fn validates_chart_columns() {
        let view = serde_json::from_str::<TableView>(
            r#"{
                "id":"chart",
                "table":"revenue",
                "display":"chart",
                "chart":{
                    "type":"bar",
                    "x":{"column":"quarter"},
                    "y":{"column":"amount","format":"currency","currency":"GBP"}
                }
            }"#,
        )
        .expect("view parses");

        view.validate("chart", "revenue", &schema(), "tables/chart.view.json")
            .expect("valid chart view");
    }

    #[test]
    fn accepts_unit_label_and_legacy_unit_display_fields() {
        let unit_label = serde_json::from_str::<TableView>(
            r#"{
                "id":"default",
                "table":"revenue",
                "columns":[{"name":"amount","unitLabel":"kg"}]
            }"#,
        )
        .expect("view parses");
        assert_eq!(unit_label.columns[0].unit_label.as_deref(), Some("kg"));

        let legacy_unit = serde_json::from_str::<TableView>(
            r#"{
                "id":"default",
                "table":"revenue",
                "columns":[{"name":"amount","unit":"kg"}]
            }"#,
        )
        .expect("view parses");
        assert_eq!(legacy_unit.columns[0].unit_label.as_deref(), Some("kg"));
    }

    #[test]
    fn rejects_unknown_view_column() {
        let view = serde_json::from_str::<TableView>(
            r#"{"id":"default","table":"revenue","columns":[{"name":"missing"}]}"#,
        )
        .expect("view parses");
        let err = view
            .validate("default", "revenue", &schema(), "tables/view.json")
            .expect_err("invalid");

        assert_eq!(
            err.diagnostic().map(|d| d.code.as_str()),
            Some("view.column.unknown")
        );
    }
}