seam-core 0.1.2

The Seam validation engine: .seam parsing, schema compilation, and validation.
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
//! The `.seam` front end.
//!
//! ```text
//! file        := declaration*
//! declaration := object | union
//! object      := "schema" ident "{" field* "}"
//! union       := "union" ident "@tag" "(" string ")" "{" variant* "}"
//! variant     := value ":" ident
//! field       := ident ":" "optional"? type rule*
//! type        := base "?"?
//! base        := ident | "[" type "]" | enum
//! enum        := "enum" "{" value ("," value)* ","? "}"
//! value       := ident | string
//! rule        := "@" ident "(" args ")"
//! format      := "@format" "(" name ")"      // a closed set, never a regex
//! ```

use crate::format::Format;
use crate::schema::{
    Field, IntType, IntWidth, ObjectType, Presence, Rule, Schema, Type, UnionType, Variant,
};
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
    pub line: usize,
    pub column: usize,
    pub message: String,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}: {}", self.line, self.column, self.message)
    }
}

impl std::error::Error for ParseError {}

pub fn parse(source: &str) -> Result<Schema, ParseError> {
    let tokens = lex(source)?;
    let mut p = Parser { toks: tokens, pos: 0, refs: Vec::new(), variant_refs: Vec::new() };
    let schema = p.file()?;
    p.resolve(&schema)?;
    Ok(schema)
}

// ---------------------------------------------------------------- lexer

#[derive(Debug, Clone, PartialEq, Eq)]
enum Tok {
    Ident(String),
    Str(String),
    Int(i128),
    LBrace,
    RBrace,
    LBracket,
    RBracket,
    LParen,
    RParen,
    Colon,
    Question,
    At,
    Comma,
    RangeIncl,
    Eof,
}

impl Tok {
    fn describe(&self) -> String {
        match self {
            Tok::Ident(s) => format!("`{s}`"),
            Tok::Str(s) => format!("string \"{s}\""),
            Tok::Int(n) => format!("`{n}`"),
            Tok::LBrace => "`{`".into(),
            Tok::RBrace => "`}`".into(),
            Tok::LBracket => "`[`".into(),
            Tok::RBracket => "`]`".into(),
            Tok::LParen => "`(`".into(),
            Tok::RParen => "`)`".into(),
            Tok::Colon => "`:`".into(),
            Tok::Question => "`?`".into(),
            Tok::At => "`@`".into(),
            Tok::Comma => "`,`".into(),
            Tok::RangeIncl => "`..=`".into(),
            Tok::Eof => "end of input".into(),
        }
    }
}

#[derive(Debug, Clone)]
struct Token {
    tok: Tok,
    line: usize,
    column: usize,
}

