type-bridge-schema 2.2.2

Canonical schema resolution and projection engine for type-bridge
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
use std::collections::BTreeMap;

use type_bridge_contract::diagnostic::{Diagnostic, DiagnosticCategory, DiagnosticCode};
use type_bridge_contract::schema::{
    DocumentFingerprint, DocumentId, SchemaDiagnostic, SchemaDiagnostics,
    SchemaDocumentSetFingerprint, SourceSpan,
};

use crate::schema_set::SchemaSetManifestDocument;

/// Default maximum number of documents in one schema document set.
pub const DEFAULT_MAX_DOCUMENTS: usize = 4_096;
/// Default maximum aggregate UTF-8 source bytes in one document set.
pub const DEFAULT_MAX_AGGREGATE_BYTES: usize = 64 * 1024 * 1024;
/// Default maximum UTF-8 source bytes in one document.
pub const DEFAULT_MAX_DOCUMENT_BYTES: usize = 16 * 1024 * 1024;
/// Default maximum YAML node nesting depth.
pub const DEFAULT_MAX_DEPTH: usize = 64;
/// Default maximum YAML nodes in one document.
pub const DEFAULT_MAX_NODES: usize = 65_536;
/// Default maximum source-token bytes in one YAML scalar.
///
/// Canonical decoded strings have their own lower contract bound. The parser
/// permits quoting and escape overhead up to the already-bounded document
/// source ceiling.
pub const DEFAULT_MAX_SCALAR_SOURCE_BYTES: usize = DEFAULT_MAX_DOCUMENT_BYTES;

/// Resource ceilings applied before a schema document becomes trusted input.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SchemaParseLimits {
    max_documents: usize,
    max_aggregate_bytes: usize,
    max_document_bytes: usize,
    max_depth: usize,
    max_nodes: usize,
    max_scalar_source_bytes: usize,
}

impl SchemaParseLimits {
    /// Creates explicit parser ceilings.
    #[must_use]
    pub const fn new(
        max_documents: usize,
        max_aggregate_bytes: usize,
        max_document_bytes: usize,
        max_depth: usize,
        max_nodes: usize,
        max_scalar_source_bytes: usize,
    ) -> Self {
        Self {
            max_documents,
            max_aggregate_bytes,
            max_document_bytes,
            max_depth,
            max_nodes,
            max_scalar_source_bytes,
        }
    }

    /// Returns the document-count ceiling.
    #[must_use]
    pub const fn max_documents(self) -> usize {
        self.max_documents
    }

    /// Returns the aggregate-source-byte ceiling.
    #[must_use]
    pub const fn max_aggregate_bytes(self) -> usize {
        self.max_aggregate_bytes
    }

    /// Returns the per-document source-byte ceiling.
    #[must_use]
    pub const fn max_document_bytes(self) -> usize {
        self.max_document_bytes
    }

    /// Returns the node-depth ceiling.
    #[must_use]
    pub const fn max_depth(self) -> usize {
        self.max_depth
    }

    /// Returns the per-document node-count ceiling.
    #[must_use]
    pub const fn max_nodes(self) -> usize {
        self.max_nodes
    }

    /// Returns the YAML scalar source-token byte ceiling.
    #[must_use]
    pub const fn max_scalar_source_bytes(self) -> usize {
        self.max_scalar_source_bytes
    }

    /// Returns the YAML scalar source-token byte ceiling.
    ///
    /// This compatibility spelling preserves callers of the original parser
    /// limit API; the limit applies to exact YAML source bytes, not decoded
    /// canonical string bytes.
    #[must_use]
    pub const fn max_scalar_bytes(self) -> usize {
        self.max_scalar_source_bytes()
    }
}

impl Default for SchemaParseLimits {
    fn default() -> Self {
        Self::new(
            DEFAULT_MAX_DOCUMENTS,
            DEFAULT_MAX_AGGREGATE_BYTES,
            DEFAULT_MAX_DOCUMENT_BYTES,
            DEFAULT_MAX_DEPTH,
            DEFAULT_MAX_NODES,
            DEFAULT_MAX_SCALAR_SOURCE_BYTES,
        )
    }
}

