mcd-core 0.1.0-alpha.1

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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Manifest parsing and basic validation.

use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

use crate::{
    errors::{Diagnostic, McdError, Result},
    package::validate_internal_path,
};

/// Root `manifest.json` model.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Manifest {
    /// Format name. Must be `MCD`.
    pub format: String,
    /// Format version. Alpha currently supports `0.1`.
    pub version: String,
    /// Conformance profile.
    pub profile: McdProfile,
    /// Optional conformance claims.
    #[serde(default)]
    pub conformance: Vec<ConformanceClaim>,
    /// Markdown entrypoint path.
    pub entrypoint: String,
    /// Optional title.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Optional encoding declaration.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub encoding: Option<String>,
    /// Declared tables.
    #[serde(default)]
    pub tables: Vec<TableManifestEntry>,
    /// Declared image metadata objects.
    #[serde(default)]
    pub images: Vec<ImageManifestEntry>,
    /// Declared annotation metadata objects.
    #[serde(default)]
    pub annotations: Vec<AnnotationManifestEntry>,
    /// Declared asset files or directories.
    #[serde(default)]
    pub assets: Vec<AssetManifestEntry>,
    /// Declared external data resources that are intentionally not packaged.
    #[serde(default, rename = "externalData")]
    pub external_data: Vec<ExternalDataManifestEntry>,
    /// Optional provenance metadata sidecar path.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub provenance: Option<String>,
    /// Optional layout file paths.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub layout: Option<LayoutManifestEntry>,
}

impl Manifest {
    /// Parse a manifest from JSON bytes and validate required basics.
    pub fn from_slice(bytes: &[u8]) -> Result<Self> {
        let manifest: Self = serde_json::from_slice(bytes)?;
        manifest.validate_basic()?;
        Ok(manifest)
    }

    /// Basic manifest validation that does not require package file existence checks.
    pub fn validate_basic(&self) -> Result<()> {
        if self.format != "MCD" {
            return Err(McdError::from_diagnostic(
                Diagnostic::error(
                    "manifest.format.unsupported",
                    "Manifest format must be MCD.",
                )
                .with_source("manifest.json"),
            ));
        }

        if self.version != "0.1" {
            return Err(McdError::from_diagnostic(
                Diagnostic::error(
                    "manifest.version.unsupported",
                    "Manifest version must be 0.1 for this alpha parser.",
                )
                .with_source("manifest.json"),
            ));
        }

        validate_manifest_path("manifest.entrypoint.invalid", &self.entrypoint)?;

        let mut ids = std::collections::HashSet::new();
        for table in &self.tables {
            if table.id.trim().is_empty() {
                return Err(McdError::from_diagnostic(
                    Diagnostic::error("manifest.table.id.empty", "Table id cannot be empty.")
                        .with_source("manifest.json"),
                ));
            }
            if !ids.insert(table.id.clone()) {
                return Err(McdError::from_diagnostic(
                    Diagnostic::error(
                        "manifest.table.id.duplicate",
                        format!("Duplicate table id '{}'.", table.id),
                    )
                    .with_source("manifest.json"),
                ));
            }
            validate_manifest_path("manifest.table.data.invalid", &table.data)?;
            validate_manifest_path("manifest.table.schema.invalid", &table.schema)?;
            for path in table.views.values() {
                validate_manifest_path("manifest.table.view.invalid", path)?;
            }
        }

        let mut ids = std::collections::HashSet::new();
        for image in &self.images {
            if image.id.trim().is_empty() {
                return Err(McdError::from_diagnostic(
                    Diagnostic::error("manifest.image.id.empty", "Image id cannot be empty.")
                        .with_source("manifest.json"),
                ));
            }
            if !ids.insert(image.id.clone()) {
                return Err(McdError::from_diagnostic(
                    Diagnostic::error(
                        "manifest.image.id.duplicate",
                        format!("Duplicate image id '{}'.", image.id),
                    )
                    .with_source("manifest.json"),
                ));
            }
            validate_manifest_path("manifest.image.metadata.invalid", &image.metadata)?;
        }

        let mut ids = std::collections::HashSet::new();
        for annotation in &self.annotations {
            if annotation.id.trim().is_empty() {
                return Err(McdError::from_diagnostic(
                    Diagnostic::error(
                        "manifest.annotation.id.empty",
                        "Annotation id cannot be empty.",
                    )
                    .with_source("manifest.json"),
                ));
            }
            if !ids.insert(annotation.id.clone()) {
                return Err(McdError::from_diagnostic(
                    Diagnostic::error(
                        "manifest.annotation.id.duplicate",
                        format!("Duplicate annotation id '{}'.", annotation.id),
                    )
                    .with_source("manifest.json"),
                ));
            }
            validate_manifest_path("manifest.annotation.metadata.invalid", &annotation.metadata)?;
        }

        for asset in &self.assets {
            validate_manifest_path("manifest.asset.path.invalid", &asset.path)?;
        }

        let mut ids = std::collections::HashSet::new();
        for external_data in &self.external_data {
            external_data.validate(&mut ids)?;
        }

        if let Some(provenance) = &self.provenance {
            validate_manifest_path("manifest.provenance.invalid", provenance)?;
        }

        if let Some(layout) = &self.layout {
            if let Some(styles) = &layout.styles {
                validate_manifest_path("manifest.layout.styles.invalid", styles)?;
            }
            if let Some(page_map) = &layout.page_map {
                validate_manifest_path("manifest.layout.page_map.invalid", page_map)?;
            }
        }

        Ok(())
    }
}

