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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
use crate::field_witnesses::{FieldName, HasField};
use crate::mongo_comparable::{MongoComparable, MongoOrdered};
use crate::path::Path;
use num_traits::Num;
/// Typed MongoDB aggregation expression.
///
/// `Expr<T, V>` carries:
/// - `T`: root document type used for field witness constraints
/// - `V`: expression value type used for compile-time operator constraints
#[derive(Clone, Debug, PartialEq)]
pub struct Expr<T, V> {
repr: bson::Bson,
_marker: std::marker::PhantomData<(T, V)>,
}
impl<T, V> Expr<T, V> {
/// Creates an expression from a raw BSON representation.
///
/// This bypasses most static guarantees and is intended as an escape hatch
/// for unsupported operators.
pub fn unsafe_raw(repr: bson::Bson) -> Self {
Self {
repr,
_marker: std::marker::PhantomData,
}
}
/// Borrows the BSON representation of the expression.
pub fn as_bson(&self) -> &bson::Bson {
&self.repr
}
/// Consumes the expression and returns its BSON representation.
pub fn into_bson(self) -> bson::Bson {
self.repr
}
/// Widens the expression value type while keeping the same BSON representation.
pub fn widen<U>(self) -> Expr<T, U> {
Expr {
repr: self.repr,
_marker: std::marker::PhantomData,
}
}
}
impl<T, V> From<Expr<T, V>> for bson::Bson {
fn from(expr: Expr<T, V>) -> Self {
expr.into_bson()
}
}
/// Builder for typed MongoDB aggregation expressions.
#[derive(Default)]
pub struct ExprBuilder<T> {
prefix: Vec<String>,
_marker: std::marker::PhantomData<T>,
}
impl<T> ExprBuilder<T> {
/// Creates a new expression builder.
pub fn new() -> Self {
Self {
prefix: Vec::new(),
_marker: std::marker::PhantomData,
}
}
/// Creates a literal expression from a BSON-convertible value.
pub fn from<V>(&self, value: V) -> Expr<T, V>
where
V: Into<bson::Bson>,
{
Expr::unsafe_raw(value.into())
}
/// Creates a raw expression from BSON.
pub fn unsafe_expr<V>(&self, value: bson::Bson) -> Expr<T, V> {
Expr::unsafe_raw(value)
}
/// Returns a field reference expression like `$field` or `$prefix.field`.
pub fn select<F>(&self) -> Expr<T, T::Value>
where
F: FieldName,
T: HasField<F>,
{
let path = if self.prefix.is_empty() {
F::field_name().to_string()
} else {
format!("{}.{}", self.prefix.join("."), F::field_name())
};
Expr::unsafe_raw(bson::Bson::String(format!("${path}")))
}
/// Creates a nested expression builder using the same lookup pattern as other builders.
pub fn with_lookup<F: FieldName, L, G: FieldName, U: HasField<G>>(
&self,
lookup: L,
) -> ExprBuilder<U>
where
T: HasField<F>,
L: FnOnce(&Path<F, T, T>) -> Path<G, U, T>,
{
let base_field: Path<F, T, T> = Path {
prefix: self.prefix.clone(),
_marker: std::marker::PhantomData,
};
let resolved = lookup(&base_field);
ExprBuilder {
prefix: resolved.prefix,
_marker: std::marker::PhantomData,
}
}
/// Convenience nested builder using identity lookup.
pub fn with_field<F: FieldName>(&self) -> ExprBuilder<T>
where
T: HasField<F>,
{
self.with_lookup::<F, _, F, T>(|path| path.clone())
}
/// Compares two expressions for equality using MongoDB's `$eq` operator.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::{FieldWitnesses, MongoComparable};
///
/// #[derive(FieldWitnesses, MongoComparable)]
/// struct User {
/// pub age: i32,
/// }
///
/// let b = expr::empty::<User>();
/// let condition = b.eq(b.select::<user_fields::Age>(), b.from(18));
/// assert_eq!(condition.into_bson(), bson::doc! { "$eq": ["$age", 18] }.into());
/// ```
pub fn eq<A, B>(&self, expr1: Expr<T, A>, expr2: Expr<T, B>) -> Expr<T, bool>
where
T: MongoComparable<A, B>,
{
binary_op("$eq", expr1, expr2)
}
/// Compares two expressions for inequality using MongoDB's `$ne` operator.
pub fn ne<A, B>(&self, expr1: Expr<T, A>, expr2: Expr<T, B>) -> Expr<T, bool>
where
T: MongoComparable<A, B>,
{
binary_op("$ne", expr1, expr2)
}
/// Returns true if `expr1` is greater than `expr2` using `$gt`.
///
/// Requires [`MongoOrdered`] evidence in addition to [`MongoComparable`],
/// so only orderable type pairs can be used.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::{FieldWitnesses, MongoComparable};
///
/// #[derive(FieldWitnesses, MongoComparable)]
/// struct User {
/// pub score: i32,
/// }
///
/// let b = expr::empty::<User>();
/// let condition = b.gt(b.select::<user_fields::Score>(), b.from(10));
/// assert_eq!(condition.into_bson(), bson::doc! { "$gt": ["$score", 10] }.into());
/// ```
pub fn gt<A, B>(&self, expr1: Expr<T, A>, expr2: Expr<T, B>) -> Expr<T, bool>
where
T: MongoComparable<A, B> + MongoOrdered<A, B>,
{
binary_op("$gt", expr1, expr2)
}
/// Returns true if `expr1` is greater than or equal to `expr2` using `$gte`.
///
/// Requires [`MongoOrdered`] evidence in addition to [`MongoComparable`].
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::{FieldWitnesses, MongoComparable};
///
/// #[derive(FieldWitnesses, MongoComparable)]
/// struct User {
/// pub age: i32,
/// }
///
/// let b = expr::empty::<User>();
/// let condition = b.gte(b.select::<user_fields::Age>(), b.from(18));
/// assert_eq!(condition.into_bson(), bson::doc! { "$gte": ["$age", 18] }.into());
/// ```
pub fn gte<A, B>(&self, expr1: Expr<T, A>, expr2: Expr<T, B>) -> Expr<T, bool>
where
T: MongoComparable<A, B> + MongoOrdered<A, B>,
{
binary_op("$gte", expr1, expr2)
}
/// Returns true if `expr1` is less than `expr2` using `$lt`.
///
/// Requires [`MongoOrdered`] evidence in addition to [`MongoComparable`].
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::{FieldWitnesses, MongoComparable};
///
/// #[derive(FieldWitnesses, MongoComparable)]
/// struct User {
/// pub age: i32,
/// }
///
/// let b = expr::empty::<User>();
/// let condition = b.lt(b.select::<user_fields::Age>(), b.from(65));
/// assert_eq!(condition.into_bson(), bson::doc! { "$lt": ["$age", 65] }.into());
/// ```
pub fn lt<A, B>(&self, expr1: Expr<T, A>, expr2: Expr<T, B>) -> Expr<T, bool>
where
T: MongoComparable<A, B> + MongoOrdered<A, B>,
{
binary_op("$lt", expr1, expr2)
}
/// Returns true if `expr1` is less than or equal to `expr2` using `$lte`.
///
/// Requires [`MongoOrdered`] evidence in addition to [`MongoComparable`].
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::{FieldWitnesses, MongoComparable};
///
/// #[derive(FieldWitnesses, MongoComparable)]
/// struct User {
/// pub score: i32,
/// }
///
/// let b = expr::empty::<User>();
/// let condition = b.lte(b.select::<user_fields::Score>(), b.from(100));
/// assert_eq!(condition.into_bson(), bson::doc! { "$lte": ["$score", 100] }.into());
/// ```
pub fn lte<A, B>(&self, expr1: Expr<T, A>, expr2: Expr<T, B>) -> Expr<T, bool>
where
T: MongoComparable<A, B> + MongoOrdered<A, B>,
{
binary_op("$lte", expr1, expr2)
}
/// Combines boolean expressions with MongoDB's `$and`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::{FieldWitnesses, MongoComparable};
///
/// #[derive(FieldWitnesses, MongoComparable)]
/// struct User {
/// pub age: i32,
/// pub score: i32,
/// }
///
/// let b = expr::empty::<User>();
/// let expr = b.and(
/// b.gt(b.select::<user_fields::Age>(), b.from(18)),
/// vec![b.gte(b.select::<user_fields::Score>(), b.from(100))],
/// );
///
/// assert_eq!(
/// expr.into_bson(),
/// bson::doc! {
/// "$and": [
/// { "$gt": ["$age", 18] },
/// { "$gte": ["$score", 100] }
/// ]
/// }
/// .into()
/// );
/// ```
pub fn and(&self, head: Expr<T, bool>, tail: Vec<Expr<T, bool>>) -> Expr<T, bool> {
nary_op("$and", head, tail)
}
/// Combines boolean expressions with MongoDB's `$or`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::{FieldWitnesses, MongoComparable};
///
/// #[derive(FieldWitnesses, MongoComparable)]
/// struct User {
/// pub age: i32,
/// pub score: i32,
/// }
///
/// let b = expr::empty::<User>();
/// let expr = b.or(
/// b.lt(b.select::<user_fields::Age>(), b.from(18)),
/// vec![b.gte(b.select::<user_fields::Score>(), b.from(90))],
/// );
///
/// assert_eq!(
/// expr.into_bson(),
/// bson::doc! {
/// "$or": [
/// { "$lt": ["$age", 18] },
/// { "$gte": ["$score", 90] }
/// ]
/// }
/// .into()
/// );
/// ```
pub fn or(&self, head: Expr<T, bool>, tail: Vec<Expr<T, bool>>) -> Expr<T, bool> {
nary_op("$or", head, tail)
}
/// Negates a boolean expression with MongoDB's `$not`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::{FieldWitnesses, MongoComparable};
///
/// #[derive(FieldWitnesses, MongoComparable)]
/// struct User {
/// pub age: i32,
/// }
///
/// let b = expr::empty::<User>();
/// let expr = b.not(b.eq(b.select::<user_fields::Age>(), b.from(18)));
///
/// assert_eq!(
/// expr.into_bson(),
/// bson::doc! { "$not": { "$eq": ["$age", 18] } }.into()
/// );
/// ```
pub fn not(&self, expr: Expr<T, bool>) -> Expr<T, bool> {
unary_op("$not", expr)
}
/// Sums numeric expressions with MongoDB's `$add`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::FieldWitnesses;
///
/// #[derive(FieldWitnesses)]
/// struct User {
/// pub age: i32,
/// pub score: i32,
/// }
///
/// let b = expr::empty::<User>();
/// let expr = b.add(b.select::<user_fields::Age>(), vec![b.select::<user_fields::Score>()]);
///
/// assert_eq!(expr.into_bson(), bson::doc! { "$add": ["$age", "$score"] }.into());
/// ```
pub fn add<N>(&self, head: Expr<T, N>, tail: Vec<Expr<T, N>>) -> Expr<T, N>
where
N: Num,
{
nary_op("$add", head, tail)
}
/// Subtracts two numeric expressions with MongoDB's `$subtract`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::FieldWitnesses;
///
/// #[derive(FieldWitnesses)]
/// struct User {
/// pub score: i32,
/// }
///
/// let b = expr::empty::<User>();
/// let expr = b.subtract(b.select::<user_fields::Score>(), b.from(5));
///
/// assert_eq!(expr.into_bson(), bson::doc! { "$subtract": ["$score", 5] }.into());
/// ```
pub fn subtract<N>(&self, minuend: Expr<T, N>, subtrahend: Expr<T, N>) -> Expr<T, N>
where
N: Num,
{
binary_op("$subtract", minuend, subtrahend)
}
/// Multiplies numeric expressions with MongoDB's `$multiply`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::FieldWitnesses;
///
/// #[derive(FieldWitnesses)]
/// struct Item {
/// pub price: i32,
/// pub quantity: i32,
/// }
///
/// let b = expr::empty::<Item>();
/// let expr = b.multiply(
/// b.select::<item_fields::Price>(),
/// vec![b.select::<item_fields::Quantity>()],
/// );
///
/// assert_eq!(expr.into_bson(), bson::doc! { "$multiply": ["$price", "$quantity"] }.into());
/// ```
pub fn multiply<N>(&self, head: Expr<T, N>, tail: Vec<Expr<T, N>>) -> Expr<T, N>
where
N: Num,
{
nary_op("$multiply", head, tail)
}
/// Divides two numeric expressions with MongoDB's `$divide`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::FieldWitnesses;
///
/// #[derive(FieldWitnesses)]
/// struct Item {
/// pub total: i32,
/// pub count: i32,
/// }
///
/// let b = expr::empty::<Item>();
/// let expr = b.divide(b.select::<item_fields::Total>(), b.select::<item_fields::Count>());
///
/// assert_eq!(expr.into_bson(), bson::doc! { "$divide": ["$total", "$count"] }.into());
/// ```
pub fn divide<N>(&self, dividend: Expr<T, N>, divisor: Expr<T, N>) -> Expr<T, N>
where
N: Num,
{
binary_op("$divide", dividend, divisor)
}
/// Computes the modulus of two numeric expressions with MongoDB's `$mod`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::FieldWitnesses;
///
/// #[derive(FieldWitnesses)]
/// struct User {
/// pub age: i32,
/// }
///
/// let b = expr::empty::<User>();
/// let expr = b.modulo(b.select::<user_fields::Age>(), b.from(2));
///
/// assert_eq!(expr.into_bson(), bson::doc! { "$mod": ["$age", 2] }.into());
/// ```
pub fn modulo<N>(&self, dividend: Expr<T, N>, divisor: Expr<T, N>) -> Expr<T, N>
where
N: Num,
{
binary_op("$mod", dividend, divisor)
}
/// Builds a conditional expression with MongoDB's `$cond`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::{FieldWitnesses, MongoComparable};
///
/// #[derive(FieldWitnesses, MongoComparable)]
/// struct User {
/// pub age: i32,
/// }
///
/// let b = expr::empty::<User>();
/// let expr = b.cond(
/// b.gte(b.select::<user_fields::Age>(), b.from(18)),
/// b.from("adult".to_string()),
/// b.from("minor".to_string()),
/// );
///
/// assert_eq!(
/// expr.into_bson(),
/// bson::doc! {
/// "$cond": {
/// "if": { "$gte": ["$age", 18] },
/// "then": "adult",
/// "else": "minor"
/// }
/// }
/// .into()
/// );
/// ```
pub fn cond<V>(
&self,
condition: Expr<T, bool>,
if_true: Expr<T, V>,
if_false: Expr<T, V>,
) -> Expr<T, V> {
let doc = bson::doc! {
"$cond": {
"if": condition.into_bson(),
"then": if_true.into_bson(),
"else": if_false.into_bson(),
}
};
Expr::unsafe_raw(doc.into())
}
/// Returns `replacement` when `expr` is null using MongoDB's `$ifNull`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::FieldWitnesses;
///
/// #[derive(FieldWitnesses)]
/// struct User {
/// pub nickname: String,
/// }
///
/// let b = expr::empty::<User>();
/// let expr = b.if_null(
/// b.select::<user_fields::Nickname>(),
/// b.from("anonymous".to_string()),
/// );
///
/// assert_eq!(
/// expr.into_bson(),
/// bson::doc! { "$ifNull": ["$nickname", "anonymous"] }.into()
/// );
/// ```
pub fn if_null<V>(&self, expr: Expr<T, V>, replacement: Expr<T, V>) -> Expr<T, V> {
binary_op("$ifNull", expr, replacement)
}
/// Concatenates string expressions with MongoDB's `$concat`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::FieldWitnesses;
///
/// #[derive(FieldWitnesses)]
/// struct User {
/// pub first_name: String,
/// pub last_name: String,
/// }
///
/// let b = expr::empty::<User>();
/// let expr = b.concat(
/// b.select::<user_fields::FirstName>(),
/// vec![b.from(" ".to_string()), b.select::<user_fields::LastName>()],
/// );
///
/// assert_eq!(
/// expr.into_bson(),
/// bson::doc! { "$concat": ["$first_name", " ", "$last_name"] }.into()
/// );
/// ```
pub fn concat(&self, head: Expr<T, String>, tail: Vec<Expr<T, String>>) -> Expr<T, String> {
nary_op("$concat", head, tail)
}
/// Uppercases a string expression using MongoDB's `$toUpper`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::FieldWitnesses;
///
/// #[derive(FieldWitnesses)]
/// struct User {
/// pub name: String,
/// }
///
/// let b = expr::empty::<User>();
/// let expr = b.to_upper(b.select::<user_fields::Name>());
///
/// assert_eq!(expr.into_bson(), bson::doc! { "$toUpper": "$name" }.into());
/// ```
pub fn to_upper(&self, expr: Expr<T, String>) -> Expr<T, String> {
unary_op("$toUpper", expr)
}
/// Lowercases a string expression using MongoDB's `$toLower`.
///
/// # Example
///
/// ```rust
/// use tnuctipun::expr;
/// use tnuctipun::FieldWitnesses;
///
/// #[derive(FieldWitnesses)]
/// struct User {
/// pub name: String,
/// }
///
/// let b = expr::empty::<User>();
/// let expr = b.to_lower(b.select::<user_fields::Name>());
///
/// assert_eq!(expr.into_bson(), bson::doc! { "$toLower": "$name" }.into());
/// ```
pub fn to_lower(&self, expr: Expr<T, String>) -> Expr<T, String> {
unary_op("$toLower", expr)
}
}
/// Creates an empty expression builder.
pub fn empty<T>() -> ExprBuilder<T> {
ExprBuilder::new()
}
fn unary_op<T, A, R>(operator: &str, expr: Expr<T, A>) -> Expr<T, R> {
let doc = bson::doc! { operator: expr.into_bson() };
Expr::unsafe_raw(doc.into())
}
fn binary_op<T, A, B, R>(operator: &str, expr1: Expr<T, A>, expr2: Expr<T, B>) -> Expr<T, R> {
let doc = bson::doc! { operator: [expr1.into_bson(), expr2.into_bson()] };
Expr::unsafe_raw(doc.into())
}
fn nary_op<T, A, R>(operator: &str, head: Expr<T, A>, tail: Vec<Expr<T, A>>) -> Expr<T, R> {
let mut all = Vec::with_capacity(1 + tail.len());
all.push(head.into_bson());
all.extend(tail.into_iter().map(Expr::into_bson));
let doc = bson::doc! { operator: all };
Expr::unsafe_raw(doc.into())
}