/// YAML scalar spelling retained by the lossless document layer.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum YamlScalarStyle {
    /// Unquoted scalar.
    Plain,
    /// Single-quoted scalar.
    SingleQuoted,
    /// Double-quoted scalar.
    DoubleQuoted,
    /// Literal block scalar.
    Literal,
    /// Folded block scalar.
    Folded,
}

/// YAML collection spelling retained by the lossless document layer.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum YamlCollectionStyle {
    /// Indentation-delimited collection.
    Block,
    /// Bracket-delimited collection.
    Flow,
}

/// Placement reported for a source comment.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CommentPlacement {
    /// Comment immediately above the associated syntax.
    Above,
    /// Comment to the right of syntax on the same line.
    Right,
    /// Free-standing comment.
    Free,
    /// Comment after the last item in a collection.
    Last,
}

/// A comment retained with its exact source location.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SchemaComment {
    text: String,
    placement: CommentPlacement,
    span: SourceSpan,
}

impl SchemaComment {
    pub(crate) fn new(text: String, placement: CommentPlacement, span: SourceSpan) -> Self {
        Self {
            text,
            placement,
            span,
        }
    }

    /// Returns the decoded comment text.
    #[must_use]
    pub fn text(&self) -> &str {
        &self.text
    }

    /// Returns the parser-reported placement.
    #[must_use]
    pub const fn placement(&self) -> CommentPlacement {
        self.placement
    }

    /// Returns the exact source span.
    #[must_use]
    pub const fn span(&self) -> &SourceSpan {
        &self.span
    }
}

/// A decoded YAML scalar with its exact spelling and source location.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct YamlScalar {
    value: String,
    raw: String,
    style: YamlScalarStyle,
    span: SourceSpan,
}

impl YamlScalar {
    pub(crate) fn new(
        value: String,
        raw: String,
        style: YamlScalarStyle,
        span: SourceSpan,
    ) -> Self {
        Self {
            value,
            raw,
            style,
            span,
        }
    }

    /// Returns the parser-decoded scalar value.
    #[must_use]
    pub fn value(&self) -> &str {
        &self.value
    }

    /// Returns the exact scalar spelling from the source document.
    #[must_use]
    pub fn raw(&self) -> &str {
        &self.raw
    }

    /// Returns the scalar style.
    #[must_use]
    pub const fn style(&self) -> YamlScalarStyle {
        self.style
    }

    /// Returns the exact source span.
    #[must_use]
    pub const fn span(&self) -> &SourceSpan {
        &self.span
    }
}

/// One string-keyed YAML mapping entry.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct YamlMappingEntry {
    key: YamlScalar,
    value: YamlNode,
}

impl YamlMappingEntry {
    pub(crate) const fn new(key: YamlScalar, value: YamlNode) -> Self {
        Self { key, value }
    }

    /// Returns the string key.
    #[must_use]
    pub const fn key(&self) -> &YamlScalar {
        &self.key
    }

    /// Returns the mapped node.
    #[must_use]
    pub const fn value(&self) -> &YamlNode {
        &self.value
    }
}

/// A lossless, insertion-ordered YAML mapping.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct YamlMapping {
    entries: Vec<YamlMappingEntry>,
    style: YamlCollectionStyle,
    span: SourceSpan,
}

impl YamlMapping {
    pub(crate) const fn new(
        entries: Vec<YamlMappingEntry>,
        style: YamlCollectionStyle,
        span: SourceSpan,
    ) -> Self {
        Self {
            entries,
            style,
            span,
        }
    }

    /// Returns entries in source order.
    #[must_use]
    pub fn entries(&self) -> &[YamlMappingEntry] {
        &self.entries
    }

    /// Returns the collection style.
    #[must_use]
    pub const fn style(&self) -> YamlCollectionStyle {
        self.style
    }

    /// Returns the collection source span.
    #[must_use]
    pub const fn span(&self) -> &SourceSpan {
        &self.span
    }
}

/// A lossless YAML sequence.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct YamlSequence {
    items: Vec<YamlNode>,
    style: YamlCollectionStyle,
    span: SourceSpan,
}

impl YamlSequence {
    pub(crate) const fn new(
        items: Vec<YamlNode>,
        style: YamlCollectionStyle,
        span: SourceSpan,
    ) -> Self {
        Self { items, style, span }
    }