fn lex(src: &str) -> Result<Vec<Token>, ParseError> {
    let chars: Vec<char> = src.chars().collect();
    let mut out = Vec::new();
    let mut i = 0;
    let mut line = 1;
    let mut col = 1;

    while let Some(&c) = chars.get(i) {
        if c == '\n' {
            i += 1;
            line += 1;
            col = 1;
            continue;
        }
        if c.is_whitespace() {
            i += 1;
            col += 1;
            continue;
        }
        if c == '/' && chars.get(i + 1) == Some(&'/') {
            while matches!(chars.get(i), Some(&ch) if ch != '\n') {
                i += 1;
                col += 1;
            }
            continue;
        }

        let (tl, tc) = (line, col);

        if c == '.' && chars.get(i + 1) == Some(&'.') && chars.get(i + 2) == Some(&'=') {
            out.push(Token { tok: Tok::RangeIncl, line: tl, column: tc });
            i += 3;
            col += 3;
            continue;
        }

        let symbol = match c {
            '{' => Some(Tok::LBrace),
            '}' => Some(Tok::RBrace),
            '[' => Some(Tok::LBracket),
            ']' => Some(Tok::RBracket),
            '(' => Some(Tok::LParen),
            ')' => Some(Tok::RParen),
            ':' => Some(Tok::Colon),
            '?' => Some(Tok::Question),
            '@' => Some(Tok::At),
            ',' => Some(Tok::Comma),
            _ => None,
        };
        if let Some(tok) = symbol {
            out.push(Token { tok, line: tl, column: tc });
            i += 1;
            col += 1;
            continue;
        }

        if c == '"' {
            i += 1;
            col += 1;
            let mut s = String::new();
            loop {
                match chars.get(i) {
                    None | Some('\n') => {
                        return Err(ParseError {
                            line: tl,
                            column: tc,
                            message: "unterminated string".into(),
                        })
                    }
                    Some('"') => {
                        i += 1;
                        col += 1;
                        break;
                    }
                    Some(&ch) => {
                        s.push(ch);
                        i += 1;
                        col += 1;
                    }
                }
            }
            out.push(Token { tok: Tok::Str(s), line: tl, column: tc });
            continue;
        }

        let negative = c == '-' && matches!(chars.get(i + 1), Some(ch) if ch.is_ascii_digit());
        if c.is_ascii_digit() || negative {
            let mut s = String::new();
            if negative {
                s.push('-');
                i += 1;
                col += 1;
            }
            while let Some(&ch) = chars.get(i) {
                if ch.is_ascii_digit() {
                    s.push(ch);
                } else if ch != '_' {
                    break;
                }
                i += 1;
                col += 1;
            }
            let n = s.parse::<i128>().map_err(|_| ParseError {
                line: tl,
                column: tc,
                message: format!("`{s}` does not fit a 128-bit integer"),
            })?;
            out.push(Token { tok: Tok::Int(n), line: tl, column: tc });
            continue;
        }

        if c.is_alphabetic() || c == '_' {
            let mut s = String::new();
            while let Some(&ch) = chars.get(i) {
                if ch.is_alphanumeric() || ch == '_' {
                    s.push(ch);
                    i += 1;
                    col += 1;
                } else {
                    break;
                }
            }
            out.push(Token { tok: Tok::Ident(s), line: tl, column: tc });
            continue;
        }

        return Err(ParseError {
            line: tl,
            column: tc,
            message: format!("unexpected character `{c}`"),
        });
    }

    out.push(Token { tok: Tok::Eof, line, column: col });
    Ok(out)
}

// --------------------------------------------------------------- parser

struct Parser {
    toks: Vec<Token>,
    pos: usize,
    /// Type references, checked once the whole file is known so that a schema
    /// may refer to one declared later.
    refs: Vec<(String, usize, usize)>,
    /// `(union, tag field, variant type, line, column)`, checked in the same
    /// pass and for the same reason.
    variant_refs: Vec<(String, String, String, usize, usize)>,
}

impl Parser {
    fn cur(&self) -> &Token {
        match self.toks.get(self.pos) {
            Some(t) => t,
            // `lex` always appends Eof, and `bump` never moves past it.
            None => match self.toks.last() {
                Some(t) => t,
                None => &EOF,
            },
        }
    }

    fn at(&self, t: &Tok) -> bool {
        &self.cur().tok == t
    }

    fn at_keyword(&self, kw: &str) -> bool {
        matches!(&self.cur().tok, Tok::Ident(s) if s == kw)
    }

    fn bump(&mut self) -> Token {
        let t = self.cur().clone();
        if !matches!(t.tok, Tok::Eof) {
            self.pos += 1;
        }
        t
    }

    fn eat(&mut self, t: &Tok) -> bool {
        if self.at(t) {
            self.bump();
            true
        } else {
            false
        }
    }

    fn eat_keyword(&mut self, kw: &str) -> bool {
        if self.at_keyword(kw) {
            self.bump();
            true
        } else {
            false
        }
    }

    fn error<T>(&self, message: String) -> Result<T, ParseError> {
        let t = self.cur();
        Err(ParseError { line: t.line, column: t.column, message })
    }

    fn expect(&mut self, t: &Tok) -> Result<(), ParseError> {
        if self.eat(t) {
            Ok(())
        } else {
            let found = self.cur().tok.describe();
            self.error(format!("expected {}, found {found}", t.describe()))
        }
    }

    fn expect_ident(&mut self, what: &str) -> Result<String, ParseError> {
        match &self.cur().tok {
            Tok::Ident(s) => {
                let s = s.clone();
                self.bump();
                Ok(s)
            }
            other => {
                let found = other.describe();
                self.error(format!("expected {what}, found {found}"))
            }
        }
    }

