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
//! Declarative macros for column-index constant generation.
//!
//! The [`columns!`] macro eliminates the brittle hand-maintained coupling
//! between a `*_COLUMNS` SQL column-string and its `COL_*` positional-index
//! constants. Instead of 3–4 separate locations to update per column set,
//! a single `columns!` invocation serves as the single source of truth.
// ---------------------------------------------------------------------------
// Helper: join literals with ", " — used internally by columns!
// ---------------------------------------------------------------------------
/// Join literal strings with `", "` separator.
///
/// ```ignore
/// assert_eq!(__columns_join!("a", "b", "c"), "a, b, c");
/// ```
// ---------------------------------------------------------------------------
// Helper: generate COL_* index constants — used internally by columns!
// ---------------------------------------------------------------------------
/// Generate `COL_{prefix}_{name}` index constants with positional indices.
///
/// Recursively processes each column identifier, emitting a `const COL_{P}_{N}: usize`
/// with the appropriate zero-based position.
// ---------------------------------------------------------------------------
// Public macro: columns!
// ---------------------------------------------------------------------------
/// Generate a column-string constant and matching column-index constants from
/// a single source-of-truth list.
///
/// # Syntax
///
/// ```ignore
/// columns! {
/// /// Optional doc comment (attached to the column string constant).
/// COLUMNS_NAME [PREFIX] {
/// FIELD_NAME => "sql_column_expression",
/// ANOTHER => "another_column",
/// }
/// }
/// ```
///
/// # Expansion
///
/// For input
/// ```ignore
/// columns! {
/// pub(crate) const MY_COLUMNS [mc] {
/// FOO => "foo",
/// BAR => "bar",
/// }
/// }
/// ```
///
/// expands to:
/// - `pub(crate) const MY_COLUMNS: &str = "foo, bar";`
/// - `const COL_MC_FOO: usize = 0;`
/// - `const COL_MC_BAR: usize = 1;`
///
/// # Expression overrides
///
/// The `=> "..."` syntax accepts any SQL expression as a string literal, so
/// complex expressions like `"json_each.value AS error"`, `"COUNT(s.id)"`,
/// or `"sm.session_key"` are fully supported.
// ---------------------------------------------------------------------------
// Declarative macro: define_store!
// ---------------------------------------------------------------------------
/// Generate a DB-backed store struct, its `open()` constructor, and a global
/// singleton (via [`global_store!`]).
///
/// Eliminates ~64 lines of boilerplate per store module.
///
/// **Form 1 — simple (no post-open step):**
/// ```ignore
/// define_store! {
/// /// Doc comment for the global static.
/// pub static STORE_NAME: StoreType,
/// db_name = "db_file_name",
/// schema = SCHEMA,
/// // optional: expect = "custom panic message",
/// }
/// ```
///
/// **Form 2 — post-open via `&self` method:**
/// ```ignore
/// define_store! {
/// pub static USER_STORE: UserStore,
/// db_name = "users",
/// schema = SCHEMA,
/// post_open = ensure_admin_user,
/// }
/// ```
/// The method is called via `this.$method().await?` after store construction.
/// It must be `async fn(&self) -> anyhow::Result<()>`. Defined in a separate
/// `impl Store { … }` block.
///
/// # Generated items
///
/// For both forms the macro generates:
/// - `#[derive(Clone, Debug)] pub struct $Store { pub(crate) conn: Connection }`
/// - `impl $Store { pub async fn open(root: &Path) -> anyhow::Result<Self> { … } }`
/// - A `global_store!` invocation creating the `OnceCell`, `init_global()`, and
/// `store()` singleton accessor
///
/// This macro is intentionally limited to these two forms. An arbitrary-block
/// form is **not** provided because Rust `macro_rules!` hygiene prevents
/// user-provided `self` / `conn` tokens inside generated method bodies.
/// The `init`-method approach (Form 2) avoids this limitation entirely.
}
$crateglobal_store!
};
// ── Form 2: Post-open via init method (auto expect) ─────────────────
=> ;
// ── Form 2b: Post-open via init method with custom expect ───────────
=>
$crateglobal_store!
};
}