    /// Returns items in source order.
    #[must_use]
    pub fn items(&self) -> &[YamlNode] {
        &self.items
    }

    /// Returns the collection style.
    #[must_use]
    pub const fn style(&self) -> YamlCollectionStyle {
        self.style
    }

    /// Returns the collection source span.
    #[must_use]
    pub const fn span(&self) -> &SourceSpan {
        &self.span
    }
}

/// A YAML node accepted by the closed schema-document grammar.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum YamlNode {
    /// Scalar node.
    Scalar(YamlScalar),
    /// Sequence node.
    Sequence(YamlSequence),
    /// Mapping node.
    Mapping(YamlMapping),
}

impl YamlNode {
    /// Returns the node source span.
    #[must_use]
    pub const fn span(&self) -> &SourceSpan {
        match self {
            Self::Scalar(value) => value.span(),
            Self::Sequence(value) => value.span(),
            Self::Mapping(value) => value.span(),
        }
    }

    /// Returns this node as a mapping, if it is one.
    #[must_use]
    pub const fn as_mapping(&self) -> Option<&YamlMapping> {
        match self {
            Self::Mapping(value) => Some(value),
            Self::Scalar(_) | Self::Sequence(_) => None,
        }
    }

    /// Returns this node as a sequence, if it is one.
    #[must_use]
    pub const fn as_sequence(&self) -> Option<&YamlSequence> {
        match self {
            Self::Sequence(value) => Some(value),
            Self::Scalar(_) | Self::Mapping(_) => None,
        }
    }

    /// Returns this node as a scalar, if it is one.
    #[must_use]
    pub const fn as_scalar(&self) -> Option<&YamlScalar> {
        match self {
            Self::Scalar(value) => Some(value),
            Self::Sequence(_) | Self::Mapping(_) => None,
        }
    }
}

/// One parsed schema source document.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SchemaDocument {
    id: DocumentId,
    source: String,
    fingerprint: DocumentFingerprint,
    root: YamlMapping,
    comments: Vec<SchemaComment>,
}

impl SchemaDocument {
    pub(crate) const fn new(
        id: DocumentId,
        source: String,
        fingerprint: DocumentFingerprint,
        root: YamlMapping,
        comments: Vec<SchemaComment>,
    ) -> Self {
        Self {
            id,
            source,
            fingerprint,
            root,
            comments,
        }
    }

    /// Parses one document with the default resource ceilings.
    pub fn parse(id: DocumentId, source: impl Into<String>) -> Result<Self, SchemaDiagnostics> {
        Self::parse_with_limits(id, source, SchemaParseLimits::default())
    }

    /// Parses one document with explicit resource ceilings.
    pub fn parse_with_limits(
        id: DocumentId,
        source: impl Into<String>,
        limits: SchemaParseLimits,
    ) -> Result<Self, SchemaDiagnostics> {
        crate::yaml::parse_document_with_limits(id, source.into(), limits)
    }

    /// Returns the stable document identifier.
    #[must_use]
    pub const fn id(&self) -> &DocumentId {
        &self.id
    }

    /// Returns the source exactly as supplied by the caller.
    #[must_use]
    pub fn source(&self) -> &str {
        &self.source
    }

    /// Returns the source-byte fingerprint.
    #[must_use]
    pub const fn fingerprint(&self) -> &DocumentFingerprint {
        &self.fingerprint
    }

    /// Returns the required root mapping.
    #[must_use]
    pub const fn root(&self) -> &YamlMapping {
        &self.root
    }

    /// Returns all comments in source order.
    #[must_use]
    pub fn comments(&self) -> &[SchemaComment] {
        &self.comments
    }
}

/// A deterministic, identifier-keyed collection of schema documents.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct SchemaDocumentSet {
    documents: BTreeMap<DocumentId, SchemaDocument>,
    manifest: Option<SchemaSetManifestDocument>,
}

impl SchemaDocumentSet {
    /// Parses documents with default resource ceilings.
    pub fn parse<I, S>(sources: I) -> Result<Self, SchemaDiagnostics>
    where
        I: IntoIterator<Item = (DocumentId, S)>,
        S: Into<String>,
    {
        Self::parse_with_limits(sources, SchemaParseLimits::default())
    }