    fn expect_int(&mut self) -> Result<i128, ParseError> {
        match self.cur().tok {
            Tok::Int(n) => {
                self.bump();
                Ok(n)
            }
            ref other => {
                let found = other.describe();
                self.error(format!("expected a number, found {found}"))
            }
        }
    }

    fn file(&mut self) -> Result<Schema, ParseError> {
        let mut schema = Schema::default();
        while !self.at(&Tok::Eof) {
            if self.at_keyword("union") {
                let (name, union) = self.union_declaration()?;
                // One namespace: an object and a union cannot share a name,
                // because a reference names one thing.
                if schema.declares(&name) {
                    return self.error(format!("`{name}` is declared more than once"));
                }
                schema.unions.insert(name, union);
                continue;
            }
            if !self.at_keyword("schema") {
                let found = self.cur().tok.describe();
                return self.error(format!("expected `schema` or `union`, found {found}"));
            }
            let (name, ty) = self.declaration()?;
            if schema.declares(&name) {
                return self.error(format!("`{name}` is declared more than once"));
            }
            schema.types.insert(name, ty);
        }
        Ok(schema)
    }

    fn union_declaration(&mut self) -> Result<(String, UnionType), ParseError> {
        self.bump(); // `union`
        let name = self.expect_ident("a union name")?;

        // `@tag` is required. A union that defaulted to a conventional field
        // name would be guessing which value decides what the payload means,
        // and the whole point of the file is that nothing is guessed.
        let at = self.cur().clone();
        if !self.eat(&Tok::At) {
            let found = at.tok.describe();
            return self.error(format!(
                "`{name}` needs `@tag(\"...\")` naming the field that decides the variant, found {found}"
            ));
        }
        let attribute = self.expect_ident("`tag`")?;
        if attribute != "tag" {
            return Err(ParseError {
                line: at.line,
                column: at.column,
                message: format!("unknown union attribute `@{attribute}`, expected `@tag`"),
            });
        }
        self.expect(&Tok::LParen)?;
        let tag = match &self.cur().tok {
            Tok::Str(s) | Tok::Ident(s) => s.clone(),
            other => {
                let found = other.describe();
                return self.error(format!("expected the tag field's name, found {found}"));
            }
        };
        self.bump();
        if tag.is_empty() {
            return self.error("the tag field's name cannot be empty".into());
        }
        self.expect(&Tok::RParen)?;

        self.expect(&Tok::LBrace)?;
        let mut variants: Vec<Variant> = Vec::new();
        while !self.at(&Tok::RBrace) && !self.at(&Tok::Eof) {
            let value = match &self.cur().tok {
                Tok::Ident(s) | Tok::Str(s) => s.clone(),
                other => {
                    let found = other.describe();
                    return self.error(format!("expected a variant tag value, found {found}"));
                }
            };
            self.bump();
            self.expect(&Tok::Colon)?;

            let t = self.cur().clone();
            let type_name = self.expect_ident("a schema name")?;
            if builtin(&type_name).is_some() {
                return Err(ParseError {
                    line: t.line,
                    column: t.column,
                    message: format!(
                        "a variant must be a declared `schema`, and `{type_name}` is a built-in type"
                    ),
                });
            }
            if variants.iter().any(|v| v.tag == value) {
                return self.error(format!("`{name}` lists the tag `{value}` twice"));
            }
            // Recorded so the second pass can check it, and check that the
            // object it names does not itself declare the tag field.
            self.variant_refs.push((
                name.clone(),
                tag.clone(),
                type_name.clone(),
                t.line,
                t.column,
            ));
            variants.push(Variant { tag: value, type_name });
        }
        self.expect(&Tok::RBrace)?;

        if variants.is_empty() {
            return self.error(format!("`{name}` needs at least one variant"));
        }

        Ok((name.clone(), UnionType { name, tag, variants }))
    }

