qubit-metadata 0.1.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
/*******************************************************************************
 *
 *    Copyright (c) 2025 - 2026.
 *    Haixing Hu, Qubit Co. Ltd.
 *
 *    All rights reserved.
 *
 ******************************************************************************/
//! Provides [`MetadataFilter`] — composable filter expressions for
//! metadata-based queries.
//!
//! A [`MetadataFilter`] can be used to select [`Metadata`] instances that
//! satisfy a set of conditions.  Conditions can be combined with logical
//! operators (`and`, `or`, `not`) to form arbitrarily complex predicates.
//!
//! # Examples
//!
//! ```rust
//! use qubit_metadata::{Metadata, MetadataFilter};
//!
//! let mut meta = Metadata::new();
//! meta.set("status", "active");
//! meta.set("score", 42_i64);
//!
//! let filter = MetadataFilter::eq("status", "active")
//!     .and(MetadataFilter::gte("score", 10_i64));
//!
//! assert!(filter.matches(&meta));
//! ```

use serde::{
    Deserialize,
    Serialize,
};
use std::cmp::Ordering;

use serde_json::{
    Number,
    Value,
};

use crate::Metadata;

/// A single comparison operator applied to one metadata key.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Condition {
    /// Key equals value.
    Eq {
        /// The metadata key.
        key: String,
        /// The expected value.
        value: Value,
    },
    /// Key does not equal value.
    Ne {
        /// The metadata key.
        key: String,
        /// The value to compare against.
        value: Value,
    },
    /// Key is greater than value (numeric / string comparison).
    Gt {
        /// The metadata key.
        key: String,
        /// The lower bound (exclusive).
        value: Value,
    },
    /// Key is greater than or equal to value.
    Gte {
        /// The metadata key.
        key: String,
        /// The lower bound (inclusive).
        value: Value,
    },
    /// Key is less than value.
    Lt {
        /// The metadata key.
        key: String,
        /// The upper bound (exclusive).
        value: Value,
    },
    /// Key is less than or equal to value.
    Lte {
        /// The metadata key.
        key: String,
        /// The upper bound (inclusive).
        value: Value,
    },
    /// Key exists in the metadata (regardless of its value).
    Exists {
        /// The metadata key.
        key: String,
    },
    /// Key does not exist in the metadata.
    NotExists {
        /// The metadata key.
        key: String,
    },
    /// The stored value is one of the listed candidates.
    In {
        /// The metadata key.
        key: String,
        /// The set of acceptable values.
        values: Vec<Value>,
    },
    /// The stored value is not any of the listed candidates.
    NotIn {
        /// The metadata key.
        key: String,
        /// The set of excluded values.
        values: Vec<Value>,
    },
}

impl Condition {
    fn matches(&self, meta: &Metadata) -> bool {
        match self {
            Condition::Eq { key, value } => meta.get_raw(key) == Some(value),
            Condition::Ne { key, value } => meta.get_raw(key) != Some(value),
            Condition::Gt { key, value } => meta
                .get_raw(key)
                .is_some_and(|v| compare_values(v, value) == Some(Ordering::Greater)),
            Condition::Gte { key, value } => meta.get_raw(key).is_some_and(|v| {
                matches!(
                    compare_values(v, value),
                    Some(Ordering::Greater) | Some(Ordering::Equal)
                )
            }),
            Condition::Lt { key, value } => meta
                .get_raw(key)
                .is_some_and(|v| compare_values(v, value) == Some(Ordering::Less)),
            Condition::Lte { key, value } => meta.get_raw(key).is_some_and(|v| {
                matches!(
                    compare_values(v, value),
                    Some(Ordering::Less) | Some(Ordering::Equal)
                )
            }),
            Condition::Exists { key } => meta.contains_key(key),
            Condition::NotExists { key } => !meta.contains_key(key),
            Condition::In { key, values } => meta.get_raw(key).is_some_and(|v| values.contains(v)),
            Condition::NotIn { key, values } => {
                meta.get_raw(key).map_or(true, |v| !values.contains(v))
            }
        }
    }
}

/// Compares two [`Value`]s where both are the same numeric or string variant.
/// Returns `None` when the values are incomparable (different types).
fn compare_values(a: &Value, b: &Value) -> Option<Ordering> {
    match (a, b) {
        (Value::Number(x), Value::Number(y)) => compare_numbers(x, y),
        (Value::String(x), Value::String(y)) => x.partial_cmp(y),
        _ => None,
    }
}

