relate-parser 1.1.0

Cypher lexer, parser, and AST for the Relate graph database
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
//! CIP-100 canonical type normalization and name rendering.
//!
//! `CanonType` is the single normalized form of a Cypher type: default
//! names (CIP-100 "Type name normalization"), CIP-100's semantic
//! simplifications (flatten nested unions, drop redundant/subtype members,
//! combine NULL with NOT NULL types), and the canonical type order all
//! already applied. Two semantically equivalent types always normalize to
//! structurally equal `CanonType` trees.
//!
//! This is the shared engine behind two features:
//! - CIP-90's `valueType()`, which renders a *runtime value's* dynamic
//!   type (built bottom-up from the value's own shape by the engine, see
//!   `relate-executor`'s `value_type_name`).
//! - The normalization of a *parsed* `TypeExpr` (property type
//!   constraints, exposed via `SHOW CONSTRAINTS.propertyType`).
//!
//! Note: `TypeExpr` (CIP-86 grammar), the type predicate evaluator, and
//! this module's `type_expr_to_canon` all live in the parser crate since
//! normalization is purely a function of the parsed type grammar; nothing
//! here depends on `CypherValue`.

use std::cmp::Ordering;

use crate::ast::{PredefinedKind, TypeExpr, VectorCoordKind};

/// A fully CIP-100-normalized Cypher type.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CanonType {
    pub kind: CanonKind,
    /// Whether this node is marked NOT NULL. Always `false` for `Nothing`
    /// (normalized away, see `CanonType::predefined`) and for `Union` (the
    /// CIP-86 grammar gives closed unions no `NOT NULL` production of
    /// their own -- nullability is carried per-member instead).
    pub not_null: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CanonKind {
    Predefined(PredefinedKind),
    List(Box<CanonType>),
    /// `VECTOR<COORD NOT NULL>(DIM)` (CIP-216). Either half may be absent
    /// (`coord: None` renders as bare `VECTOR`, `dimension: None` omits the
    /// `(...)` suffix) -- both are grammatically legal `TypeExpr::Vector`
    /// shapes, kept around only so `validate_property_type` can render
    /// them back into Neo4j's own error text for the "need to define both"
    /// rejection.
    Vector {
        coord: Option<VectorCoordKind>,
        dimension: Option<i64>,
    },
    /// Two or more component types, already flattened, deduped, subtype-
    /// collapsed, and ordered -- the result of `normalize_union`.
    Union(Vec<CanonType>),
}

/// CIP-100 "Default type names" table, vector coordinate half: the
/// canonical coordinate name rendered inside `VECTOR<...>`. `Int64`/
/// `Float64` render as the plain `INTEGER`/`FLOAT` names (CIP-216 S-6: they
/// double as ordinary property type aliases), not `INTEGER64`/`FLOAT64`.
fn vector_coord_name(k: VectorCoordKind) -> &'static str {
    k.canonical_name()
}

/// Reverse of `vector_coord_name`.
fn vector_coord_kind_from_name(name: &str) -> Option<VectorCoordKind> {
    VectorCoordKind::from_canonical_name(name)
}

impl CanonType {
    /// Builds a predefined leaf, applying the "Simplify NULL NOT NULL" /
    /// "Simplify NOTHING NOT NULL" rules (both normalize to plain
    /// `NOTHING`) up front so every other rule can assume `not_null` is
    /// always `false` on `Nothing`.
    pub fn predefined(kind: PredefinedKind, not_null: bool) -> Self {
        if matches!(kind, PredefinedKind::Nothing)
            || (matches!(kind, PredefinedKind::Null) && not_null)
        {
            return CanonType {
                kind: CanonKind::Predefined(PredefinedKind::Nothing),
                not_null: false,
            };
        }
        CanonType {
            kind: CanonKind::Predefined(kind),
            not_null,
        }
    }

    pub fn list(element: CanonType, not_null: bool) -> Self {
        CanonType {
            kind: CanonKind::List(Box::new(element)),
            not_null,
        }
    }
}