fn manifest_error(code: impl Into<String>, message: impl Into<String>) -> McdError {
    McdError::from_diagnostic(
        Diagnostic::error(code, message).with_source("manifest.json".to_owned()),
    )
}

fn validate_manifest_path(code: &'static str, path: &str) -> Result<()> {
    validate_internal_path(path).map(|_| ()).map_err(|_| {
        McdError::from_diagnostic(
            Diagnostic::error(code, format!("Invalid internal package path '{path}'."))
                .with_source("manifest.json"),
        )
    })
}

/// Supported MCD conformance profiles.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum McdProfile {
    /// Semantic source profile.
    #[serde(rename = "MCD-Core")]
    Core,
    /// Rendered profile.
    #[serde(rename = "MCD-Rendered")]
    Rendered,
    /// Verified profile.
    #[serde(rename = "MCD-Verified")]
    Verified,
    /// Signed profile.
    #[serde(rename = "MCD-Signed")]
    Signed,
}

/// Optional conformance claims.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConformanceClaim {
    /// Core semantic conformance.
    #[serde(rename = "MCD-Core")]
    Core,
    /// Image metadata conformance.
    #[serde(rename = "MCD-Images")]
    Images,
    /// Chart conformance.
    #[serde(rename = "MCD-Charts")]
    Charts,
    /// Strict machine-readable conformance.
    #[serde(rename = "MCD-Strict")]
    Strict,
}

/// Manifest declaration for a table.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TableManifestEntry {
    /// Stable table id.
    pub id: String,
    /// CSV data path.
    pub data: String,
    /// Table schema JSON path.
    pub schema: String,
    /// Named table views.
    #[serde(default)]
    pub views: IndexMap<String, String>,
}

/// Manifest declaration for an image metadata object.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImageManifestEntry {
    /// Stable image id.
    pub id: String,
    /// Image metadata JSON path.
    pub metadata: String,
}

/// Manifest declaration for an annotation metadata object.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AnnotationManifestEntry {
    /// Stable annotation id.
    pub id: String,
    /// Annotation metadata JSON path.
    pub metadata: String,
}

/// Manifest declaration for an asset path or asset directory.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AssetManifestEntry {
    /// Optional stable asset id.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Asset file path or directory prefix.
    pub path: String,
}

/// Manifest declaration for a large or external data resource.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalDataManifestEntry {
    /// Stable external resource id.
    pub id: String,
    /// Absolute URI for the resource.
    pub uri: String,
    /// Declared media type, such as `text/csv` or `application/parquet`.
    pub media_type: String,
    /// Optional integrity hash for deterministic retrieval.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub hash: Option<String>,
    /// Optional expected resource size in bytes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub size_bytes: Option<u64>,
    /// Optional human-readable description.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Optional access metadata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub access: Option<ExternalDataAccess>,
}

