runique 2.1.10

A Django-inspired web framework for Rust with ORM, templates, and comprehensive security middleware
Documentation
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Macro `search_apply_op!` — maps Django operators to SeaORM conditions (eq, icontains, gte…).

// ─────────────────────────────────────────────────────────────────────────────
// search_apply_op! — maps a Django operator name to a SeaORM condition
// ─────────────────────────────────────────────────────────────────────────────

#[macro_export]
#[doc(hidden)]
macro_rules! search_apply_op {
    ($col:expr, eq,         $val:expr) => {
        $col.eq($val)
    };
    ($col:expr, exact,      $val:expr) => {
        $col.eq($val)
    };
    ($col:expr, ne,         $val:expr) => {
        $col.ne($val)
    };
    ($col:expr, gt,         $val:expr) => {
        $col.gt($val)
    };
    ($col:expr, lt,         $val:expr) => {
        $col.lt($val)
    };
    ($col:expr, gte,        $val:expr) => {
        $col.gte($val)
    };
    ($col:expr, lte,        $val:expr) => {
        $col.lte($val)
    };
    ($col:expr, like,       $val:expr) => {
        $col.like($val)
    };
    ($col:expr, ilike,      $val:expr) => {
        $col.ilike($val)
    };
    ($col:expr, not_like,   $val:expr) => {
        $col.not_like($val)
    };
    ($col:expr, not_ilike,  $val:expr) => {
        $col.not_ilike($val)
    };
    ($col:expr, contains,   $val:expr) => {
        $col.like(format!("%{}%", $val))
    };
    ($col:expr, icontains,  $val:expr) => {
        $col.ilike(format!("%{}%", $val))
    };
    ($col:expr, startswith, $val:expr) => {
        $col.like(format!("{}%", $val))
    };
    ($col:expr, endswith,   $val:expr) => {
        $col.like(format!("%{}", $val))
    };
    ($col:expr, iexact,     $val:expr) => {
        $col.ilike($val)
    };
}

// ─────────────────────────────────────────────────────────────────────────────
// search_munch! — TT muncher: iterates through conditions separated by ','
//
// Normalization: the input always receives a final comma added by
// the multi-condition arm of `search!`. Each arm consumes an item + ',' and
// recursively calls itself on the rest.
// ─────────────────────────────────────────────────────────────────────────────