/// CIP-100 "Default type names" table.
fn predefined_name(kind: PredefinedKind) -> &'static str {
    match kind {
        PredefinedKind::Nothing => "NOTHING",
        PredefinedKind::Null => "NULL",
        PredefinedKind::Any => "ANY",
        PredefinedKind::Boolean => "BOOLEAN",
        PredefinedKind::String => "STRING",
        PredefinedKind::Integer => "INTEGER",
        PredefinedKind::Float => "FLOAT",
        PredefinedKind::Date => "DATE",
        PredefinedKind::Duration => "DURATION",
        PredefinedKind::Point => "POINT",
        PredefinedKind::LocalTime => "LOCAL TIME",
        PredefinedKind::ZonedTime => "ZONED TIME",
        PredefinedKind::LocalDateTime => "LOCAL DATETIME",
        PredefinedKind::ZonedDateTime => "ZONED DATETIME",
        PredefinedKind::Node => "NODE",
        PredefinedKind::Relationship => "RELATIONSHIP",
        PredefinedKind::Path => "PATH",
        PredefinedKind::Map => "MAP",
        PredefinedKind::Uuid => "UUID",
    }
}

/// CIP-100 "General order of types" rank; lower sorts first. `List`'s rank
/// sits between `MAP` and `PATH` per the doc's constructed-types list;
/// `Vector` (CIP-216, not in the original CIP-100 table) sits directly
/// before `List` -- the only order relationship pinned down by observed
/// live-Neo4j behavior (CIP-216 01#5: `VECTOR<...>(...)` sorts before
/// `LIST<...>` in a union). `Union` is never itself ranked in practice
/// (nested unions are always flattened away, so a bare `Union` never
/// appears as a sibling of anything else needing a rank).
fn rank(kind: &CanonKind) -> u8 {
    match kind {
        CanonKind::Predefined(k) => match k {
            PredefinedKind::Nothing => 0,
            PredefinedKind::Null => 1,
            // CIP-246 amendment to the CIP-100 order: UUID sits between
            // STRING and the numerics.
            PredefinedKind::Boolean => 2,
            PredefinedKind::String => 3,
            PredefinedKind::Uuid => 4,
            PredefinedKind::Integer => 5,
            PredefinedKind::Float => 6,
            PredefinedKind::Date => 7,
            PredefinedKind::LocalTime => 8,
            PredefinedKind::ZonedTime => 9,
            PredefinedKind::LocalDateTime => 10,
            PredefinedKind::ZonedDateTime => 11,
            PredefinedKind::Duration => 12,
            PredefinedKind::Point => 13,
            PredefinedKind::Node => 14,
            PredefinedKind::Relationship => 15,
            PredefinedKind::Map => 16,
            PredefinedKind::Path => 19,
            PredefinedKind::Any => 21,
        },
        CanonKind::Vector { .. } => 17,
        CanonKind::List(_) => 18,
        CanonKind::Union(_) => 20,
    }
}

/// Renders a `CanonType` to its CIP-100 canonical string: uppercase,
/// single-space word separators, no space around `<>`, `|` surrounded by
/// exactly one space on each side.
pub fn render_canon(t: &CanonType) -> String {
    let base = match &t.kind {
        CanonKind::Predefined(k) => predefined_name(*k).to_string(),
        CanonKind::List(elem) => format!("LIST<{}>", render_canon(elem)),
        CanonKind::Vector { coord, dimension } => {
            let mut s = String::from("VECTOR");
            if let Some(c) = coord {
                s.push('<');
                s.push_str(vector_coord_name(*c));
                s.push_str(" NOT NULL>");
            }
            if let Some(d) = dimension {
                s.push('(');
                s.push_str(&d.to_string());
                s.push(')');
            }
            s
        }
        CanonKind::Union(members) => members
            .iter()
            .map(render_canon)
            .collect::<Vec<_>>()
            .join(" | "),
    };
    if t.not_null && !matches!(t.kind, CanonKind::Predefined(PredefinedKind::Nothing)) {
        format!("{base} NOT NULL")
    } else {
        base
    }
}

