qubit-metadata 0.3.0

Type-safe extensible metadata model for the Qubit LLM SDK
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
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
/*******************************************************************************
 *
 *    Copyright (c) 2025 - 2026.
 *    Haixing Hu, Qubit Co. Ltd.
 *
 *    All rights reserved.
 *
 ******************************************************************************/
//! [`MetadataFilterBuilder`] — fluent builder for composable filters.

use qubit_value::{Value, ValueConstructor};

use crate::{
    Condition, FilterMatchOptions, MetadataFilter, MetadataResult, MetadataSchema,
    MissingKeyPolicy, NumberComparisonPolicy, metadata_filter::FilterExpr,
};

/// Builder for [`MetadataFilter`].
///
/// Predicates without an explicit connector (`eq`, `gt`, `exists`, and so on)
/// are appended with logical AND. Use `or_*` methods or group methods for more
/// complex expressions.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct MetadataFilterBuilder {
    /// Root expression being built. `None` means match all.
    pub(crate) expr: Option<FilterExpr>,
    /// Match policies copied into the built filter.
    pub(crate) options: FilterMatchOptions,
}

impl MetadataFilterBuilder {
    /// Builds an immutable [`MetadataFilter`].
    #[inline]
    #[must_use]
    pub fn build(self) -> MetadataFilter {
        MetadataFilter::new(self.expr, self.options)
    }

    /// Builds an immutable filter and validates it against `schema`.
    ///
    /// # Errors
    ///
    /// Returns an error when the filter references unknown schema fields, uses
    /// range operators on non-comparable field types, or compares a field with an
    /// incompatible value type.
    #[inline]
    pub fn build_checked(self, schema: &MetadataSchema) -> MetadataResult<MetadataFilter> {
        let filter = self.build();
        schema.validate_filter(&filter)?;
        Ok(filter)
    }

    /// Replaces the match options used by the built filter.
    #[inline]
    #[must_use]
    pub fn with_options(mut self, options: FilterMatchOptions) -> Self {
        self.options = options;
        self
    }

    /// Sets how the built filter treats missing keys in negative predicates.
    #[inline]
    #[must_use]
    pub fn missing_key_policy(mut self, missing_key_policy: MissingKeyPolicy) -> Self {
        self.options.missing_key_policy = missing_key_policy;
        self
    }

    /// Sets how the built filter handles mixed numeric comparisons.
    #[inline]
    #[must_use]
    pub fn number_comparison_policy(
        mut self,
        number_comparison_policy: NumberComparisonPolicy,
    ) -> Self {
        self.options.number_comparison_policy = number_comparison_policy;
        self
    }

