nanograph 0.8.1

Embedded typed property graph database. Schema-as-code, compile-time validated, Arrow-native.
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
use pest::Parser;
use pest::error::InputLocation;
use pest_derive::Parser;

use crate::error::{NanoError, ParseDiagnostic, Result, SourceSpan};
use crate::types::{PropType, ScalarType};

use super::ast::*;

#[derive(Parser)]
#[grammar = "schema/schema.pest"]
struct SchemaParser;

pub fn parse_schema(input: &str) -> Result<SchemaFile> {
    parse_schema_diagnostic(input).map_err(|e| NanoError::Parse(e.to_string()))
}

pub fn parse_schema_diagnostic(input: &str) -> std::result::Result<SchemaFile, ParseDiagnostic> {
    let pairs = SchemaParser::parse(Rule::schema_file, input).map_err(pest_error_to_diagnostic)?;

    let mut declarations = Vec::new();
    for pair in pairs {
        match pair.as_rule() {
            Rule::schema_file => {
                for inner in pair.into_inner() {
                    if let Rule::schema_decl = inner.as_rule() {
                        declarations
                            .push(parse_schema_decl(inner).map_err(nano_error_to_diagnostic)?);
                    }
                }
            }
            _ => {}
        }
    }
    let schema = SchemaFile { declarations };
    validate_schema_annotations(&schema).map_err(nano_error_to_diagnostic)?;
    Ok(schema)
}

fn pest_error_to_diagnostic(err: pest::error::Error<Rule>) -> ParseDiagnostic {
    let span = match err.location {
        InputLocation::Pos(pos) => Some(SourceSpan::new(pos, pos)),
        InputLocation::Span((start, end)) => Some(SourceSpan::new(start, end)),
    };
    ParseDiagnostic::new(err.to_string(), span)
}

fn nano_error_to_diagnostic(err: NanoError) -> ParseDiagnostic {
    ParseDiagnostic::new(err.to_string(), None)
}

fn parse_schema_decl(pair: pest::iterators::Pair<Rule>) -> Result<SchemaDecl> {
    let inner = pair.into_inner().next().unwrap();
    match inner.as_rule() {
        Rule::node_decl => Ok(SchemaDecl::Node(parse_node_decl(inner)?)),
        Rule::edge_decl => Ok(SchemaDecl::Edge(parse_edge_decl(inner)?)),
        _ => Err(NanoError::Parse(format!(
            "unexpected rule: {:?}",
            inner.as_rule()
        ))),
    }
}

fn parse_node_decl(pair: pest::iterators::Pair<Rule>) -> Result<NodeDecl> {
    let mut inner = pair.into_inner();
    let name = inner.next().unwrap().as_str().to_string();

    let mut annotations = Vec::new();
    let mut parent = None;
    let mut properties = Vec::new();

    for item in inner {
        match item.as_rule() {
            Rule::annotation => {
                annotations.push(parse_annotation(item)?);
            }
            Rule::type_name => {
                parent = Some(item.as_str().to_string());
            }
            Rule::prop_decl => {
                properties.push(parse_prop_decl(item)?);
            }
            _ => {}
        }
    }

    Ok(NodeDecl {
        name,
        annotations,
        parent,
        properties,
    })
}

fn parse_edge_decl(pair: pest::iterators::Pair<Rule>) -> Result<EdgeDecl> {
    let mut inner = pair.into_inner();
    let name = inner.next().unwrap().as_str().to_string();
    let from_type = inner.next().unwrap().as_str().to_string();
    let to_type = inner.next().unwrap().as_str().to_string();

    let mut annotations = Vec::new();
    let mut properties = Vec::new();
    for item in inner {
        match item.as_rule() {
            Rule::annotation => annotations.push(parse_annotation(item)?),
            Rule::prop_decl => properties.push(parse_prop_decl(item)?),
            _ => {}
        }
    }

    Ok(EdgeDecl {
        name,
        from_type,
        to_type,
        annotations,
        properties,
    })
}

fn parse_prop_decl(pair: pest::iterators::Pair<Rule>) -> Result<PropDecl> {
    let mut inner = pair.into_inner();
    let name = inner.next().unwrap().as_str().to_string();
    let type_ref = inner.next().unwrap();
    let prop_type = parse_type_ref(type_ref)?;

    let mut annotations = Vec::new();
    for item in inner {
        if let Rule::annotation = item.as_rule() {
            annotations.push(parse_annotation(item)?);
        }
    }

    Ok(PropDecl {
        name,
        prop_type,
        annotations,
    })
}