/// True if every value of `a` is also a value of `b` (CIP-100 "Simplify
/// encompassing types"). Used to drop redundant members from a dynamic
/// union: `NOTHING` is a subtype of everything (including `NULL`), `NULL`
/// is a subtype of every other nullable type, `ANY` is a supertype of
/// everything, `T NOT NULL` is a subtype of the nullable `T`, and
/// `LIST<T1>` is a subtype of `LIST<T2>` when `T1` is a subtype of `T2`.
fn is_subtype(a: &CanonType, b: &CanonType) -> bool {
    if a == b {
        return true;
    }
    if matches!(b.kind, CanonKind::Predefined(PredefinedKind::Any)) {
        return true;
    }
    if matches!(a.kind, CanonKind::Predefined(PredefinedKind::Nothing)) {
        return true;
    }
    if let CanonKind::Union(ma) = &a.kind {
        return ma.iter().all(|m| is_subtype(m, b));
    }
    if let CanonKind::Union(mb) = &b.kind {
        return mb.iter().any(|m| is_subtype(a, m));
    }
    if matches!(a.kind, CanonKind::Predefined(PredefinedKind::Null)) {
        return matches!(b.kind, CanonKind::Predefined(PredefinedKind::Null))
            || (!b.not_null && !matches!(b.kind, CanonKind::Predefined(PredefinedKind::Nothing)));
    }
    match (&a.kind, &b.kind) {
        (CanonKind::Predefined(ka), CanonKind::Predefined(kb)) => {
            ka == kb && (a.not_null || !b.not_null)
        }
        (CanonKind::List(ea), CanonKind::List(eb)) => {
            (a.not_null || !b.not_null) && is_subtype(ea, eb)
        }
        _ => false,
    }
}

/// Whether a member's presence in a union should trigger the "if the
/// dynamic union contains at least one nullable type, then all types are
/// nullable" rule. `NOTHING` never triggers it -- it isn't "nullable" in
/// the sense of containing the null value, it contains no values at all.
fn is_nullable_trigger(t: &CanonType) -> bool {
    !t.not_null && !matches!(t.kind, CanonKind::Predefined(PredefinedKind::Nothing))
}

/// Removes exact duplicates and members that are a strict subtype of a
/// different member still present (CIP-100 "Remove redundant types from
/// dynamic unions" + "Simplify encompassing types").
fn dedup_subtypes(items: Vec<CanonType>) -> Vec<CanonType> {
    let mut result = Vec::with_capacity(items.len());
    for (i, item) in items.iter().enumerate() {
        let redundant = items.iter().enumerate().any(|(j, other)| {
            if i == j {
                return false;
            }
            if item == other {
                // Exact duplicate: keep only the first occurrence.
                return i > j;
            }
            is_subtype(item, other)
        });
        if !redundant {
            result.push(item.clone());
        }
    }
    result
}

/// Cardinality used by the CIP-100 tie-break algorithm: a closed dynamic
/// union's own component count, or 1 for anything else (a single type is
/// a "union of one").
fn cardinality(t: &CanonType) -> usize {
    match &t.kind {
        CanonKind::Union(members) => members.len(),
        _ => 1,
    }
}

fn as_members(t: &CanonType) -> Vec<&CanonType> {
    match &t.kind {
        CanonKind::Union(members) => members.iter().collect(),
        _ => vec![t],
    }
}

/// CIP-100 total order over normalized types: general type rank first;
/// for two lists, the closed-dynamic-union tie-break algorithm (fewer
/// inner types first, then lexicographic by first differing inner type);
/// NOT NULL ordered directly before the equivalent nullable type.
fn cmp_canon(a: &CanonType, b: &CanonType) -> Ordering {
    let ra = rank(&a.kind);
    let rb = rank(&b.kind);
    if ra != rb {
        return ra.cmp(&rb);
    }
    match (&a.kind, &b.kind) {
        (CanonKind::List(ea), CanonKind::List(eb)) => {
            let ca = cardinality(ea);
            let cb = cardinality(eb);
            if ca != cb {
                return ca.cmp(&cb);
            }
            let (ma, mb) = (as_members(ea), as_members(eb));
            for (x, y) in ma.iter().zip(mb.iter()) {
                let ord = cmp_canon(x, y);
                if ord != Ordering::Equal {
                    return ord;
                }
            }
            // NOT NULL before nullable, at the list level itself.
            b.not_null.cmp(&a.not_null)
        }
        (CanonKind::Predefined(_), CanonKind::Predefined(_)) => b.not_null.cmp(&a.not_null),
        _ => Ordering::Equal,
    }
}