#[macro_export]
#[doc(hidden)]
macro_rules! search_munch {
    // ── Base cases ──────────────────────────────────────────────────────────
    ($b:expr, $entity:ty;) => {};
    ($b:expr, $entity:ty; ,) => {};

    // ── join Relation — INNER JOIN ────────────────────────────────
    ($b:expr, $entity:ty; join $rel:ident , $($rest:tt)*) => {
        {
            use sea_orm::RelationTrait;
            $b = $b.join(
                <$entity as sea_orm::EntityTrait>::Relation::$rel.def()
            );
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── left_join Relation — LEFT JOIN ───────────────────────────
    ($b:expr, $entity:ty; left_join $rel:ident , $($rest:tt)*) => {
        {
            use sea_orm::RelationTrait;
            $b = $b.left_join(
                <$entity as sea_orm::EntityTrait>::Relation::$rel.def()
            );
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── mod::Col op val — filter on relation ─────────────────────
    ($b:expr, $entity:ty; $mod:ident :: $col:ident $op:ident $val:expr , $($rest:tt)*) => {
        {
            use sea_orm::ColumnTrait;
            $b = $b.filter(
                $crate::search_apply_op!($mod::Column::$col, $op, $val)
            );
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };
    // ── Col isnull ────────────────────────────────────────────────────────────
    ($b:expr, $entity:ty; $col:ident isnull , $($rest:tt)*) => {
        {
            use sea_orm::ColumnTrait;
            $b = $b.filter(<$entity as sea_orm::EntityTrait>::Column::$col.is_null());
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── Col not_null ──────────────────────────────────────────────────────────
    ($b:expr, $entity:ty; $col:ident not_null , $($rest:tt)*) => {
        {
            use sea_orm::ColumnTrait;
            $b = $b.filter(<$entity as sea_orm::EntityTrait>::Column::$col.is_not_null());
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── Col in [v1, v2] — Literal IN ────────────────────────────────────────
    ($b:expr, $entity:ty; $col:ident in [$($val:expr),+ $(,)?] , $($rest:tt)*) => {
        {
            use sea_orm::ColumnTrait;
            let conds = vec![$(<$entity as sea_orm::EntityTrait>::Column::$col.eq($val)),+];
            $b = $b.filter(sea_orm::Condition::any(conds));
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── Col not_in [v1, v2] — Literal NOT IN ────────────────────────────────
    ($b:expr, $entity:ty; $col:ident not_in [$($val:expr),+ $(,)?] , $($rest:tt)*) => {
        {
            use sea_orm::ColumnTrait;
            $b = $b.filter(
                <$entity as sea_orm::EntityTrait>::Column::$col.is_not_in(vec![$($val),+])
            );
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── Col in (expr) — Dynamic IN ─────────────────────────────────────────
    ($b:expr, $entity:ty; $col:ident in ($val:expr) , $($rest:tt)*) => {
        {
            use sea_orm::ColumnTrait;
            let __vals: ::std::vec::Vec<_> = (&$val).into_iter().cloned().collect();
            $b = $b.filter(<$entity as sea_orm::EntityTrait>::Column::$col.is_in(__vals));
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── Col not_in (expr) — Dynamic NOT IN ─────────────────────────────────
    ($b:expr, $entity:ty; $col:ident not_in ($val:expr) , $($rest:tt)*) => {
        {
            use sea_orm::ColumnTrait;
            let __vals: ::std::vec::Vec<_> = (&$val).into_iter().cloned().collect();
            $b = $b.filter(<$entity as sea_orm::EntityTrait>::Column::$col.is_not_in(__vals));
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── Col range (a, b) — BETWEEN ───────────────────────────────────────────
    ($b:expr, $entity:ty; $col:ident range ($start:expr, $end:expr) , $($rest:tt)*) => {
        {
            use sea_orm::ColumnTrait;
            $b = $b.filter(
                sea_orm::Condition::all()
                    .add(<$entity as sea_orm::EntityTrait>::Column::$col.gte($start))
                    .add(<$entity as sea_orm::EntityTrait>::Column::$col.lte($end))
            );
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── Col not_range (a, b) — NOT BETWEEN ───────────────────────────────────
    ($b:expr, $entity:ty; $col:ident not_range ($start:expr, $end:expr) , $($rest:tt)*) => {
        {
            use sea_orm::ColumnTrait;
            $b = $b.filter(
                sea_orm::Condition::any()
                    .add(<$entity as sea_orm::EntityTrait>::Column::$col.lt($start))
                    .add(<$entity as sea_orm::EntityTrait>::Column::$col.gt($end))
            );
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── or(Col1 op val, Col2 op val) — multi-column OR ─────────────────────
    ($b:expr, $entity:ty; or($($col:ident $op:ident $val:expr),+ $(,)?) , $($rest:tt)*) => {
        {
            use sea_orm::ColumnTrait;
            let mut cond = sea_orm::Condition::any();
            $(
                cond = cond.add(
                    $crate::search_apply_op!(<$entity as sea_orm::EntityTrait>::Column::$col, $op, $val)
                );
            )+
            $b = $b.filter(cond);
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── ! Col op val — exclusion ──────────────────────────────────────────────
    ($b:expr, $entity:ty; ! $col:ident $op:ident $val:expr , $($rest:tt)*) => {
        {
            use sea_orm::ColumnTrait;
            $b = $b.exclude(
                $crate::search_apply_op!(<$entity as sea_orm::EntityTrait>::Column::$col, $op, $val)
            );
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── asc Col / desc Col — sorting ──────────────────────────────────────────────
    ($b:expr, $entity:ty; asc $col:ident , $($rest:tt)*) => {
        $b = $b.order_by_asc(<$entity as sea_orm::EntityTrait>::Column::$col);
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    ($b:expr, $entity:ty; desc $col:ident , $($rest:tt)*) => {
        $b = $b.order_by_desc(<$entity as sea_orm::EntityTrait>::Column::$col);
        $crate::search_munch!($b, $entity; $($rest)*);
    };

    // ── Col op val — general filter ───────────────────────────────────────────
    ($b:expr, $entity:ty; $col:ident $op:ident $val:expr , $($rest:tt)*) => {
        {
            use sea_orm::ColumnTrait;
            $b = $b.filter(
                $crate::search_apply_op!(<$entity as sea_orm::EntityTrait>::Column::$col, $op, $val)
            );
        }
        $crate::search_munch!($b, $entity; $($rest)*);
    };
}

// ─────────────────────────────────────────────────────────────────────────────
// search! — SeaORM filtering DSL, Django syntax
//
// Available operators (Col op val) :
//   eq, exact, ne, gt, lt, gte, lte
//   like, ilike, not_like, not_ilike
//   contains, icontains, startswith, endswith, iexact
//
// Special forms :
//   Col isnull           → IS NULL
//   Col not_null         → IS NOT NULL
//   Col in [v1, v2]      → IN (literal)
//   Col in (expr)        → IN (dynamic)
//   Col not_in [v1, v2]  → NOT IN (literal)
//   Col not_in (expr)    → NOT IN (dynamic)
//   Col range (a, b)     → BETWEEN a AND b
//   Col not_range (a, b) → NOT BETWEEN
//   or(C1 op v, C2 op v) → multi-column OR
//   ! Col op val         → exclusion (NOT)
//   search!(Entity)      → fetch all without filter
// ─────────────────────────────────────────────────────────────────────────────

#[macro_export]
macro_rules! search {
    // ── @Form => ... (delegates to the associated Entity type via FormEntity) ──────────
    (@ $form:ty => $($tt:tt)+) => {{
        $crate::search!(<$form as $crate::forms::FormEntity>::Entity => $($tt)+)
    }};

    // ── Fetch all ─────────────────────────────────────────────────────────────
    ($entity:ty) => {{
        $crate::macros::bdd::objects::Objects::<$entity>::new().all()
    }};

    // ── Col isnull ────────────────────────────────────────────────────────────
    ($entity:ty => $col:ident isnull) => {{
        use sea_orm::{EntityTrait, ColumnTrait};
        $crate::macros::bdd::objects::Objects::<$entity>::new()
            .filter(<$entity as EntityTrait>::Column::$col.is_null())
    }};

    // ── Col not_null ──────────────────────────────────────────────────────────
    ($entity:ty => $col:ident not_null) => {{
        use sea_orm::{EntityTrait, ColumnTrait};
        $crate::macros::bdd::objects::Objects::<$entity>::new()
            .filter(<$entity as EntityTrait>::Column::$col.is_not_null())
    }};

    // ── Col in [v1, v2] — Literal IN ────────────────────────────────────────
    ($entity:ty => $col:ident in [$($val:expr),+ $(,)?]) => {{
        use sea_orm::{EntityTrait, ColumnTrait, Condition};
        let conds = vec![$(<$entity as EntityTrait>::Column::$col.eq($val)),+];
        $crate::macros::bdd::objects::Objects::<$entity>::new()
            .filter(Condition::any(conds))
    }};

    // ── Col not_in [v1, v2] — Literal NOT IN ────────────────────────────────
    ($entity:ty => $col:ident not_in [$($val:expr),+ $(,)?]) => {{
        use sea_orm::{EntityTrait, ColumnTrait};
        $crate::macros::bdd::objects::Objects::<$entity>::new()
            .filter(<$entity as EntityTrait>::Column::$col.is_not_in(vec![$($val),+]))
    }};

    // ── Col in (expr) — Dynamic IN ─────────────────────────────────────────
    ($entity:ty => $col:ident in ($val:expr)) => {{
        use sea_orm::{EntityTrait, ColumnTrait};
        let __vals: ::std::vec::Vec<_> = (&$val).into_iter().cloned().collect();
        $crate::macros::bdd::objects::Objects::<$entity>::new()
            .filter(<$entity as EntityTrait>::Column::$col.is_in(__vals))
    }};

    // ── Col not_in (expr) — Dynamic NOT IN ─────────────────────────────────
    ($entity:ty => $col:ident not_in ($val:expr)) => {{
        use sea_orm::{EntityTrait, ColumnTrait};
        let __vals: ::std::vec::Vec<_> = (&$val).into_iter().cloned().collect();
        $crate::macros::bdd::objects::Objects::<$entity>::new()
            .filter(<$entity as EntityTrait>::Column::$col.is_not_in(__vals))
    }};

    // ── Col range (a, b) — BETWEEN ───────────────────────────────────────────
    ($entity:ty => $col:ident range ($start:expr, $end:expr)) => {{
        use sea_orm::{EntityTrait, ColumnTrait, Condition};
        $crate::macros::bdd::objects::Objects::<$entity>::new()
            .filter(Condition::all([
                <$entity as EntityTrait>::Column::$col.gte($start),
                <$entity as EntityTrait>::Column::$col.lte($end),
            ]))
    }};

    // ── Col not_range (a, b) — NOT BETWEEN ───────────────────────────────────
    ($entity:ty => $col:ident not_range ($start:expr, $end:expr)) => {{
        use sea_orm::{EntityTrait, ColumnTrait, Condition};
        $crate::macros::bdd::objects::Objects::<$entity>::new()
            .filter(Condition::any([
                <$entity as EntityTrait>::Column::$col.lt($start),
                <$entity as EntityTrait>::Column::$col.gt($end),
            ]))
    }};

    // ── or(Col1 op val, Col2 op val) — multi-column OR ─────────────────────
    ($entity:ty => or($($col:ident $op:ident $val:expr),+ $(,)?)) => {{
        use sea_orm::{EntityTrait, ColumnTrait, Condition};
        let mut cond = Condition::any();
        $(
            cond = cond.add(
                $crate::search_apply_op!(<$entity as EntityTrait>::Column::$col, $op, $val)
            );
        )+
        $crate::macros::bdd::objects::Objects::<$entity>::new().filter(cond)
    }};

    // ── ! Col op val — exclusion ──────────────────────────────────────────────
    ($entity:ty => ! $col:ident $op:ident $val:expr) => {{
        use sea_orm::{EntityTrait, ColumnTrait};
        $crate::macros::bdd::objects::Objects::<$entity>::new()
            .exclude($crate::search_apply_op!(<$entity as EntityTrait>::Column::$col, $op, $val))
    }};

    // ── Col op val — single condition ─────────────────────────────────────────
    ($entity:ty => $col:ident $op:ident $val:expr) => {{
        use sea_orm::{EntityTrait, ColumnTrait};
        $crate::macros::bdd::objects::Objects::<$entity>::new()
            .filter($crate::search_apply_op!(<$entity as EntityTrait>::Column::$col, $op, $val))
    }};

    // ── Multi-conditions (TT muncher) ─────────────────────────────────────────
    // Captures all the rest. Adds a final comma to normalize the muncher.
    ($entity:ty => $($tt:tt)+) => {{
        use sea_orm::{EntityTrait, ColumnTrait};
        let mut b = $crate::macros::bdd::objects::Objects::<$entity>::new().all();
        $crate::search_munch!(b, $entity; $($tt)+ ,);
        b
    }};
}

// ─────────────────────────────────────────────────────────────────────────────
// search_cond! — returns a sea_orm::Condition for use in existing query builders
//
// Unlike `search!` which wraps an Objects query builder, `search_cond!` returns
// a raw Condition to merge into an already-in-progress query.
// ─────────────────────────────────────────────────────────────────────────────

#[macro_export]
macro_rules! search_cond {
    // ── all_columns icontains val — runtime LIKE across every column ──────────
    ($entity:ty => all_columns icontains $val:expr) => {{
        use sea_orm::{EntityTrait, Iterable, IdenStatic};
        let __val = ($val).replace('\'', "''");
        let mut __cond = sea_orm::Condition::any();
        for col in <$entity as EntityTrait>::Column::iter() {
            __cond = __cond.add(sea_orm::sea_query::Expr::cust(
                format!("LOWER(CAST({} AS TEXT)) LIKE LOWER('%{}%')", col.as_str(), __val)
            ));
        }
        __cond
    }};

    // ── or("col1" icontains val, "col2" icontains val, ...) ──────────────────
    ($entity:ty => or($($col:literal icontains $val:expr),+ $(,)?)) => {{
        let mut __cond = sea_orm::Condition::any();
        $(
            {
                let __v = ($val).replace('\'', "''");
                __cond = __cond.add(sea_orm::sea_query::Expr::cust(
                    format!("LOWER(CAST({} AS TEXT)) LIKE LOWER('%{}%')", $col, __v)
                ));
            }
        )+
        __cond
    }};
}