impl ExternalDataManifestEntry {
    fn validate(&self, ids: &mut std::collections::HashSet<String>) -> Result<()> {
        if self.id.trim().is_empty() {
            return Err(manifest_error(
                "manifest.external_data.id.empty",
                "External data id cannot be empty.",
            ));
        }
        if !ids.insert(self.id.clone()) {
            return Err(manifest_error(
                "manifest.external_data.id.duplicate",
                format!("Duplicate external data id '{}'.", self.id),
            ));
        }
        if !is_valid_id(&self.id) {
            return Err(manifest_error(
                "manifest.external_data.id.invalid",
                format!("Invalid external data id '{}'.", self.id),
            ));
        }
        if !is_supported_external_uri(&self.uri) {
            return Err(manifest_error(
                "manifest.external_data.uri.invalid",
                format!(
                    "External data URI '{}' must be an absolute http, https, s3, gs, file, or ipfs URI.",
                    self.uri
                ),
            ));
        }
        if !is_media_type(&self.media_type) {
            return Err(manifest_error(
                "manifest.external_data.media_type.invalid",
                format!(
                    "External data media type '{}' is not valid.",
                    self.media_type
                ),
            ));
        }
        if let Some(hash) = &self.hash
            && !is_sha256(hash)
        {
            return Err(manifest_error(
                "manifest.external_data.hash.invalid",
                "External data hash must use sha256:<64 lowercase hex characters>.",
            ));
        }
        if self
            .description
            .as_deref()
            .is_some_and(|description| description.trim().is_empty())
        {
            return Err(manifest_error(
                "manifest.external_data.description.empty",
                "External data description cannot be empty.",
            ));
        }
        if let Some(access) = &self.access {
            access.validate()?;
        }
        Ok(())
    }
}

/// Access notes for an external data resource.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalDataAccess {
    /// Whether access requires network IO.
    #[serde(default)]
    pub requires_network: bool,
    /// Whether access requires credentials or authentication.
    #[serde(default)]
    pub requires_authentication: bool,
    /// Optional human-readable access notes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,
}

impl ExternalDataAccess {
    fn validate(&self) -> Result<()> {
        if self
            .notes
            .as_deref()
            .is_some_and(|notes| notes.trim().is_empty())
        {
            return Err(manifest_error(
                "manifest.external_data.access.notes.empty",
                "External data access notes cannot be empty.",
            ));
        }
        Ok(())
    }
}

pub(crate) fn is_valid_id(id: &str) -> bool {
    let mut chars = id.chars();
    let Some(first) = chars.next() else {
        return false;
    };
    first.is_ascii_alphanumeric()
        && chars.all(|character| {
            character.is_ascii_alphanumeric() || matches!(character, '_' | '.' | '-')
        })
}

pub(crate) fn is_supported_external_uri(uri: &str) -> bool {
    let uri = uri.trim();
    if uri.is_empty() || uri.contains(char::is_whitespace) {
        return false;
    }
    let Some((scheme, rest)) = uri.split_once(':') else {
        return false;
    };
    if scheme.is_empty()
        || !scheme.chars().all(|character| {
            character.is_ascii_alphanumeric() || matches!(character, '+' | '.' | '-')
        })
    {
        return false;
    }
    if !rest.starts_with("//") || rest.len() <= 2 {
        return false;
    }
    matches!(scheme, "http" | "https" | "s3" | "gs" | "file" | "ipfs")
}

pub(crate) fn is_sha256(value: &str) -> bool {
    value.strip_prefix("sha256:").is_some_and(|hex| {
        hex.len() == 64
            && hex
                .chars()
                .all(|ch| ch.is_ascii_hexdigit() && !ch.is_ascii_uppercase())
    })
}

pub(crate) fn is_media_type(value: &str) -> bool {
    let Some((kind, subtype)) = value.split_once('/') else {
        return false;
    };
    !kind.is_empty()
        && !subtype.is_empty()
        && !kind.contains(char::is_whitespace)
        && !subtype.contains(char::is_whitespace)
}