    fn declaration(&mut self) -> Result<(String, ObjectType), ParseError> {
        self.bump(); // `schema`
        let name = self.expect_ident("a schema name")?;
        self.expect(&Tok::LBrace)?;

        let mut fields: Vec<Field> = Vec::new();
        while !self.at(&Tok::RBrace) && !self.at(&Tok::Eof) {
            let field = self.field()?;
            if fields.iter().any(|f| f.name == field.name) {
                return self.error(format!("`{name}` declares `{}` more than once", field.name));
            }
            fields.push(field);
        }
        self.expect(&Tok::RBrace)?;

        Ok((
            name.clone(),
            ObjectType { name, fields, deny_unknown_fields: true },
        ))
    }

    fn field(&mut self) -> Result<Field, ParseError> {
        let name = self.expect_ident("a field name")?;
        self.expect(&Tok::Colon)?;
        let optional = self.eat_keyword("optional");
        let (ty, nullable) = self.ty()?;

        let mut rules = Vec::new();
        while self.at(&Tok::At) {
            rules.push(self.rule()?);
        }

        Ok(Field { name, ty, presence: Presence { optional, nullable }, rules })
    }

    /// Returns the type and whether a `?` suffix marked it nullable.
    fn ty(&mut self) -> Result<(Type, bool), ParseError> {
        let base = if self.eat(&Tok::LBracket) {
            let (item, item_nullable) = self.ty()?;
            self.expect(&Tok::RBracket)?;
            Type::Array { item: Box::new(item), item_nullable }
        } else if self.at_keyword("enum") {
            self.enumeration()?
        } else {
            let t = self.cur().clone();
            let name = self.expect_ident("a type")?;
            match builtin(&name) {
                Some(ty) => ty,
                None => {
                    self.refs.push((name.clone(), t.line, t.column));
                    Type::Ref(name)
                }
            }
        };

        let nullable = self.eat(&Tok::Question);
        Ok((base, nullable))
    }

    fn enumeration(&mut self) -> Result<Type, ParseError> {
        self.bump(); // `enum`
        self.expect(&Tok::LBrace)?;

        let mut values: Vec<String> = Vec::new();
        loop {
            if self.at(&Tok::RBrace) {
                break;
            }
            let value = match &self.cur().tok {
                Tok::Ident(s) | Tok::Str(s) => s.clone(),
                other => {
                    let found = other.describe();
                    return self.error(format!("expected an enum value, found {found}"));
                }
            };
            self.bump();
            if values.contains(&value) {
                return self.error(format!("`{value}` is listed twice"));
            }
            values.push(value);
            if !self.eat(&Tok::Comma) {
                break;
            }
        }
        self.expect(&Tok::RBrace)?;

        if values.is_empty() {
            return self.error("an enum needs at least one value".into());
        }
        Ok(Type::Enum(values))
    }

    fn rule(&mut self) -> Result<Rule, ParseError> {
        self.expect(&Tok::At)?;
        let at = self.cur().clone();
        let name = self.expect_ident("a rule name")?;
        self.expect(&Tok::LParen)?;

        let rule = match name.as_str() {
            "min_len" => Rule::MinLen(self.count(&name)?),
            "max_len" => Rule::MaxLen(self.count(&name)?),
            "min_items" => Rule::MinItems(self.count(&name)?),
            "max_items" => Rule::MaxItems(self.count(&name)?),
            "format" => {
                let at = self.cur().clone();
                let value = match &self.cur().tok {
                    Tok::Ident(s) | Tok::Str(s) => s.clone(),
                    other => {
                        let found = other.describe();
                        return self.error(format!("expected a format name, found {found}"));
                    }
                };
                self.bump();
                match Format::parse(&value) {
                    Some(f) => Rule::Format(f),
                    None => {
                        // The set is closed on purpose. Naming what is
                        // available beats leaving the author to guess, and
                        // guessing is what a regex would have invited.
                        let names: Vec<&str> = Format::ALL.iter().map(|f| f.name()).collect();
                        return Err(ParseError {
                            line: at.line,
                            column: at.column,
                            message: format!(
                                "unknown format `{value}`, expected one of: {}",
                                names.join(", ")
                            ),
                        });
                    }
                }
            }
            "range" => {
                let min = self.expect_int()?;
                self.expect(&Tok::RangeIncl)?;
                let max = self.expect_int()?;
                if min > max {
                    return self.error(format!("`range({min}..={max})` is empty"));
                }
                Rule::Range { min, max }
            }
            _ => {
                return Err(ParseError {
                    line: at.line,
                    column: at.column,
                    message: format!("unknown rule `@{name}`"),
                })
            }
        };

        self.expect(&Tok::RParen)?;
        Ok(rule)
    }

