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
//! A name with two faces: the one a database sees and the one Rust can spell.
//!
//! A column is declared once and used twice — as the string in a `CREATE TABLE`, a `try_get`, a
//! `#[serde(rename = "…")]` and an error message, and as the IDENT of the field that carries it in
//! an emitted row. Those two are not always the same string: `type` and `bit-rate` are ordinary
//! column names and neither is a legal field name.
//!
//! Keeping them in ONE value is what stops them drifting. A pair of strings passed around
//! separately gets swapped eventually — and the failure is silent, because a mangled name still
//! compiles and a column called `bit_rate` still exists somewhere. Here the choice is made by the
//! trait a use site reaches for: quoting a name into an expansion yields the Rust face, and
//! formatting it yields the external one.
//!
//! Borrowed from `polarsync-codegen`'s `RustSafeName`, which carries the same two faces for the
//! same reason.
use fmt;
use ToSnakeCase as _;
use ;
/// The keywords a field cannot be named. Reserved ones are here too: `become` and `gen` are not
/// usable as identifiers in this edition either, and a list that only tracked what is currently
/// STRICT would let an emission break on an edition bump.
///
/// `union`, `macro_rules`, `raw` and `safe` are deliberately absent — they are contextual, and
/// `let union = 1;` compiles.
const KEYWORDS: & = &;
/// A name as the database spells it, together with the identifier Rust can spell it as.
///
/// The EXTERNAL name is never rewritten: it is what reaches a `CREATE TABLE`, a `SELECT`, and the
/// `rename` attribute an emitted row carries — so a declaration that says `rename = "bit-rate"`
/// gets a column called `bit-rate`, whatever the field holding it has to be called.
/// The external name as an identifier, by the smallest change that makes it one.
///
/// - The name is normalised to SNAKE CASE: `bitRate` becomes `bit_rate`, `HTTPServer` becomes
/// `http_server`. The Rust face is idiomatic Rust, not merely legal Rust.
/// - `-`, and every other character an identifier cannot carry, becomes `_`.
/// - A leading digit gains a `_`.
/// - A KEYWORD gains a trailing `_`.
/// - `_` alone is the wildcard pattern rather than an identifier, so it gains a second one.
///
/// The keyword rule is a trailing underscore rather than a raw identifier because `r#` cannot
/// express four of them: `r#crate`, `r#self`, `r#super` and `r#Self` are all rejected by the
/// compiler. One rule that covers every keyword beats two rules with an exception list.
///
/// The kept set is ASCII, which is narrower than what an identifier may hold. That is on purpose
/// and it is about not PANICKING: `char::is_alphanumeric` accepts `²` — a number by Unicode's
/// reckoning and not an identifier character by the compiler's — so a rule written that way builds
/// a string [`syn::Ident`] rejects, and a proc macro that panics reports nothing a reader can act
/// on. A non-ASCII external name keeps its own spelling; only the field carrying it is folded.
/// The RUST face — quoting a name always yields valid Rust.
/// The EXTERNAL face — string literals, error messages, and the `rename` attributes an emitted row
/// carries.