    /// Appends an equality predicate with AND: `key == value`.
    #[inline]
    #[must_use]
    pub fn eq<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.and_eq(key, value)
    }

    /// Appends a not-equal predicate with AND: `key != value`.
    #[inline]
    #[must_use]
    pub fn ne<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.and_ne(key, value)
    }

    /// Appends a less-than predicate with AND: `key < value`.
    #[inline]
    #[must_use]
    pub fn lt<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.and_lt(key, value)
    }

    /// Appends a less-than-or-equal predicate with AND: `key <= value`.
    #[inline]
    #[must_use]
    pub fn le<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.and_le(key, value)
    }

    /// Appends a greater-than predicate with AND: `key > value`.
    #[inline]
    #[must_use]
    pub fn gt<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.and_gt(key, value)
    }

    /// Appends a greater-than-or-equal predicate with AND: `key >= value`.
    #[inline]
    #[must_use]
    pub fn ge<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.and_ge(key, value)
    }

    /// Appends an inclusion predicate with AND: `key` is in `values`.
    #[inline]
    #[must_use]
    pub fn in_set<I, T>(self, key: &str, values: I) -> Self
    where
        I: IntoIterator<Item = T>,
        Value: ValueConstructor<T>,
    {
        self.and_in_set(key, values)
    }

    /// Appends an exclusion predicate with AND: `key` is not in `values`.
    #[inline]
    #[must_use]
    pub fn not_in_set<I, T>(self, key: &str, values: I) -> Self
    where
        I: IntoIterator<Item = T>,
        Value: ValueConstructor<T>,
    {
        self.and_not_in_set(key, values)
    }

    /// Appends an existence predicate with AND.
    #[inline]
    #[must_use]
    pub fn exists(self, key: &str) -> Self {
        self.and_exists(key)
    }

    /// Appends a non-existence predicate with AND.
    #[inline]
    #[must_use]
    pub fn not_exists(self, key: &str) -> Self {
        self.and_not_exists(key)
    }

    /// Appends an equality predicate with AND: `key == value`.
    #[inline]
    #[must_use]
    pub fn and_eq<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.and_condition(Condition::Equal {
            key: key.to_string(),
            value: to_value(value),
        })
    }

    /// Appends a not-equal predicate with AND: `key != value`.
    #[inline]
    #[must_use]
    pub fn and_ne<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.and_condition(Condition::NotEqual {
            key: key.to_string(),
            value: to_value(value),
        })
    }

    /// Appends a less-than predicate with AND: `key < value`.
    #[inline]
    #[must_use]
    pub fn and_lt<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.and_condition(Condition::Less {
            key: key.to_string(),
            value: to_value(value),
        })
    }

    /// Appends a less-than-or-equal predicate with AND: `key <= value`.
    #[inline]
    #[must_use]
    pub fn and_le<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.and_condition(Condition::LessEqual {
            key: key.to_string(),
            value: to_value(value),
        })
    }

    /// Appends a greater-than predicate with AND: `key > value`.
    #[inline]
    #[must_use]
    pub fn and_gt<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.and_condition(Condition::Greater {
            key: key.to_string(),
            value: to_value(value),
        })
    }

    /// Appends a greater-than-or-equal predicate with AND: `key >= value`.
    #[inline]
    #[must_use]
    pub fn and_ge<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.and_condition(Condition::GreaterEqual {
            key: key.to_string(),
            value: to_value(value),
        })
    }

    /// Appends an inclusion predicate with AND: `key` is in `values`.
    #[inline]
    #[must_use]
    pub fn and_in_set<I, T>(self, key: &str, values: I) -> Self
    where
        I: IntoIterator<Item = T>,
        Value: ValueConstructor<T>,
    {
        self.and_condition(Condition::In {
            key: key.to_string(),
            values: Self::collect_values(values),
        })
    }

    /// Appends an exclusion predicate with AND: `key` is not in `values`.
    #[inline]
    #[must_use]
    pub fn and_not_in_set<I, T>(self, key: &str, values: I) -> Self
    where
        I: IntoIterator<Item = T>,
        Value: ValueConstructor<T>,
    {
        self.and_condition(Condition::NotIn {
            key: key.to_string(),
            values: Self::collect_values(values),
        })
    }

    /// Appends an existence predicate with AND.
    #[inline]
    #[must_use]
    pub fn and_exists(self, key: &str) -> Self {
        self.and_condition(Condition::Exists {
            key: key.to_string(),
        })
    }

    /// Appends a non-existence predicate with AND.
    #[inline]
    #[must_use]
    pub fn and_not_exists(self, key: &str) -> Self {
        self.and_condition(Condition::NotExists {
            key: key.to_string(),
        })
    }

    /// Appends an equality predicate with OR: `key == value`.
    #[inline]
    #[must_use]
    pub fn or_eq<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.or_condition(Condition::Equal {
            key: key.to_string(),
            value: to_value(value),
        })
    }

    /// Appends a not-equal predicate with OR: `key != value`.
    #[inline]
    #[must_use]
    pub fn or_ne<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.or_condition(Condition::NotEqual {
            key: key.to_string(),
            value: to_value(value),
        })
    }

    /// Appends a less-than predicate with OR: `key < value`.
    #[inline]
    #[must_use]
    pub fn or_lt<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.or_condition(Condition::Less {
            key: key.to_string(),
            value: to_value(value),
        })
    }

    /// Appends a less-than-or-equal predicate with OR: `key <= value`.
    #[inline]
    #[must_use]
    pub fn or_le<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.or_condition(Condition::LessEqual {
            key: key.to_string(),
            value: to_value(value),
        })
    }

    /// Appends a greater-than predicate with OR: `key > value`.
    #[inline]
    #[must_use]
    pub fn or_gt<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.or_condition(Condition::Greater {
            key: key.to_string(),
            value: to_value(value),
        })
    }

    /// Appends a greater-than-or-equal predicate with OR: `key >= value`.
    #[inline]
    #[must_use]
    pub fn or_ge<T>(self, key: &str, value: T) -> Self
    where
        Value: ValueConstructor<T>,
    {
        self.or_condition(Condition::GreaterEqual {
            key: key.to_string(),
            value: to_value(value),
        })
    }

    /// Appends an inclusion predicate with OR: `key` is in `values`.
    #[inline]
    #[must_use]
    pub fn or_in_set<I, T>(self, key: &str, values: I) -> Self
    where
        I: IntoIterator<Item = T>,
        Value: ValueConstructor<T>,
    {
        self.or_condition(Condition::In {
            key: key.to_string(),
            values: Self::collect_values(values),
        })
    }

    /// Appends an exclusion predicate with OR: `key` is not in `values`.
    #[inline]
    #[must_use]
    pub fn or_not_in_set<I, T>(self, key: &str, values: I) -> Self
    where
        I: IntoIterator<Item = T>,
        Value: ValueConstructor<T>,
    {
        self.or_condition(Condition::NotIn {
            key: key.to_string(),
            values: Self::collect_values(values),
        })
    }

    /// Appends an existence predicate with OR.
    #[inline]
    #[must_use]
    pub fn or_exists(self, key: &str) -> Self {
        self.or_condition(Condition::Exists {
            key: key.to_string(),
        })
    }

    /// Appends a non-existence predicate with OR.
    #[inline]
    #[must_use]
    pub fn or_not_exists(self, key: &str) -> Self {
        self.or_condition(Condition::NotExists {
            key: key.to_string(),
        })
    }

    /// Appends a grouped expression with AND.
    ///
    /// The closure receives a fresh builder for the group. Policies configured
    /// inside the group are ignored; configure policies on the outer builder.
    #[inline]
    #[must_use]
    pub fn and<F>(self, build: F) -> Self
    where
        F: FnOnce(Self) -> Self,
    {
        let group = build(Self::default()).expr;
        self.and_expr(group)
    }

    /// Appends a grouped expression with OR.
    ///
    /// The closure receives a fresh builder for the group. Policies configured
    /// inside the group are ignored; configure policies on the outer builder.
    #[inline]
    #[must_use]
    pub fn or<F>(self, build: F) -> Self
    where
        F: FnOnce(Self) -> Self,
    {
        let group = build(Self::default()).expr;
        self.or_expr(group)
    }

    /// Appends a negated grouped expression with AND.
    #[inline]
    #[must_use]
    pub fn and_not<F>(self, build: F) -> Self
    where
        F: FnOnce(Self) -> Self,
    {
        let group = build(Self::default()).expr;
        self.and_expr(Self::negate_expr(group))
    }

    /// Appends a negated grouped expression with OR.
    #[inline]
    #[must_use]
    pub fn or_not<F>(self, build: F) -> Self
    where
        F: FnOnce(Self) -> Self,
    {
        let group = build(Self::default()).expr;
        self.or_expr(Self::negate_expr(group))
    }

    /// Negates the entire builder expression.
    #[allow(clippy::should_implement_trait)]
    #[inline]
    #[must_use]
    pub fn not(mut self) -> Self {
        self.expr = Self::negate_expr(self.expr);
        self
    }

    /// Converts a sequence of typed values into stored values.
    #[inline]
    fn collect_values<I, T>(values: I) -> Vec<Value>
    where
        I: IntoIterator<Item = T>,
        Value: ValueConstructor<T>,
    {
        values.into_iter().map(to_value).collect()
    }

    /// Combines the current expression with `expr` using logical AND.
    #[inline]
    fn and_expr(mut self, expr: Option<FilterExpr>) -> Self {
        self.expr = match (self.expr, expr) {
            (None, rhs) => rhs,
            (lhs, None) => lhs,
            (Some(lhs), Some(rhs)) => Some(FilterExpr::and(lhs, rhs)),
        };
        self
    }

    /// Combines the current expression with `expr` using logical OR.
    #[inline]
    fn or_expr(mut self, expr: Option<FilterExpr>) -> Self {
        self.expr = match (self.expr, expr) {
            (None, rhs) => rhs,
            (lhs, None) => lhs,
            (Some(lhs), Some(rhs)) => Some(FilterExpr::or(lhs, rhs)),
        };
        self
    }

    /// Appends a condition with logical AND.
    #[inline]
    fn and_condition(self, condition: Condition) -> Self {
        self.and_expr(Some(FilterExpr::Condition(condition)))
    }

    /// Appends a condition with logical OR.
    #[inline]
    fn or_condition(self, condition: Condition) -> Self {
        self.or_expr(Some(FilterExpr::Condition(condition)))
    }

    /// Negates an optional expression.
    #[inline]
    pub(crate) fn negate_expr(expr: Option<FilterExpr>) -> Option<FilterExpr> {
        match expr {
            None => Some(FilterExpr::False),
            Some(FilterExpr::False) => None,
            Some(FilterExpr::Not(inner)) => Some(*inner),
            Some(other) => Some(FilterExpr::Not(Box::new(other))),
        }
    }
}

#[inline]
fn to_value<T>(value: T) -> Value
where
    Value: ValueConstructor<T>,
{
    <Value as ValueConstructor<T>>::from_type(value)
}