    fn count(&mut self, rule: &str) -> Result<usize, ParseError> {
        let at = self.cur().clone();
        let n = self.expect_int()?;
        usize::try_from(n).map_err(|_| ParseError {
            line: at.line,
            column: at.column,
            message: format!("`@{rule}` needs a non-negative number, found {n}"),
        })
    }

    fn resolve(&self, schema: &Schema) -> Result<(), ParseError> {
        for (name, line, column) in &self.refs {
            if !schema.declares(name) {
                return Err(ParseError {
                    line: *line,
                    column: *column,
                    message: format!("unknown type `{name}`"),
                });
            }
        }

        for (union, tag, type_name, line, column) in &self.variant_refs {
            let err = |message: String| ParseError { line: *line, column: *column, message };

            let Some(object) = schema.types.get(type_name) else {
                return Err(err(if schema.unions.contains_key(type_name) {
                    // Allowing this would need a second discriminant, and
                    // nothing in the payload says which one to read first.
                    format!(
                        "`{type_name}` is a union, and a variant of `{union}` must be a `schema`"
                    )
                } else {
                    format!("unknown type `{type_name}`")
                }));
            };

            // The tag is supplied by the union and consumed while validating,
            // so a variant declaring it too would create two sources of truth
            // for one value, which could then disagree.
            if object.field(tag).is_some() {
                return Err(err(format!(
                    "`{type_name}` declares `{tag}`, which is the tag `{union}` uses; \
                     the tag belongs to the union, not to its variants"
                )));
            }
        }

        Ok(())
    }
}

static EOF: Token = Token { tok: Tok::Eof, line: 1, column: 1 };

fn builtin(name: &str) -> Option<Type> {
    let int = |width, signed| Some(Type::Int(IntType { width, signed }));
    match name {
        "String" => Some(Type::String),
        "bool" => Some(Type::Bool),
        "f64" => Some(Type::Float),
        "Date" => Some(Type::Date),
        "DateTime" => Some(Type::DateTime),
        "i8" => int(IntWidth::W8, true),
        "i16" => int(IntWidth::W16, true),
        "i32" => int(IntWidth::W32, true),
        "i64" => int(IntWidth::W64, true),
        "u8" => int(IntWidth::W8, false),
        "u16" => int(IntWidth::W16, false),
        "u32" => int(IntWidth::W32, false),
        "u64" => int(IntWidth::W64, false),
        _ => None,
    }
}

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

    const USER: &str = r#"