/// Normalizes a set of already-normalized component types into their
/// single CIP-100 normalized form: flattens nested unions, removes
/// duplicate/subtype members, applies the "combine NULL and NOT NULL
/// types" encompassing rule (a union with at least one nullable member
/// makes every member nullable), and orders what remains. Returns the
/// single member directly if only one remains once simplified (CIP-100
/// "Simplify dynamic unions with one type"), or `NOTHING` for an empty
/// input.
pub fn normalize_union(components: Vec<CanonType>) -> CanonType {
    let mut flat = Vec::with_capacity(components.len());
    for c in components {
        match c.kind {
            CanonKind::Union(members) => flat.extend(members),
            _ => flat.push(c),
        }
    }

    flat = dedup_subtypes(flat);

    if flat.iter().any(is_nullable_trigger) {
        for m in &mut flat {
            if !matches!(m.kind, CanonKind::Predefined(PredefinedKind::Nothing)) {
                m.not_null = false;
            }
        }
        flat = dedup_subtypes(flat);
    }

    flat.sort_by(cmp_canon);

    match flat.len() {
        0 => CanonType::predefined(PredefinedKind::Nothing, false),
        1 => flat.into_iter().next().unwrap(),
        _ => CanonType {
            kind: CanonKind::Union(flat),
            not_null: false,
        },
    }
}

/// Converts a parsed `TypeExpr` (CIP-86 grammar) to its CIP-100 canonical
/// form.
pub fn type_expr_to_canon(te: &TypeExpr) -> CanonType {
    match te {
        TypeExpr::Predefined { kind, not_null } => CanonType::predefined(*kind, *not_null),
        TypeExpr::List { element, not_null } => {
            CanonType::list(type_expr_to_canon(element), *not_null)
        }
        TypeExpr::PropertyValue { not_null } => property_value_canon(*not_null),
        TypeExpr::Vector {
            coord,
            dimension,
            not_null,
        } => CanonType {
            kind: CanonKind::Vector {
                coord: *coord,
                dimension: *dimension,
            },
            not_null: *not_null,
        },
        TypeExpr::Union(components) => {
            let converted: Vec<CanonType> = components.iter().map(type_expr_to_canon).collect();
            normalize_union(converted)
        }
    }
}

/// CIP-100 "Simplify ANY PROPERTY VALUE to the union of all property
/// types": the 11 property-storable scalar/temporal types plus a
/// `LIST<T NOT NULL>` of each (list elements are always NOT NULL --
/// property-storable lists can never contain a null element).
fn property_value_canon(not_null: bool) -> CanonType {
    const SCALARS: [PredefinedKind; 11] = [
        PredefinedKind::Boolean,
        PredefinedKind::String,
        PredefinedKind::Integer,
        PredefinedKind::Float,
        PredefinedKind::Date,
        PredefinedKind::LocalTime,
        PredefinedKind::ZonedTime,
        PredefinedKind::LocalDateTime,
        PredefinedKind::ZonedDateTime,
        PredefinedKind::Duration,
        PredefinedKind::Point,
    ];
    let mut members: Vec<CanonType> = SCALARS
        .iter()
        .map(|k| CanonType::predefined(*k, not_null))
        .collect();
    for k in SCALARS {
        members.push(CanonType::list(CanonType::predefined(k, true), not_null));
    }
    normalize_union(members)
}

/// Whether `k` is one of the composite/unbounded kinds CIP-85 disallows as
/// a property type (or list inner element type): `NOTHING`/`NULL`/`ANY`/
/// `NODE`/`RELATIONSHIP`/`PATH`/`MAP`. The 11 scalar/temporal property-
/// storable kinds (plus their `LIST<...>` forms) are the only ones left.
fn is_disallowed_property_kind(k: PredefinedKind) -> bool {
    matches!(
        k,
        PredefinedKind::Nothing
            | PredefinedKind::Null
            | PredefinedKind::Any
            | PredefinedKind::Node
            | PredefinedKind::Relationship
            | PredefinedKind::Path
            | PredefinedKind::Map
    )
}