const MAX_SAFE_INTEGER_F64_U64: u64 = 9_007_199_254_740_992; // 2^53
const I64_MIN_F64: f64 = -9_223_372_036_854_775_808.0; // -2^63
const I64_EXCLUSIVE_MAX_F64: f64 = 9_223_372_036_854_775_808.0; // 2^63
const U64_EXCLUSIVE_MAX_F64: f64 = 18_446_744_073_709_551_616.0; // 2^64

fn compare_numbers(a: &Number, b: &Number) -> Option<Ordering> {
    if let (Some(xi), Some(yi)) = (a.as_i64(), b.as_i64()) {
        return Some(xi.cmp(&yi));
    }
    if let (Some(xi), Some(yu)) = (a.as_i64(), b.as_u64()) {
        return Some(compare_i64_u64(xi, yu));
    }
    if let (Some(xu), Some(yi)) = (a.as_u64(), b.as_i64()) {
        return Some(compare_i64_u64(yi, xu).reverse());
    }
    if let (Some(xu), Some(yu)) = (a.as_u64(), b.as_u64()) {
        return Some(xu.cmp(&yu));
    }
    if let (Some(xi), Some(yf)) = (a.as_i64(), b.as_f64()) {
        return compare_i64_f64(xi, yf);
    }
    if let (Some(xf), Some(yi)) = (a.as_f64(), b.as_i64()) {
        return compare_i64_f64(yi, xf).map(Ordering::reverse);
    }
    if let (Some(xu), Some(yf)) = (a.as_u64(), b.as_f64()) {
        return compare_u64_f64(xu, yf);
    }
    if let (Some(xf), Some(yu)) = (a.as_f64(), b.as_u64()) {
        return compare_u64_f64(yu, xf).map(Ordering::reverse);
    }
    if let (Some(xf), Some(yf)) = (a.as_f64(), b.as_f64()) {
        return xf.partial_cmp(&yf);
    }

    // serde_json::Number always represents one of i64/u64/f64.
    unreachable!("Number must be representable as i64/u64/f64")
}

#[inline]
fn compare_i64_u64(x: i64, y: u64) -> Ordering {
    if x < 0 {
        Ordering::Less
    } else {
        (x as u64).cmp(&y)
    }
}

#[inline]
fn compare_i64_f64(x: i64, y: f64) -> Option<Ordering> {
    if y.fract() == 0.0 && (I64_MIN_F64..I64_EXCLUSIVE_MAX_F64).contains(&y) {
        // Integer-vs-integer path avoids precision loss for values > 2^53.
        return Some(x.cmp(&(y as i64)));
    }

    if x.unsigned_abs() <= MAX_SAFE_INTEGER_F64_U64 {
        return (x as f64).partial_cmp(&y);
    }

    None
}

#[inline]
fn compare_u64_f64(x: u64, y: f64) -> Option<Ordering> {
    if y < 0.0 {
        return Some(Ordering::Greater);
    }

    if y.fract() == 0.0 && (0.0..U64_EXCLUSIVE_MAX_F64).contains(&y) {
        // Integer-vs-integer path avoids precision loss for values > 2^53.
        return Some(x.cmp(&(y as u64)));
    }

    None
}

/// A composable filter expression over [`Metadata`].
///
/// Filters can be built from primitive [`Condition`]s and combined with
/// [`MetadataFilter::and`], [`MetadataFilter::or`], and [`MetadataFilter::not`].
///
/// # Examples
///
/// ```rust
/// use qubit_metadata::{Metadata, MetadataFilter};
///
/// let mut meta = Metadata::new();
/// meta.set("env", "prod");
/// meta.set("version", 2_i64);
///
/// let f = MetadataFilter::eq("env", "prod")
///     .and(MetadataFilter::gte("version", 1_i64));
///
/// assert!(f.matches(&meta));
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum MetadataFilter {
    /// A leaf condition.
    Condition(Condition),
    /// All child filters must match.
    And(Vec<MetadataFilter>),
    /// At least one child filter must match.
    Or(Vec<MetadataFilter>),
    /// The child filter must not match.
    Not(Box<MetadataFilter>),
}

impl MetadataFilter {
    // ── Leaf constructors ────────────────────────────────────────────────────

    /// Creates an equality filter: `key == value`.
    pub fn eq<T: Serialize>(key: impl Into<String>, value: T) -> Self {
        Self::Condition(Condition::Eq {
            key: key.into(),
            value: serde_json::to_value(value)
                .expect("MetadataFilter::eq: value must be serializable"),
        })
    }

    /// Creates a not-equal filter: `key != value`.
    pub fn ne<T: Serialize>(key: impl Into<String>, value: T) -> Self {
        Self::Condition(Condition::Ne {
            key: key.into(),
            value: serde_json::to_value(value)
                .expect("MetadataFilter::ne: value must be serializable"),
        })
    }