fn parse_type_ref(pair: pest::iterators::Pair<Rule>) -> Result<PropType> {
    let text = pair.as_str();
    let nullable = text.ends_with('?');

    let mut inner = pair
        .into_inner()
        .next()
        .ok_or_else(|| NanoError::Parse("type reference is missing core type".to_string()))?;
    if inner.as_rule() == Rule::core_type {
        inner = inner
            .into_inner()
            .next()
            .ok_or_else(|| NanoError::Parse("type reference is missing core type".to_string()))?;
    }

    match inner.as_rule() {
        Rule::base_type => {
            let scalar = ScalarType::from_str_name(inner.as_str())
                .ok_or_else(|| NanoError::Parse(format!("unknown type: {}", inner.as_str())))?;
            Ok(PropType::scalar(scalar, nullable))
        }
        Rule::vector_type => {
            let dim_text = inner
                .into_inner()
                .next()
                .ok_or_else(|| NanoError::Parse("Vector type missing dimension".to_string()))?
                .as_str();
            let dim = dim_text
                .parse::<u32>()
                .map_err(|e| NanoError::Parse(format!("invalid Vector dimension: {}", e)))?;
            if dim == 0 {
                return Err(NanoError::Parse(
                    "Vector dimension must be greater than zero".to_string(),
                ));
            }
            if dim > i32::MAX as u32 {
                return Err(NanoError::Parse(format!(
                    "Vector dimension {} exceeds maximum supported {}",
                    dim,
                    i32::MAX
                )));
            }
            Ok(PropType::scalar(ScalarType::Vector(dim), nullable))
        }
        Rule::list_type => {
            let element = inner
                .into_inner()
                .next()
                .ok_or_else(|| NanoError::Parse("list type missing element type".to_string()))?;
            let scalar = ScalarType::from_str_name(element.as_str()).ok_or_else(|| {
                NanoError::Parse(format!("unknown list element type: {}", element.as_str()))
            })?;
            Ok(PropType::list_of(scalar, nullable))
        }
        Rule::enum_type => {
            let mut values = Vec::new();
            for value in inner.into_inner() {
                if value.as_rule() == Rule::enum_value {
                    values.push(value.as_str().to_string());
                }
            }
            if values.is_empty() {
                return Err(NanoError::Parse(
                    "enum type must include at least one value".to_string(),
                ));
            }
            let mut dedup = values.clone();
            dedup.sort();
            dedup.dedup();
            if dedup.len() != values.len() {
                return Err(NanoError::Parse(
                    "enum type cannot include duplicate values".to_string(),
                ));
            }
            Ok(PropType::enum_type(values, nullable))
        }
        other => Err(NanoError::Parse(format!(
            "unexpected type rule: {:?}",
            other
        ))),
    }
}

fn parse_annotation(pair: pest::iterators::Pair<Rule>) -> Result<Annotation> {
    let mut inner = pair.into_inner();
    let name = inner.next().unwrap().as_str().to_string();
    let value = inner.next().map(|p| {
        let s = p.as_str();
        s.strip_prefix('"')
            .and_then(|inner| inner.strip_suffix('"'))
            .unwrap_or(s)
            .to_string()
    });

    Ok(Annotation { name, value })
}