/// Validates a `LIST<...>` inner element type (CIP-85: "lists cannot have
/// a union/list/nullable inner type", NOT NULL is mandatory on the
/// element). Errors are rendered against `list_canon` (the whole
/// `LIST<...>` type), matching Neo4j's error shape. `pub` (not just used by
/// `validate_property_type`) because CIP-178/181's graph-type property
/// types share this exact restriction while allowing top-level `NOT NULL`
/// (which `validate_property_type` itself forbids -- that rule is specific
/// to standalone property type *constraints*, not a node/edge type's own
/// declared properties).
pub fn validate_list_element(list_canon: &CanonType, elem: &CanonType) -> Result<(), String> {
    if matches!(elem.kind, CanonKind::Union(_)) {
        return Err(format!(
            "Invalid property type `{}`. Lists cannot have a union of types as an inner type.",
            render_canon(list_canon)
        ));
    }
    if matches!(elem.kind, CanonKind::List(_)) {
        return Err(format!(
            "Invalid property type `{}`. Lists cannot have a list as an inner type.",
            render_canon(list_canon)
        ));
    }
    // CIP-216 Negative 4: a list of vectors is not a supported property
    // type, regardless of the vector's own NOT NULL/coordinate/dimension.
    if matches!(elem.kind, CanonKind::Vector { .. }) {
        return Err(format!(
            "{} is not supported in property type constraints. A list in this position \
             cannot have VECTOR as an inner type.",
            render_canon(list_canon)
        ));
    }
    if !elem.not_null {
        return Err(format!(
            "Invalid property type `{}`. Lists cannot have a nullable inner type.",
            render_canon(list_canon)
        ));
    }
    if let CanonKind::Predefined(k) = &elem.kind {
        if is_disallowed_property_kind(*k) {
            return Err(format!(
                "{} is not supported in property type constraints.",
                render_canon(list_canon)
            ));
        }
    }
    Ok(())
}

/// The `<value type>` expression form of [`validate_property_type`], for
/// callers that still hold the parsed type: `PROPERTY VALUE` canonicalizes
/// into an open union of every storable scalar, so the canonical form alone
/// cannot tell it apart from an explicitly written union -- and Neo4j
/// rejects it as a property type constraint while accepting the union
/// (live corpus: `Create*PropertyTypeConstraintCommands`). One entry point
/// for both call sites, so the expression-level rule cannot drift from the
/// canonical one.
pub fn validate_property_type_expr(te: &TypeExpr) -> Result<(), String> {
    if matches!(te, TypeExpr::PropertyValue { .. }) {
        return Err("PROPERTY VALUE is not supported in property type constraints.".to_string());
    }
    validate_property_type(&type_expr_to_canon(te))
}

/// Validates a normalized type against Relate's property type
/// constraint restrictions (CIP-85): no top-level `NOT NULL` (on a scalar
/// or on the list itself -- Neo4j 2026.05 does not implement `TYPE NOT
/// NULL` for property type constraints), no `NOTHING`/`NULL`/`ANY`/`NODE`/
/// `RELATIONSHIP`/`PATH`/`MAP`, and -- inside a `LIST<...>` -- no union,
/// no nested list, and no nullable inner type (NOT NULL is mandatory
/// there).
pub fn validate_property_type(canon: &CanonType) -> Result<(), String> {
    if canon.not_null {
        return Err(format!(
            "{} is not supported in property type constraints.",
            render_canon(canon)
        ));
    }
    match &canon.kind {
        CanonKind::Predefined(k) => {
            if is_disallowed_property_kind(*k) {
                Err(format!(
                    "{} is not supported in property type constraints.",
                    render_canon(canon)
                ))
            } else {
                Ok(())
            }
        }
        CanonKind::List(elem) => validate_list_element(canon, elem),
        // CIP-216 Negative 1/2: both coordinate type and dimension are
        // mandatory for a vector property type constraint; either omitted
        // makes the whole constraint creation fail (the type itself
        // parses fine -- this is a runtime rejection, not a syntax error).
        CanonKind::Vector { coord, dimension } => {
            if coord.is_none() || dimension.is_none() {
                Err(format!(
                    "{} is not supported in property type constraints. Property type \
                     constraints for vectors need to define both coordinate type and \
                     dimension.",
                    render_canon(canon)
                ))
            } else {
                Ok(())
            }
        }
        CanonKind::Union(members) => {
            for m in members {
                validate_property_type(m)?;
            }
            Ok(())
        }
    }
}