    /// Creates a greater-than filter: `key > value`.
    pub fn gt<T: Serialize>(key: impl Into<String>, value: T) -> Self {
        Self::Condition(Condition::Gt {
            key: key.into(),
            value: serde_json::to_value(value)
                .expect("MetadataFilter::gt: value must be serializable"),
        })
    }

    /// Creates a greater-than-or-equal filter: `key >= value`.
    pub fn gte<T: Serialize>(key: impl Into<String>, value: T) -> Self {
        Self::Condition(Condition::Gte {
            key: key.into(),
            value: serde_json::to_value(value)
                .expect("MetadataFilter::gte: value must be serializable"),
        })
    }

    /// Creates a less-than filter: `key < value`.
    pub fn lt<T: Serialize>(key: impl Into<String>, value: T) -> Self {
        Self::Condition(Condition::Lt {
            key: key.into(),
            value: serde_json::to_value(value)
                .expect("MetadataFilter::lt: value must be serializable"),
        })
    }

    /// Creates a less-than-or-equal filter: `key <= value`.
    pub fn lte<T: Serialize>(key: impl Into<String>, value: T) -> Self {
        Self::Condition(Condition::Lte {
            key: key.into(),
            value: serde_json::to_value(value)
                .expect("MetadataFilter::lte: value must be serializable"),
        })
    }

    /// Creates an existence filter: the key must be present.
    pub fn exists(key: impl Into<String>) -> Self {
        Self::Condition(Condition::Exists { key: key.into() })
    }

    /// Creates a non-existence filter: the key must be absent.
    pub fn not_exists(key: impl Into<String>) -> Self {
        Self::Condition(Condition::NotExists { key: key.into() })
    }

    /// Creates an in-set filter: `key ∈ values`.
    pub fn in_values<T, I>(key: impl Into<String>, values: I) -> Self
    where
        T: Serialize,
        I: IntoIterator<Item = T>,
    {
        let values = values
            .into_iter()
            .map(|v| {
                serde_json::to_value(v)
                    .expect("MetadataFilter::in_values: each value must be serializable")
            })
            .collect();
        Self::Condition(Condition::In {
            key: key.into(),
            values,
        })
    }

    /// Creates a not-in-set filter: `key ∉ values`.
    pub fn not_in_values<T, I>(key: impl Into<String>, values: I) -> Self
    where
        T: Serialize,
        I: IntoIterator<Item = T>,
    {
        let values = values
            .into_iter()
            .map(|v| {
                serde_json::to_value(v)
                    .expect("MetadataFilter::not_in_values: each value must be serializable")
            })
            .collect();
        Self::Condition(Condition::NotIn {
            key: key.into(),
            values,
        })
    }

    // ── Logical combinators ──────────────────────────────────────────────────

    /// Combines `self` and `other` with a logical AND.
    ///
    /// If `self` is already an `And` node the new filter is appended to its
    /// children rather than creating a new nested node.
    #[must_use]
    pub fn and(self, other: MetadataFilter) -> Self {
        match self {
            MetadataFilter::And(mut children) => {
                children.push(other);
                MetadataFilter::And(children)
            }
            _ => MetadataFilter::And(vec![self, other]),
        }
    }

    /// Combines `self` and `other` with a logical OR.
    ///
    /// If `self` is already an `Or` node the new filter is appended to its
    /// children rather than creating a new nested node.
    #[must_use]
    pub fn or(self, other: MetadataFilter) -> Self {
        match self {
            MetadataFilter::Or(mut children) => {
                children.push(other);
                MetadataFilter::Or(children)
            }
            _ => MetadataFilter::Or(vec![self, other]),
        }
    }

    /// Wraps `self` in a logical NOT.
    #[allow(clippy::should_implement_trait)]
    #[must_use]
    pub fn not(self) -> Self {
        !self
    }

    // ── Evaluation ───────────────────────────────────────────────────────────

    /// Returns `true` if `meta` satisfies this filter.
    pub fn matches(&self, meta: &Metadata) -> bool {
        match self {
            MetadataFilter::Condition(cond) => cond.matches(meta),
            MetadataFilter::And(children) => children.iter().all(|f| f.matches(meta)),
            MetadataFilter::Or(children) => children.iter().any(|f| f.matches(meta)),
            MetadataFilter::Not(inner) => !inner.matches(meta),
        }
    }
}

impl std::ops::Not for MetadataFilter {
    type Output = MetadataFilter;

    fn not(self) -> Self::Output {
        MetadataFilter::Not(Box::new(self))
    }
}