    /// Parses documents with explicit per-document and aggregate ceilings.
    pub fn parse_with_limits<I, S>(
        sources: I,
        limits: SchemaParseLimits,
    ) -> Result<Self, SchemaDiagnostics>
    where
        I: IntoIterator<Item = (DocumentId, S)>,
        S: Into<String>,
    {
        let mut documents: BTreeMap<DocumentId, SchemaDocument> = BTreeMap::new();
        let mut aggregate_bytes = 0usize;

        for (id, source) in sources {
            if documents.len() >= limits.max_documents() {
                return Err(resource_diagnostic(
                    "schema_document_count_limit",
                    format!(
                        "schema document count exceeds the limit of {}",
                        limits.max_documents()
                    ),
                    None,
                ));
            }

            let source = source.into();
            aggregate_bytes = aggregate_bytes.checked_add(source.len()).ok_or_else(|| {
                resource_diagnostic(
                    "schema_aggregate_size_limit",
                    "schema aggregate source size overflowed",
                    None,
                )
            })?;
            if aggregate_bytes > limits.max_aggregate_bytes() {
                return Err(resource_diagnostic(
                    "schema_aggregate_size_limit",
                    format!(
                        "schema aggregate source size exceeds the limit of {} bytes",
                        limits.max_aggregate_bytes()
                    ),
                    None,
                ));
            }

            if let Some(existing) = documents.get(&id) {
                return Err(crate::yaml::diagnostic_with_related(
                    DiagnosticCategory::InvalidContract,
                    "duplicate_schema_document",
                    format!("schema document identifier `{}` is duplicated", id.as_str()),
                    existing.root().span().clone(),
                    existing.root().span().clone(),
                    "first document with this identifier",
                ));
            }

            let document = SchemaDocument::parse_with_limits(id.clone(), source, limits)?;
            documents.insert(id, document);
        }

        Ok(Self {
            documents,
            manifest: None,
        })
    }

    pub(crate) fn attach_manifest(&mut self, manifest: SchemaSetManifestDocument) {
        self.manifest = Some(manifest);
    }

    /// Returns the schema-set manifest retained by file-backed loading, if any.
    #[must_use]
    pub const fn manifest(&self) -> Option<&SchemaSetManifestDocument> {
        self.manifest.as_ref()
    }

    /// Fingerprints ordered portable document paths and exact source fingerprints.
    pub fn fingerprint(&self) -> Result<SchemaDocumentSetFingerprint, SchemaDiagnostics> {
        let mut canonical = Vec::new();
        canonical.extend_from_slice(
            &u64::try_from(self.documents.len())
                .expect("schema document count is bounded below u64::MAX")
                .to_be_bytes(),
        );
        for (id, document) in &self.documents {
            canonical.extend_from_slice(
                &u64::try_from(id.as_str().len())
                    .expect("document identifier length is bounded below u64::MAX")
                    .to_be_bytes(),
            );
            canonical.extend_from_slice(id.as_str().as_bytes());
            canonical.extend_from_slice(&document.fingerprint().as_fingerprint().digest().bytes());
        }
        SchemaDocumentSetFingerprint::compute(&canonical)
            .map_err(|error| SchemaDiagnostics::one(SchemaDiagnostic::new(error, None)))
    }

    /// Returns the number of documents.
    #[must_use]
    pub fn len(&self) -> usize {
        self.documents.len()
    }

    /// Reports whether the set has no documents.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.documents.is_empty()
    }

    /// Returns a document by stable identifier.
    #[must_use]
    pub fn get(&self, id: &DocumentId) -> Option<&SchemaDocument> {
        self.documents.get(id)
    }

    /// Iterates documents in stable identifier order.
    pub fn iter(&self) -> impl ExactSizeIterator<Item = (&DocumentId, &SchemaDocument)> {
        self.documents.iter()
    }
}

pub(crate) fn resource_diagnostic(
    code: &'static str,
    message: impl Into<String>,
    primary: Option<SourceSpan>,
) -> SchemaDiagnostics {
    let diagnostic = Diagnostic::new(
        DiagnosticCategory::ResourceLimit,
        DiagnosticCode::new(code).expect("static schema diagnostic code is valid"),
        message,
    );
    SchemaDiagnostics::one(SchemaDiagnostic::new(diagnostic, primary))
}