/// Optional layout paths in the manifest.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LayoutManifestEntry {
    /// Styles JSON path.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub styles: Option<String>,
    /// Page map JSON path.
    #[serde(default, rename = "pageMap", skip_serializing_if = "Option::is_none")]
    pub page_map: Option<String>,
}

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

    #[test]
    fn parses_minimal_manifest() {
        let manifest = Manifest::from_slice(
            br#"{"format":"MCD","version":"0.1","profile":"MCD-Core","entrypoint":"content/main.md"}"#,
        )
        .expect("manifest parses");

        assert_eq!(manifest.entrypoint, "content/main.md");
        assert!(manifest.tables.is_empty());
    }

    #[test]
    fn rejects_duplicate_table_ids() {
        let err = Manifest::from_slice(
            br#"{
                "format":"MCD",
                "version":"0.1",
                "profile":"MCD-Core",
                "entrypoint":"content/main.md",
                "tables":[
                    {"id":"revenue","data":"tables/a.csv","schema":"tables/a.schema.json"},
                    {"id":"revenue","data":"tables/b.csv","schema":"tables/b.schema.json"}
                ]
            }"#,
        )
        .expect_err("duplicate ids should fail");

        assert_eq!(
            err.diagnostic().map(|d| d.code.as_str()),
            Some("manifest.table.id.duplicate")
        );
    }

    #[test]
    fn parses_external_data_references() {
        let manifest = Manifest::from_slice(
            br#"{
                "format":"MCD",
                "version":"0.1",
                "profile":"MCD-Core",
                "entrypoint":"content/main.md",
                "externalData":[{
                    "id":"sensor-log",
                    "uri":"https://example.com/data/sensor-log.csv",
                    "mediaType":"text/csv",
                    "hash":"sha256:0000000000000000000000000000000000000000000000000000000000000000",
                    "sizeBytes":128,
                    "description":"Canonical sensor log.",
                    "access":{
                        "requiresNetwork":true,
                        "requiresAuthentication":false,
                        "notes":"Public HTTPS dataset."
                    }
                }]
            }"#,
        )
        .expect("manifest parses");

        assert_eq!(manifest.external_data.len(), 1);
        assert_eq!(manifest.external_data[0].id, "sensor-log");
        assert_eq!(manifest.external_data[0].media_type, "text/csv");
    }

    #[test]
    fn rejects_invalid_external_data_references() {
        let err = Manifest::from_slice(
            br#"{
                "format":"MCD",
                "version":"0.1",
                "profile":"MCD-Core",
                "entrypoint":"content/main.md",
                "externalData":[{
                    "id":"sensor-log",
                    "uri":"data/sensor-log.csv",
                    "mediaType":"text/csv"
                }]
            }"#,
        )
        .expect_err("relative URI should fail");

        assert_eq!(
            err.diagnostic().map(|d| d.code.as_str()),
            Some("manifest.external_data.uri.invalid")
        );

        let err = Manifest::from_slice(
            br#"{
                "format":"MCD",
                "version":"0.1",
                "profile":"MCD-Core",
                "entrypoint":"content/main.md",
                "externalData":[
                    {"id":"sensor-log","uri":"https://example.com/a.csv","mediaType":"text/csv"},
                    {"id":"sensor-log","uri":"https://example.com/b.csv","mediaType":"text/csv"}
                ]
            }"#,
        )
        .expect_err("duplicate id should fail");

        assert_eq!(
            err.diagnostic().map(|d| d.code.as_str()),
            Some("manifest.external_data.id.duplicate")
        );
    }

    #[test]
    fn parses_provenance_sidecar_path() {
        let manifest = Manifest::from_slice(
            br#"{
                "format":"MCD",
                "version":"0.1",
                "profile":"MCD-Core",
                "entrypoint":"content/main.md",
                "provenance":"provenance/provenance.json"
            }"#,
        )
        .expect("manifest parses");

        assert_eq!(
            manifest.provenance.as_deref(),
            Some("provenance/provenance.json")
        );
    }

    #[test]
    fn rejects_invalid_provenance_sidecar_path() {
        let err = Manifest::from_slice(
            br#"{
                "format":"MCD",
                "version":"0.1",
                "profile":"MCD-Core",
                "entrypoint":"content/main.md",
                "provenance":"../provenance.json"
            }"#,
        )
        .expect_err("unsafe path should fail");

        assert_eq!(
            err.diagnostic().map(|d| d.code.as_str()),
            Some("manifest.provenance.invalid")
        );
    }
}