/// Reverse of `render_canon` for a property type constraint's stored
/// canonical string: reconstructs a `TypeExpr` a caller can feed to a
/// value-type matcher. Only understands the shape `render_canon` actually
/// produces for a `validate_property_type`-accepted type (predefined
/// names, `LIST<...>`, a trailing ` NOT NULL`, and `" | "`-separated
/// unions) -- not general Cypher type syntax. Used to re-derive a runtime
/// type matcher for a constraint loaded back from storage (CIP-85
/// enforcement), where only the rendered string persists. Returns `None`
/// if `s` doesn't match that shape (should not happen for a string this
/// module itself produced).
pub fn parse_canon_type_str(s: &str) -> Option<TypeExpr> {
    if s.contains(" | ") {
        let members = s
            .split(" | ")
            .map(parse_single_canon_type_str)
            .collect::<Option<Vec<_>>>()?;
        return Some(TypeExpr::Union(members));
    }
    parse_single_canon_type_str(s)
}

fn parse_single_canon_type_str(s: &str) -> Option<TypeExpr> {
    let (base, not_null) = match s.strip_suffix(" NOT NULL") {
        Some(b) => (b, true),
        None => (s, false),
    };
    if let Some(inner) = base.strip_prefix("LIST<").and_then(|x| x.strip_suffix('>')) {
        return Some(TypeExpr::List {
            element: Box::new(parse_single_canon_type_str(inner)?),
            not_null,
        });
    }
    if let Some(rest) = base.strip_prefix("VECTOR") {
        let (coord, dimension) = parse_vector_canon_tail(rest)?;
        return Some(TypeExpr::Vector {
            coord,
            dimension,
            not_null,
        });
    }
    Some(TypeExpr::Predefined {
        kind: predefined_kind_from_name(base)?,
        not_null,
    })
}

/// Parses the part of a rendered vector type string after the `VECTOR`
/// prefix (and after any outer ` NOT NULL` has already been stripped by
/// the caller): an optional `<COORD NOT NULL>` and an optional `(DIM)`,
/// in that order, and nothing else. Reverse of the `CanonKind::Vector`
/// branch of `render_canon`.
fn parse_vector_canon_tail(rest: &str) -> Option<(Option<VectorCoordKind>, Option<i64>)> {
    let mut s = rest;
    let mut coord = None;
    if let Some(after_lt) = s.strip_prefix('<') {
        let end = after_lt.find('>')?;
        let inner = after_lt[..end].strip_suffix(" NOT NULL")?;
        coord = Some(vector_coord_kind_from_name(inner)?);
        s = &after_lt[end + 1..];
    }
    let dimension = if let Some(after_paren) = s.strip_prefix('(') {
        let end = after_paren.find(')')?;
        let dim = after_paren[..end].parse::<i64>().ok()?;
        s = &after_paren[end + 1..];
        Some(dim)
    } else {
        None
    };
    if !s.is_empty() {
        return None;
    }
    Some((coord, dimension))
}

/// Reverse of `predefined_name`.
fn predefined_kind_from_name(name: &str) -> Option<PredefinedKind> {
    Some(match name {
        "NOTHING" => PredefinedKind::Nothing,
        "NULL" => PredefinedKind::Null,
        "ANY" => PredefinedKind::Any,
        "BOOLEAN" => PredefinedKind::Boolean,
        "STRING" => PredefinedKind::String,
        "INTEGER" => PredefinedKind::Integer,
        "FLOAT" => PredefinedKind::Float,
        "UUID" => PredefinedKind::Uuid,
        "DATE" => PredefinedKind::Date,
        "DURATION" => PredefinedKind::Duration,
        "POINT" => PredefinedKind::Point,
        "LOCAL TIME" => PredefinedKind::LocalTime,
        "ZONED TIME" => PredefinedKind::ZonedTime,
        "LOCAL DATETIME" => PredefinedKind::LocalDateTime,
        "ZONED DATETIME" => PredefinedKind::ZonedDateTime,
        "NODE" => PredefinedKind::Node,
        "RELATIONSHIP" => PredefinedKind::Relationship,
        "PATH" => PredefinedKind::Path,
        "MAP" => PredefinedKind::Map,
        _ => return None,
    })
}

#[cfg(test)]
mod tests;