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
//! Declarative macros for column-index constant generation.
//!
//! The [`crate::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.agent_id"` are fully supported.
// Declarative macro: define_store!
/// Generate a DB-backed store struct, its `open()` constructor, and a global
/// singleton (via [`crate::global_store!`]).
///
/// Eliminates ~64 lines of boilerplate per store module.
///
/// # Syntax
///
/// ```ignore
/// define_store! {
/// /// Doc comment for the global static.
/// pub static STORE_NAME: StoreType,
/// db_name = "db_file_name",
/// schema = SCHEMA,
/// post_open = ensure_admin_user, // optional; omitted when not needed
/// expect = "custom panic message",
/// }
/// ```
///
/// The `post_open` field is optional. When present, it names an
/// `async fn(&self) -> anyhow::Result<()>` method that is called after
/// the database connection is established (via `this.$method().await?`)
/// but before the store is returned. The method must be defined in a
/// separate `impl Store { … }` block.
///
/// # Generated items
///
/// 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
///
/// An arbitrary-block form is **not** provided because Rust `macro_rules!`
/// hygiene prevents user-provided `self` / `conn` tokens inside generated
/// method bodies. The `post_open` approach avoids this limitation entirely.
;
$?
Ok
}
}
$crateglobal_store!
};
}