schema User {
  id:          u64
  name:        String            @min_len(3) @max_len(64)
  age:         u32               @range(18..=120)
  plan:        enum { free, pro, enterprise }
  tags:        [String]          @max_items(10)

  nickname:    String?           // present, may be null
  bio:         optional String   // may be absent
  avatar:      optional String?  // may be absent OR null
}
"#;

    fn user() -> ObjectType {
        let schema = parse(USER).expect("USER should parse");
        schema.get("User").cloned().expect("User should exist")
    }

    fn field(name: &str) -> Field {
        user().field(name).cloned().expect("field should exist")
    }

    #[test]
    fn parses_the_readme_schema() {
        let u = user();
        assert_eq!(u.name, "User");
        assert_eq!(u.fields.len(), 8);
        assert!(u.deny_unknown_fields);
    }

    #[test]
    fn fields_keep_declaration_order() {
        let names: Vec<_> = user().fields.iter().map(|f| f.name.clone()).collect();
        assert_eq!(
            names,
            ["id", "name", "age", "plan", "tags", "nickname", "bio", "avatar"]
        );
    }

    #[test]
    fn the_four_presence_states_round_trip() {
        assert_eq!(field("id").presence, Presence::required());
        assert_eq!(field("nickname").presence, Presence::nullable());
        assert_eq!(field("bio").presence, Presence::optional());
        assert_eq!(field("avatar").presence, Presence::optional_nullable());
    }

    #[test]
    fn integer_width_and_signedness_survive() {
        assert_eq!(
            field("id").ty,
            Type::Int(IntType { width: IntWidth::W64, signed: false })
        );
        assert_eq!(
            field("age").ty,
            Type::Int(IntType { width: IntWidth::W32, signed: false })
        );
    }

    #[test]
    fn enums_arrays_and_rules_parse() {
        assert_eq!(
            field("plan").ty,
            Type::Enum(vec!["free".into(), "pro".into(), "enterprise".into()])
        );
        assert_eq!(
            field("tags").ty,
            Type::Array { item: Box::new(Type::String), item_nullable: false }
        );
        assert_eq!(field("name").rules, vec![Rule::MinLen(3), Rule::MaxLen(64)]);
        assert_eq!(field("age").rules, vec![Rule::Range { min: 18, max: 120 }]);
        assert_eq!(field("tags").rules, vec![Rule::MaxItems(10)]);
    }

    #[test]
    fn comments_are_ignored() {
        let s = parse("// leading\nschema A { x: u8 } // trailing\n").expect("should parse");
        assert!(s.get("A").is_some());
    }

    #[test]
    fn a_type_may_refer_to_one_declared_later() {
        let s = parse("schema A { b: B }\nschema B { x: u8 }").expect("should parse");
        assert_eq!(
            s.get("A").and_then(|a| a.field("b")).map(|f| f.ty.clone()),
            Some(Type::Ref("B".into()))
        );
    }

    fn err(src: &str) -> ParseError {
        parse(src).expect_err("should not parse")
    }

    #[test]
    fn an_unknown_type_is_reported_where_it_is_used() {
        let e = err("schema A { b: Nope }");
        assert_eq!(e.message, "unknown type `Nope`");
        assert_eq!((e.line, e.column), (1, 15));
    }

    #[test]
    fn unknown_rules_are_rejected_rather_than_ignored() {
        assert_eq!(
            err("schema A { x: u8 @nope(1) }").message,
            "unknown rule `@nope`"
        );
    }

    #[test]
    fn structural_mistakes_point_at_the_right_token() {
        assert_eq!(err("schema A { x u8 }").message, "expected `:`, found `u8`");
        assert_eq!(
            err("schema A {").message,
            "expected `}`, found end of input"
        );
        assert_eq!(
            err("A { }").message,
            "expected `schema` or `union`, found `A`"
        );
    }

    #[test]
    fn duplicates_are_caught() {
        assert!(err("schema A { x: u8\n x: u8 }")
            .message
            .contains("more than once"));
        assert!(err("schema A { x: u8 }\nschema A { y: u8 }")
            .message
            .contains("more than once"));
        assert!(err("schema A { x: enum { a, a } }")
            .message
            .contains("twice"));
    }

    #[test]
    fn array_items_carry_their_own_nullability() {
        let s = parse("schema A { x: [String?]\n y: optional [u8]? }").expect("should parse");
        let a = s.get("A").expect("A should exist");

        let x = a.field("x").expect("x should exist");
        assert_eq!(
            x.ty,
            Type::Array { item: Box::new(Type::String), item_nullable: true }
        );
        // The list itself is required and non-null; only its elements may be null.
        assert_eq!(x.presence, Presence::required());

        // Element nullability and field nullability are independent.
        let y = a.field("y").expect("y should exist");
        assert_eq!(y.presence, Presence::optional_nullable());
        assert!(matches!(y.ty, Type::Array { item_nullable: false, .. }));
    }

    #[test]
    fn rule_arguments_are_checked() {
        assert!(err("schema A { x: String @min_len(-1) }")
            .message
            .contains("non-negative"));
        assert!(err("schema A { x: u8 @range(10..=1) }")
            .message
            .contains("empty"));
    }

    #[test]
    fn quoted_enum_values_allow_characters_idents_cannot_hold() {
        let s =
            parse(r#"schema A { r: enum { "us-east-1", "eu-west-2" } }"#).expect("should parse");
        assert_eq!(
            s.get("A").and_then(|a| a.field("r")).map(|f| f.ty.clone()),
            Some(Type::Enum(vec!["us-east-1".into(), "eu-west-2".into()]))
        );
    }
}