relate_model/identifier.rs
1//! Shape-only Cypher identifier quoting for display text.
2//!
3//! This is the lowest shared home of the policy: the parser's generated
4//! Cypher and the engine's value rendering both consult it rather than each
5//! spelling the backtick escape themselves. It sits in `relate-model`
6//! because that is the only crate both `relate-parser` and
7//! `relate-core` depend on.
8
9/// True when `name` has the plain ASCII identifier shape
10/// `[A-Za-z_][A-Za-z0-9_]*` — the shape-only half of the quoting policy.
11///
12/// ASCII-only by design, and deliberately narrower than the lexer: unquoted
13/// identifiers may use the Unicode 13.0.0 classes (so `café` parses bare,
14/// ADR-0182), but rendered text quotes every non-ASCII name. Backticks keep
15/// rendered Cypher ASCII — readable by older Relate versions that
16/// predate Unicode identifiers, and independent of the pinned Unicode table
17/// version — and the quoted form round-trips exactly in every name position,
18/// so the narrower gate is a rendering choice, not a dialect claim.
19pub fn has_plain_identifier_shape(name: &str) -> bool {
20 let mut chars = name.chars();
21 chars
22 .next()
23 .is_some_and(|c| c.is_ascii() && (c == '_' || c.is_ascii_alphabetic()))
24 && chars.all(|c| c.is_ascii() && (c == '_' || c.is_ascii_alphanumeric()))
25}
26
27/// Renders `name` in the always-backtick-quoted form: wrapped in backticks
28/// with every embedded backtick doubled (the Cypher escape rule), whatever
29/// [`has_plain_identifier_shape`] would say. The one spelling of the quoting
30/// escape: every caller that needs a forced-quoted name calls it instead of
31/// re-spelling the `format!`/`replace`.
32pub fn quote_identifier_always(name: &str) -> String {
33 format!("`{}`", name.replace('`', "``"))
34}
35
36/// Renders `name` for display text: bare when [`has_plain_identifier_shape`]
37/// holds, otherwise through [`quote_identifier_always`].
38///
39/// Display text is not re-parsed as a statement, so it owes no
40/// keyword-position guarantee: a keyword spelling such as `when` stays bare
41/// here, while generated Cypher that must round-trip as a statement uses the
42/// parser's keyword-aware `token::quote_identifier` instead.
43pub fn quote_identifier_shaped(name: &str) -> String {
44 if has_plain_identifier_shape(name) {
45 name.to_string()
46 } else {
47 quote_identifier_always(name)
48 }
49}
50
51/// Renders a parameter reference `$name` for text that must stay
52/// collision-free across every spelling (plan-cache templates, expression
53/// text, plan-node details): the `$` sigil plus the shaped name — bare
54/// when [`has_plain_identifier_shape`] holds, otherwise the
55/// backtick-quoted form. The domain is names: the empty string is the
56/// `$(expr)` dynamic-opener sentinel, never a parameter's name — callers
57/// bail on the sentinel before rendering (asserted in debug builds). The
58/// gate is the one identifier shape `[A-Za-z_][A-Za-z0-9_]*`, deliberately
59/// reused here and therefore narrower than the parameter-name grammar
60/// (which also admits a digit start, ADR-0182): a digit-start name
61/// over-quotes to `$`1x`` — a conservative render that still round-trips
62/// — and a name that is not identifier-shaped at all (a space, an
63/// embedded backtick) must quote to re-parse as itself. Two different
64/// names never render alike, and one name always re-parses as itself
65/// (LABS-1602's backtick-quoted names).
66pub fn parameter_reference(name: &str) -> String {
67 debug_assert!(
68 !name.is_empty(),
69 "the empty literal is the `$(expr)` opener sentinel, never a name"
70 );
71 format!("${}", quote_identifier_shaped(name))
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77
78 #[test]
79 fn parameter_reference_is_shaped_and_round_trips() {
80 // The canonical `$name` render: bare for plain-shaped names, the
81 // backtick-quoted form otherwise — so one name always re-parses as
82 // itself (the backtick scan decodes the doubled pair) and two
83 // different names never render alike.
84 assert_eq!(parameter_reference("uid"), "$uid");
85 assert_eq!(parameter_reference("p 1"), "$`p 1`");
86 assert_eq!(parameter_reference("1x"), "$`1x`");
87 assert_eq!(parameter_reference("a`b"), "$`a``b`");
88 assert_ne!(parameter_reference("p 1"), parameter_reference("p"));
89 }
90
91 #[test]
92 fn plain_identifier_shape_is_ascii_only() {
93 for bare in ["a", "Person", "_x", "a1", "ship_date"] {
94 assert!(has_plain_identifier_shape(bare), "{bare}");
95 }
96 for quoted in ["", "1a", "a b", "a-b", "café", "we`ird", "a.b"] {
97 assert!(!has_plain_identifier_shape(quoted), "{quoted}");
98 }
99 }
100
101 #[test]
102 fn shaped_quoting_leaves_plain_names_bare_and_escapes_the_rest() {
103 assert_eq!(quote_identifier_shaped("Person"), "Person");
104 assert_eq!(quote_identifier_shaped("first name"), "`first name`");
105 assert_eq!(quote_identifier_shaped("we`ird"), "`we``ird`");
106 assert_eq!(quote_identifier_always("Person"), "`Person`");
107 }
108}