fn validate_schema_annotations(schema: &SchemaFile) -> Result<()> {
    for decl in &schema.declarations {
        match decl {
            SchemaDecl::Node(node) => {
                for ann in &node.annotations {
                    if ann.name == "key"
                        || ann.name == "unique"
                        || ann.name == "index"
                        || ann.name == "embed"
                    {
                        return Err(NanoError::Parse(format!(
                            "@{} is only supported on node properties (node {})",
                            ann.name, node.name
                        )));
                    }
                }

                let mut key_count = 0usize;
                for prop in &node.properties {
                    let mut key_seen = false;
                    let mut unique_seen = false;
                    let mut index_seen = false;
                    let mut embed_seen = false;
                    let is_vector = matches!(prop.prop_type.scalar, ScalarType::Vector(_));
                    for ann in &prop.annotations {
                        if prop.prop_type.list
                            && (ann.name == "key"
                                || ann.name == "unique"
                                || ann.name == "index"
                                || ann.name == "embed")
                        {
                            return Err(NanoError::Parse(format!(
                                "@{} is not supported on list property {}.{}",
                                ann.name, node.name, prop.name
                            )));
                        }
                        if is_vector && (ann.name == "key" || ann.name == "unique") {
                            return Err(NanoError::Parse(format!(
                                "@{} is not supported on vector property {}.{}",
                                ann.name, node.name, prop.name
                            )));
                        }
                        if ann.name == "key" {
                            if ann.value.is_some() {
                                return Err(NanoError::Parse(format!(
                                    "@key on {}.{} does not accept a value",
                                    node.name, prop.name
                                )));
                            }
                            if key_seen {
                                return Err(NanoError::Parse(format!(
                                    "property {}.{} declares @key multiple times",
                                    node.name, prop.name
                                )));
                            }
                            key_seen = true;
                            key_count += 1;
                        } else if ann.name == "unique" {
                            if ann.value.is_some() {
                                return Err(NanoError::Parse(format!(
                                    "@unique on {}.{} does not accept a value",
                                    node.name, prop.name
                                )));
                            }
                            if unique_seen {
                                return Err(NanoError::Parse(format!(
                                    "property {}.{} declares @unique multiple times",
                                    node.name, prop.name
                                )));
                            }
                            unique_seen = true;
                        } else if ann.name == "index" {
                            if ann.value.is_some() {
                                return Err(NanoError::Parse(format!(
                                    "@index on {}.{} does not accept a value",
                                    node.name, prop.name
                                )));
                            }
                            if index_seen {
                                return Err(NanoError::Parse(format!(
                                    "property {}.{} declares @index multiple times",
                                    node.name, prop.name
                                )));
                            }
                            index_seen = true;
                        } else if ann.name == "embed" {
                            if embed_seen {
                                return Err(NanoError::Parse(format!(
                                    "property {}.{} declares @embed multiple times",
                                    node.name, prop.name
                                )));
                            }
                            embed_seen = true;

                            if !is_vector {
                                return Err(NanoError::Parse(format!(
                                    "@embed is only supported on vector properties ({}.{})",
                                    node.name, prop.name
                                )));
                            }

                            let source_prop = ann.value.as_deref().ok_or_else(|| {
                                NanoError::Parse(format!(
                                    "@embed on {}.{} requires a source property name",
                                    node.name, prop.name
                                ))
                            })?;
                            if source_prop.trim().is_empty() {
                                return Err(NanoError::Parse(format!(
                                    "@embed on {}.{} requires a non-empty source property name",
                                    node.name, prop.name
                                )));
                            }

                            let source_decl = node
                                .properties
                                .iter()
                                .find(|p| p.name == source_prop)
                                .ok_or_else(|| {
                                    NanoError::Parse(format!(
                                        "@embed on {}.{} references unknown source property {}",
                                        node.name, prop.name, source_prop
                                    ))
                                })?;
                            if source_decl.prop_type.list
                                || source_decl.prop_type.scalar != ScalarType::String
                            {
                                return Err(NanoError::Parse(format!(
                                    "@embed source property {}.{} must be String",
                                    node.name, source_prop
                                )));
                            }
                        }
                    }
                }

                if key_count > 1 {
                    return Err(NanoError::Parse(format!(
                        "node type {} has multiple @key properties; only one is currently supported",
                        node.name
                    )));
                }
            }
            SchemaDecl::Edge(edge) => {
                for ann in &edge.annotations {
                    if ann.name == "key"
                        || ann.name == "unique"
                        || ann.name == "index"
                        || ann.name == "embed"
                    {
                        return Err(NanoError::Parse(format!(
                            "@{} is not supported on edges (edge {})",
                            ann.name, edge.name
                        )));
                    }
                }

                for prop in &edge.properties {
                    for ann in &prop.annotations {
                        if ann.name == "key"
                            || ann.name == "unique"
                            || ann.name == "index"
                            || ann.name == "embed"
                        {
                            return Err(NanoError::Parse(format!(
                                "@{} is not supported on edge properties (edge {}.{})",
                                ann.name, edge.name, prop.name
                            )));
                        }
                    }
                }
            }
        }
    }

    Ok(())
}

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

    #[test]
    fn test_parse_basic_schema() {
        let input = r#"
node Person {
    name: String
    age: I32?
}

node Company {
    name: String
}

edge Knows: Person -> Person {
    since: Date?
}

edge WorksAt: Person -> Company {
    title: String?
}
"#;
        let schema = parse_schema(input).unwrap();
        assert_eq!(schema.declarations.len(), 4);

        // Check Person node
        match &schema.declarations[0] {
            SchemaDecl::Node(n) => {
                assert_eq!(n.name, "Person");
                assert!(n.annotations.is_empty());
                assert!(n.parent.is_none());
                assert_eq!(n.properties.len(), 2);
                assert_eq!(n.properties[0].name, "name");
                assert!(!n.properties[0].prop_type.nullable);
                assert_eq!(n.properties[1].name, "age");
                assert!(n.properties[1].prop_type.nullable);
            }
            _ => panic!("expected Node"),
        }

        // Check Knows edge
        match &schema.declarations[2] {
            SchemaDecl::Edge(e) => {
                assert_eq!(e.name, "Knows");
                assert_eq!(e.from_type, "Person");
                assert_eq!(e.to_type, "Person");
                assert!(e.annotations.is_empty());
                assert_eq!(e.properties.len(), 1);
            }
            _ => panic!("expected Edge"),
        }
    }

    #[test]
    fn test_parse_inheritance() {
        let input = r#"
node Person {
    name: String
}
node Employee : Person {
    employee_id: String
}
"#;
        let schema = parse_schema(input).unwrap();
        match &schema.declarations[1] {
            SchemaDecl::Node(n) => {
                assert_eq!(n.name, "Employee");
                assert_eq!(n.parent.as_deref(), Some("Person"));
            }
            _ => panic!("expected Node"),
        }
    }

    #[test]
    fn test_parse_annotation() {
        let input = r#"
node Person {
    name: String @unique
    id: U64 @key
    handle: String @index
}
"#;
        let schema = parse_schema(input).unwrap();
        match &schema.declarations[0] {
            SchemaDecl::Node(n) => {
                assert_eq!(n.properties[0].annotations.len(), 1);
                assert_eq!(n.properties[0].annotations[0].name, "unique");
                assert_eq!(n.properties[1].annotations[0].name, "key");
                assert_eq!(n.properties[2].annotations[0].name, "index");
            }
            _ => panic!("expected Node"),
        }
    }

    #[test]
    fn test_parse_embed_annotation_identifier_arg() {
        let input = r#"
node Doc {
    title: String
    embedding: Vector(3) @embed(title)
}
"#;
        let schema = parse_schema(input).unwrap();
        match &schema.declarations[0] {
            SchemaDecl::Node(n) => {
                assert_eq!(n.properties[1].annotations.len(), 1);
                assert_eq!(n.properties[1].annotations[0].name, "embed");
                assert_eq!(
                    n.properties[1].annotations[0].value.as_deref(),
                    Some("title")
                );
            }
            _ => panic!("expected Node"),
        }
    }

    #[test]
    fn test_parse_edge_no_body() {
        let input = "edge WorksAt: Person -> Company\n";
        let schema = parse_schema(input).unwrap();
        match &schema.declarations[0] {
            SchemaDecl::Edge(e) => {
                assert_eq!(e.name, "WorksAt");
                assert!(e.annotations.is_empty());
                assert!(e.properties.is_empty());
            }
            _ => panic!("expected Edge"),
        }
    }

    #[test]
    fn test_parse_type_rename_annotation() {
        let input = r#"
node Account @rename_from("User") {
    full_name: String @rename_from("name")
}

edge ConnectedTo: Account -> Account @rename_from("Knows")
"#;
        let schema = parse_schema(input).unwrap();
        match &schema.declarations[0] {
            SchemaDecl::Node(n) => {
                assert_eq!(n.name, "Account");
                assert_eq!(n.annotations.len(), 1);
                assert_eq!(n.annotations[0].name, "rename_from");
                assert_eq!(n.annotations[0].value.as_deref(), Some("User"));
                assert_eq!(n.properties[0].annotations[0].name, "rename_from");
                assert_eq!(
                    n.properties[0].annotations[0].value.as_deref(),
                    Some("name")
                );
            }
            _ => panic!("expected Node"),
        }
        match &schema.declarations[1] {
            SchemaDecl::Edge(e) => {
                assert_eq!(e.name, "ConnectedTo");
                assert_eq!(e.annotations.len(), 1);
                assert_eq!(e.annotations[0].name, "rename_from");
                assert_eq!(e.annotations[0].value.as_deref(), Some("Knows"));
            }
            _ => panic!("expected Edge"),
        }
    }

    #[test]
    fn test_reject_multiple_node_keys() {
        let input = r#"
node Person {
    id: U64 @key
    ext_id: String @key
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(err.to_string().contains("multiple @key properties"));
    }

    #[test]
    fn test_reject_unique_with_value() {
        let input = r#"
node Person {
    email: String @unique("x")
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(err.to_string().contains("@unique"));
        assert!(err.to_string().contains("does not accept a value"));
    }

    #[test]
    fn test_reject_index_with_value() {
        let input = r#"
node Person {
    email: String @index("x")
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(err.to_string().contains("@index"));
        assert!(err.to_string().contains("does not accept a value"));
    }

    #[test]
    fn test_reject_unique_on_node_annotation() {
        let input = r#"
node Person @unique {
    email: String
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(
            err.to_string()
                .contains("only supported on node properties")
        );
    }

    #[test]
    fn test_reject_index_on_node_annotation() {
        let input = r#"
node Person @index {
    email: String
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(
            err.to_string()
                .contains("only supported on node properties")
        );
    }

    #[test]
    fn test_reject_unique_on_edge_property() {
        let input = r#"
node Person { name: String }
edge Knows: Person -> Person {
    weight: I32 @unique
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(err.to_string().contains("edge properties"));
    }

    #[test]
    fn test_reject_index_on_edge_property() {
        let input = r#"
node Person { name: String }
edge Knows: Person -> Person {
    weight: I32 @index
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(err.to_string().contains("edge properties"));
    }

    #[test]
    fn test_reject_embed_without_source_property() {
        let input = r#"
node Doc {
    title: String
    embedding: Vector(3) @embed
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(err.to_string().contains("requires a source property name"));
    }

    #[test]
    fn test_reject_embed_on_non_vector_property() {
        let input = r#"
node Doc {
    title: String @embed(title)
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(
            err.to_string()
                .contains("only supported on vector properties")
        );
    }

    #[test]
    fn test_reject_embed_unknown_source_property() {
        let input = r#"
node Doc {
    title: String
    embedding: Vector(3) @embed(body)
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(
            err.to_string()
                .contains("references unknown source property")
        );
    }

    #[test]
    fn test_reject_embed_source_not_string() {
        let input = r#"
node Doc {
    body: I32
    embedding: Vector(3) @embed(body)
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(err.to_string().contains("must be String"));
    }

    #[test]
    fn test_reject_embed_on_edge_property() {
        let input = r#"
node Doc { title: String }
edge Linked: Doc -> Doc {
    embedding: Vector(3) @embed(title)
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(err.to_string().contains("edge properties"));
    }

    #[test]
    fn test_parse_enum_and_list_types() {
        let input = r#"
node Ticket {
    status: enum(open, closed, blocked)
    tags: [String]
}
"#;
        let schema = parse_schema(input).unwrap();
        match &schema.declarations[0] {
            SchemaDecl::Node(n) => {
                let status = &n.properties[0].prop_type;
                assert!(status.is_enum());
                assert!(!status.list);
                assert_eq!(
                    status.enum_values.as_ref().unwrap(),
                    &vec![
                        "blocked".to_string(),
                        "closed".to_string(),
                        "open".to_string()
                    ]
                );

                let tags = &n.properties[1].prop_type;
                assert!(tags.list);
                assert!(!tags.is_enum());
                assert_eq!(tags.scalar, ScalarType::String);
            }
            _ => panic!("expected Node"),
        }
    }

    #[test]
    fn test_reject_duplicate_enum_values() {
        let input = r#"
node Ticket {
    status: enum(open, closed, open)
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(err.to_string().contains("duplicate values"));
    }

    #[test]
    fn test_reject_key_on_list_property() {
        let input = r#"
node Ticket {
    tags: [String] @key
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(err.to_string().contains("list property"));
    }

    #[test]
    fn test_parse_vector_type() {
        let input = r#"
node Doc {
    embedding: Vector(3)
}
"#;
        let schema = parse_schema(input).unwrap();
        match &schema.declarations[0] {
            SchemaDecl::Node(n) => match n.properties[0].prop_type.scalar {
                ScalarType::Vector(dim) => assert_eq!(dim, 3),
                other => panic!("expected vector type, got {:?}", other),
            },
            _ => panic!("expected node"),
        }
    }

    #[test]
    fn test_reject_zero_vector_dimension() {
        let input = r#"
node Doc {
    embedding: Vector(0)
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(err.to_string().contains("Vector dimension"));
    }

    #[test]
    fn test_reject_vector_dimension_larger_than_arrow_bound() {
        let input = r#"
node Doc {
    embedding: Vector(2147483648)
}
"#;
        let err = parse_schema(input).unwrap_err();
        assert!(err.to_string().contains("exceeds maximum supported"));
    }

    #[test]
    fn test_parse_error() {
        let input = "node { }"; // missing type name
        assert!(parse_schema(input).is_err());
    }

    #[test]
    fn test_parse_error_diagnostic_has_span() {
        let input = "node { }";
        let err = parse_schema_diagnostic(input).unwrap_err();
        assert!(err.span.is_some());
    }
}