halo_space/
macros.rs

1//! Macro helpers providing variadic-style ergonomics for builders.
2//! Macros like `select_cols!` / `where_exprs!` accept varargs strings without manual Vec creation.
3
4#[doc(hidden)]
5#[macro_export]
6macro_rules! __collect_strings {
7    () => {
8        Vec::<String>::new()
9    };
10    ($($value:expr),+ $(,)?) => {{
11        let mut values = Vec::<String>::new();
12        $(
13            $crate::extend_into_strings($value, &mut values);
14        )*
15        values
16    }};
17}
18
19#[doc(hidden)]
20#[macro_export]
21macro_rules! __collect_static_strs {
22    () => {
23        Vec::<&'static str>::new()
24    };
25    ($($value:expr),+ $(,)?) => {{
26        let mut values = Vec::<&'static str>::new();
27        $(
28            values.push($value);
29        )*
30        values
31    }};
32}
33
34#[doc(hidden)]
35#[macro_export]
36macro_rules! __builder_with_strings {
37    ($builder:expr, $method:ident $(, $arg:expr)* $(,)?) => {
38        $builder.$method($crate::__collect_strings!($($arg),*))
39    };
40}
41
42pub trait IntoStrings {
43    fn extend_into_strings(self, dst: &mut Vec<String>);
44}
45
46impl IntoStrings for String {
47    fn extend_into_strings(self, dst: &mut Vec<String>) {
48        dst.push(self);
49    }
50}
51
52impl IntoStrings for &str {
53    fn extend_into_strings(self, dst: &mut Vec<String>) {
54        dst.push(self.to_string());
55    }
56}
57
58impl<const N: usize, T> IntoStrings for [T; N]
59where
60    T: Into<String> + Clone,
61{
62    fn extend_into_strings(self, dst: &mut Vec<String>) {
63        for item in &self {
64            dst.push(item.clone().into());
65        }
66    }
67}
68
69impl<T> IntoStrings for &[T]
70where
71    T: Into<String> + Clone,
72{
73    fn extend_into_strings(self, dst: &mut Vec<String>) {
74        for item in self {
75            dst.push(item.clone().into());
76        }
77    }
78}
79
80impl<T> IntoStrings for &Vec<T>
81where
82    T: Into<String> + Clone,
83{
84    fn extend_into_strings(self, dst: &mut Vec<String>) {
85        for item in self {
86            dst.push(item.clone().into());
87        }
88    }
89}
90
91impl<T> IntoStrings for Vec<T>
92where
93    T: Into<String>,
94{
95    fn extend_into_strings(self, dst: &mut Vec<String>) {
96        for item in self {
97            dst.push(item.into());
98        }
99    }
100}
101
102#[doc(hidden)]
103pub fn extend_into_strings<T>(value: T, dst: &mut Vec<String>)
104where
105    T: IntoStrings,
106{
107    value.extend_into_strings(dst);
108}
109
110#[doc(hidden)]
111pub fn collect_into_strings<T>(value: T) -> Vec<String>
112where
113    T: IntoStrings,
114{
115    let mut dst = Vec::new();
116    value.extend_into_strings(&mut dst);
117    dst
118}
119
120#[doc(hidden)]
121#[macro_export]
122macro_rules! __builder_with_strings_after {
123    ($builder:expr, $method:ident, $first:expr $(, $arg:expr)* $(,)?) => {
124        $builder.$method($first, $crate::__collect_strings!($($arg),*))
125    };
126}
127
128#[doc(hidden)]
129#[macro_export]
130macro_rules! __builder_with_strings_after_two {
131    ($builder:expr, $method:ident, $first:expr, $second:expr $(, $arg:expr)* $(,)?) => {
132        $builder.$method($first, $second, $crate::__collect_strings!($($arg),*))
133    };
134}
135
136/// Variadic helper for `SelectBuilder::select`.
137#[macro_export]
138macro_rules! select_cols {
139    ($builder:expr $(, $col:expr)* $(,)?) => {
140        $crate::__builder_with_strings!($builder, select $(, $col)*)
141    };
142}
143pub use crate::select_cols;
144
145/// Variadic helper for `SelectBuilder::select_more`.
146#[macro_export]
147macro_rules! select_more_cols {
148    ($builder:expr $(, $col:expr)* $(,)?) => {
149        $crate::__builder_with_strings!($builder, select_more $(, $col)*)
150    };
151}
152pub use crate::select_more_cols;
153
154/// Variadic helper for `SelectBuilder::from`.
155#[macro_export]
156macro_rules! from_tables {
157    ($builder:expr $(, $table:expr)* $(,)?) => {
158        $crate::__builder_with_strings!($builder, from $(, $table)*)
159    };
160}
161pub use crate::from_tables;
162
163/// Variadic helper for `SelectBuilder::join`.
164#[macro_export]
165macro_rules! join_on {
166    ($builder:expr, $table:expr $(, $expr:expr)* $(,)?) => {
167        $crate::__builder_with_strings_after!($builder, join, $table $(, $expr)*)
168    };
169}
170pub use crate::join_on;
171
172/// Variadic helper for `SelectBuilder::join_with_option`.
173#[macro_export]
174macro_rules! join_with_option {
175    ($builder:expr, $option:expr, $table:expr $(, $expr:expr)* $(,)?) => {
176        $crate::__builder_with_strings_after_two!($builder, join_with_option, $option, $table $(, $expr)*)
177    };
178}
179pub use crate::join_with_option;
180
181/// Variadic helper for `where_` calls (Select/Update/Delete).
182#[macro_export]
183macro_rules! where_exprs {
184    ($builder:expr $(, $expr:expr)* $(,)?) => {
185        $crate::__builder_with_strings!($builder, where_ $(, $expr)*)
186    };
187}
188pub use crate::where_exprs;
189
190/// Variadic helper for `having`.
191#[macro_export]
192macro_rules! having_exprs {
193    ($builder:expr $(, $expr:expr)* $(,)?) => {
194        $crate::__builder_with_strings!($builder, having $(, $expr)*)
195    };
196}
197pub use crate::having_exprs;
198
199/// Variadic helper for `group_by`.
200#[macro_export]
201macro_rules! group_by_cols {
202    ($builder:expr $(, $col:expr)* $(,)?) => {
203        $crate::__builder_with_strings!($builder, group_by $(, $col)*)
204    };
205}
206pub use crate::group_by_cols;
207
208/// Variadic helper for `order_by`.
209#[macro_export]
210macro_rules! order_by_cols {
211    ($builder:expr $(, $col:expr)* $(,)?) => {
212        $crate::__builder_with_strings!($builder, order_by $(, $col)*)
213    };
214}
215pub use crate::order_by_cols;
216
217/// Variadic helper for `InsertBuilder::cols`.
218#[macro_export]
219macro_rules! insert_cols {
220    ($builder:expr $(, $col:expr)* $(,)?) => {
221        $crate::__builder_with_strings!($builder, cols $(, $col)*)
222    };
223}
224pub use crate::insert_cols;
225
226/// Variadic helper for `InsertBuilder::select`.
227#[macro_export]
228macro_rules! insert_select_cols {
229    ($builder:expr $(, $col:expr)* $(,)?) => {
230        $crate::__builder_with_strings!($builder, select $(, $col)*)
231    };
232}
233pub use crate::insert_select_cols;
234
235/// Variadic helper for `returning` calls.
236#[macro_export]
237macro_rules! returning_cols {
238    ($builder:expr $(, $col:expr)* $(,)?) => {
239        $crate::__builder_with_strings!($builder, returning $(, $col)*)
240    };
241}
242pub use crate::returning_cols;
243
244/// Variadic helper for `DeleteBuilder::delete_from`.
245#[macro_export]
246macro_rules! delete_from_tables {
247    ($builder:expr $(, $table:expr)* $(,)?) => {
248        $crate::__builder_with_strings!($builder, delete_from $(, $table)*)
249    };
250}
251pub use crate::delete_from_tables;
252
253/// Variadic helper for `UpdateBuilder::update`.
254#[macro_export]
255macro_rules! update_tables {
256    ($builder:expr $(, $table:expr)* $(,)?) => {
257        $crate::__builder_with_strings!($builder, update $(, $table)*)
258    };
259}
260pub use crate::update_tables;
261
262/// Variadic helper for `UpdateBuilder::set`.
263#[macro_export]
264macro_rules! update_set {
265    ($builder:expr $(, $assignment:expr)* $(,)?) => {
266        $crate::__builder_with_strings!($builder, set $(, $assignment)*)
267    };
268}
269pub use crate::update_set;
270
271/// Variadic helper for `UpdateBuilder::set_more`.
272#[macro_export]
273macro_rules! update_set_more {
274    ($builder:expr $(, $assignment:expr)* $(,)?) => {
275        $crate::__builder_with_strings!($builder, set_more $(, $assignment)*)
276    };
277}
278pub use crate::update_set_more;
279
280/// Variadic helper for `CTEBuilder::select`.
281#[macro_export]
282macro_rules! cte_select_cols {
283    ($builder:expr $(, $col:expr)* $(,)?) => {
284        $crate::__builder_with_strings!($builder, select $(, $col)*)
285    };
286}
287pub use crate::cte_select_cols;
288
289/// Variadic helper for `CTEBuilder::delete_from`.
290#[macro_export]
291macro_rules! cte_delete_from {
292    ($builder:expr $(, $table:expr)* $(,)?) => {
293        $crate::__builder_with_strings!($builder, delete_from $(, $table)*)
294    };
295}
296pub use crate::cte_delete_from;
297
298/// Variadic helper for `CTEBuilder::update`.
299#[macro_export]
300macro_rules! cte_update_tables {
301    ($builder:expr $(, $table:expr)* $(,)?) => {
302        $crate::__builder_with_strings!($builder, update $(, $table)*)
303    };
304}
305pub use crate::cte_update_tables;
306
307/// Variadic helper for `CTEQueryBuilder::table`.
308#[macro_export]
309macro_rules! cte_query_table {
310    ($builder:expr, $name:expr $(, $col:expr)* $(,)?) => {
311        $crate::__builder_with_strings_after!($builder, table, $name $(, $col)*)
312    };
313}
314pub use crate::cte_query_table;
315
316/// Variadic helper for `CreateTableBuilder::define`.
317#[macro_export]
318macro_rules! create_table_define {
319    ($builder:expr $(, $def:expr)* $(,)?) => {
320        $crate::__builder_with_strings!($builder, define $(, $def)*)
321    };
322}
323pub use crate::create_table_define;
324
325/// Variadic helper for `CreateTableBuilder::option`.
326#[macro_export]
327macro_rules! create_table_option {
328    ($builder:expr $(, $opt:expr)* $(,)?) => {
329        $crate::__builder_with_strings!($builder, option $(, $opt)*)
330    };
331}
332pub use crate::create_table_option;
333
334/// Variadic helper for `Struct::with_tag`.
335#[macro_export]
336macro_rules! struct_with_tag {
337    ($builder:expr $(, $tag:expr)* $(,)?) => {
338        $builder.with_tag($crate::__collect_static_strs!($($tag),*))
339    };
340}
341pub use crate::struct_with_tag;
342
343/// Variadic helper for `Struct::without_tag`.
344#[macro_export]
345macro_rules! struct_without_tag {
346    ($builder:expr $(, $tag:expr)* $(,)?) => {
347        $builder.without_tag($crate::__collect_static_strs!($($tag),*))
348    };
349}
350pub use crate::struct_without_tag;