dcbor-pattern 0.11.1

Pattern matcher for dCBOR
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
use std::ops::RangeInclusive;

use dcbor::prelude::*;

use crate::pattern::{Matcher, Path, Pattern, vm::Instr};

/// Pattern for matching number values in dCBOR.
#[derive(Debug, Clone)]
pub enum NumberPattern {
    /// Matches any number.
    Any,
    /// Matches the exact number.
    Value(f64),
    /// Matches numbers within a range, inclusive (..=).
    Range(RangeInclusive<f64>),
    /// Matches numbers that are greater than the specified value.
    GreaterThan(f64),
    /// Matches numbers that are greater than or equal to the specified value.
    GreaterThanOrEqual(f64),
    /// Matches numbers that are less than the specified value.
    LessThan(f64),
    /// Matches numbers that are less than or equal to the specified value.
    LessThanOrEqual(f64),
    /// Matches numbers that are NaN (Not a Number).
    NaN,
    /// Matches positive infinity.
    Infinity,
    /// Matches negative infinity.
    NegInfinity,
}

impl std::hash::Hash for NumberPattern {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        match self {
            NumberPattern::Any => 0u8.hash(state),
            NumberPattern::Value(value) => {
                1u8.hash(state);
                value.to_bits().hash(state);
            }
            NumberPattern::Range(range) => {
                2u8.hash(state);
                range.start().to_bits().hash(state);
                range.end().to_bits().hash(state);
            }
            NumberPattern::GreaterThan(value) => {
                3u8.hash(state);
                value.to_bits().hash(state);
            }
            NumberPattern::GreaterThanOrEqual(value) => {
                4u8.hash(state);
                value.to_bits().hash(state);
            }
            NumberPattern::LessThan(value) => {
                5u8.hash(state);
                value.to_bits().hash(state);
            }
            NumberPattern::LessThanOrEqual(value) => {
                6u8.hash(state);
                value.to_bits().hash(state);
            }
            NumberPattern::NaN => 7u8.hash(state),
            NumberPattern::Infinity => 8u8.hash(state),
            NumberPattern::NegInfinity => 9u8.hash(state),
        }
    }
}

impl PartialEq for NumberPattern {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (NumberPattern::Any, NumberPattern::Any) => true,
            (NumberPattern::Value(a), NumberPattern::Value(b)) => a == b,
            (NumberPattern::Range(a), NumberPattern::Range(b)) => a == b,
            (NumberPattern::GreaterThan(a), NumberPattern::GreaterThan(b)) => {
                a == b
            }
            (
                NumberPattern::GreaterThanOrEqual(a),
                NumberPattern::GreaterThanOrEqual(b),
            ) => a == b,
            (NumberPattern::LessThan(a), NumberPattern::LessThan(b)) => a == b,
            (
                NumberPattern::LessThanOrEqual(a),
                NumberPattern::LessThanOrEqual(b),
            ) => a == b,
            (NumberPattern::NaN, NumberPattern::NaN) => true,
            (NumberPattern::Infinity, NumberPattern::Infinity) => true,
            (NumberPattern::NegInfinity, NumberPattern::NegInfinity) => true,
            _ => false,
        }
    }
}

impl Eq for NumberPattern {}

impl NumberPattern {
    /// Creates a new `NumberPattern` that matches any number.
    pub fn any() -> Self { NumberPattern::Any }

    /// Creates a new `NumberPattern` that matches the exact number.
    pub fn value<T>(value: T) -> Self
    where
        T: Into<f64>,
    {
        NumberPattern::Value(value.into())
    }

    /// Creates a new `NumberPattern` that matches numbers within the specified
    /// range.
    pub fn range<A>(range: RangeInclusive<A>) -> Self
    where
        A: Into<f64> + Copy,
    {
        let start = (*range.start()).into();
        let end = (*range.end()).into();
        NumberPattern::Range(RangeInclusive::new(start, end))
    }

    /// Creates a new `NumberPattern` that matches numbers greater than the
    /// specified value.
    pub fn greater_than<T>(value: T) -> Self
    where
        T: Into<f64>,
    {
        NumberPattern::GreaterThan(value.into())
    }

    /// Creates a new `NumberPattern` that matches numbers greater than or
    /// equal to the specified value.
    pub fn greater_than_or_equal<T>(value: T) -> Self
    where
        T: Into<f64>,
    {
        NumberPattern::GreaterThanOrEqual(value.into())
    }

    /// Creates a new `NumberPattern` that matches numbers less than the
    /// specified value.
    pub fn less_than<T>(value: T) -> Self
    where
        T: Into<f64>,
    {
        NumberPattern::LessThan(value.into())
    }

    /// Creates a new `NumberPattern` that matches numbers less than or equal
    /// to the specified value.
    pub fn less_than_or_equal<T>(value: T) -> Self
    where
        T: Into<f64>,
    {
        NumberPattern::LessThanOrEqual(value.into())
    }

    /// Creates a new `NumberPattern` that matches NaN values.
    pub fn nan() -> Self { NumberPattern::NaN }

    /// Creates a new `NumberPattern` that matches positive infinity.
    pub fn infinity() -> Self { NumberPattern::Infinity }

    /// Creates a new `NumberPattern` that matches negative infinity.
    pub fn neg_infinity() -> Self { NumberPattern::NegInfinity }
}

impl Matcher for NumberPattern {
    fn paths(&self, haystack: &CBOR) -> Vec<Path> {
        let is_hit = match self {
            NumberPattern::Any => haystack.is_number(),
            NumberPattern::Value(want) => {
                if let Ok(value) = f64::try_from_cbor(haystack) {
                    value == *want
                } else {
                    false
                }
            }
            NumberPattern::Range(want) => {
                if let Ok(value) = f64::try_from_cbor(haystack) {
                    want.contains(&value)
                } else {
                    false
                }
            }
            NumberPattern::GreaterThan(want) => {
                if let Ok(value) = f64::try_from_cbor(haystack) {
                    value > *want
                } else {
                    false
                }
            }
            NumberPattern::GreaterThanOrEqual(want) => {
                if let Ok(value) = f64::try_from_cbor(haystack) {
                    value >= *want
                } else {
                    false
                }
            }
            NumberPattern::LessThan(want) => {
                if let Ok(value) = f64::try_from_cbor(haystack) {
                    value < *want
                } else {
                    false
                }
            }
            NumberPattern::LessThanOrEqual(want) => {
                if let Ok(value) = f64::try_from_cbor(haystack) {
                    value <= *want
                } else {
                    false
                }
            }
            NumberPattern::NaN => {
                if let Ok(value) = f64::try_from_cbor(haystack) {
                    value.is_nan()
                } else {
                    false
                }
            }
            NumberPattern::Infinity => {
                if let Ok(value) = f64::try_from_cbor(haystack) {
                    value.is_infinite() && value.is_sign_positive()
                } else {
                    false
                }
            }
            NumberPattern::NegInfinity => {
                if let Ok(value) = f64::try_from_cbor(haystack) {
                    value.is_infinite() && value.is_sign_negative()
                } else {
                    false
                }
            }
        };

        if is_hit {
            vec![vec![haystack.clone()]]
        } else {
            vec![]
        }
    }

    fn compile(
        &self,
        code: &mut Vec<Instr>,
        literals: &mut Vec<Pattern>,
        _captures: &mut Vec<String>,
    ) {
        let idx = literals.len();
        literals.push(Pattern::Value(crate::pattern::ValuePattern::Number(
            self.clone(),
        )));
        code.push(Instr::MatchPredicate(idx));
    }
}

impl std::fmt::Display for NumberPattern {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NumberPattern::Any => write!(f, "number"),
            NumberPattern::Value(value) => write!(f, "{}", value),
            NumberPattern::Range(range) => {
                write!(f, "{}...{}", range.start(), range.end())
            }
            NumberPattern::GreaterThan(value) => {
                write!(f, ">{}", value)
            }
            NumberPattern::GreaterThanOrEqual(value) => {
                write!(f, ">={}", value)
            }
            NumberPattern::LessThan(value) => write!(f, "<{}", value),
            NumberPattern::LessThanOrEqual(value) => {
                write!(f, "<={}", value)
            }
            NumberPattern::NaN => write!(f, "NaN"),
            NumberPattern::Infinity => write!(f, "Infinity"),
            NumberPattern::NegInfinity => write!(f, "-Infinity"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_number_pattern_display() {
        assert_eq!(NumberPattern::any().to_string(), "number");
        assert_eq!(NumberPattern::value(42.0).to_string(), "42");
        assert_eq!(NumberPattern::range(1.0..=10.0).to_string(), "1...10");
        assert_eq!(NumberPattern::greater_than(5.0).to_string(), ">5");
        assert_eq!(
            NumberPattern::greater_than_or_equal(5.0).to_string(),
            ">=5"
        );
        assert_eq!(NumberPattern::less_than(5.0).to_string(), "<5");
        assert_eq!(NumberPattern::less_than_or_equal(5.0).to_string(), "<=5");
        assert_eq!(NumberPattern::nan().to_string(), "NaN");
        assert_eq!(NumberPattern::infinity().to_string(), "Infinity");
        assert_eq!(NumberPattern::neg_infinity().to_string(), "-Infinity");
    }

    #[test]
    fn test_number_pattern_matching() {
        let int_cbor = 42.to_cbor();
        let float_cbor = 3.2222.to_cbor();
        let negative_cbor = (-10).to_cbor();
        let nan_cbor = f64::NAN.to_cbor();
        let text_cbor = "not a number".to_cbor();

        // Test Any pattern
        let any_pattern = NumberPattern::any();
        assert!(any_pattern.matches(&int_cbor));
        assert!(any_pattern.matches(&float_cbor));
        assert!(any_pattern.matches(&negative_cbor));
        assert!(any_pattern.matches(&nan_cbor));
        assert!(!any_pattern.matches(&text_cbor));

        // Test exact patterns
        let exact_pattern = NumberPattern::value(42.0);
        assert!(exact_pattern.matches(&int_cbor));
        assert!(!exact_pattern.matches(&float_cbor));
        assert!(!exact_pattern.matches(&text_cbor));

        // Test range pattern
        let range_pattern = NumberPattern::range(1.0..=50.0);
        assert!(range_pattern.matches(&int_cbor));
        assert!(range_pattern.matches(&float_cbor));
        assert!(!range_pattern.matches(&negative_cbor));
        assert!(!range_pattern.matches(&text_cbor));

        // Test comparison patterns
        let gt_pattern = NumberPattern::greater_than(10.0);
        assert!(gt_pattern.matches(&int_cbor));
        assert!(!gt_pattern.matches(&float_cbor));
        assert!(!gt_pattern.matches(&negative_cbor));

        let lt_pattern = NumberPattern::less_than(50.0);
        assert!(lt_pattern.matches(&int_cbor));
        assert!(lt_pattern.matches(&float_cbor));
        assert!(lt_pattern.matches(&negative_cbor));

        // Test NaN pattern
        let nan_pattern = NumberPattern::nan();
        assert!(!nan_pattern.matches(&int_cbor));
        assert!(!nan_pattern.matches(&float_cbor));
        assert!(nan_pattern.matches(&nan_cbor));
        assert!(!nan_pattern.matches(&text_cbor));
    }

    #[test]
    fn test_number_pattern_paths() {
        let int_cbor = 42.to_cbor();
        let text_cbor = "not a number".to_cbor();

        let any_pattern = NumberPattern::any();
        let int_paths = any_pattern.paths(&int_cbor);
        assert_eq!(int_paths.len(), 1);
        assert_eq!(int_paths[0].len(), 1);
        assert_eq!(int_paths[0][0], int_cbor);

        let text_paths = any_pattern.paths(&text_cbor);
        assert_eq!(text_paths.len(), 0);

        let exact_pattern = NumberPattern::value(42.0);
        let paths = exact_pattern.paths(&int_cbor);
        assert_eq!(paths.len(), 1);
        assert_eq!(paths[0].len(), 1);
        assert_eq!(paths[0][0], int_cbor);

        let no_match_paths = exact_pattern.paths(&text_cbor);
        assert_eq!(no_match_paths.len(), 0);
    }

    #[test]
    fn test_number_conversion() {
        let int_cbor = 42.to_cbor();
        let float_cbor = 3.2222.to_cbor();
        let negative_cbor = (-10).to_cbor();
        let text_cbor = "not a number".to_cbor();

        // Test direct conversion using try_from_cbor
        assert_eq!(f64::try_from_cbor(&int_cbor).ok(), Some(42.0));
        assert_eq!(f64::try_from_cbor(&float_cbor).ok(), Some(3.2222));
        assert_eq!(f64::try_from_cbor(&negative_cbor).ok(), Some(-10.0));
        assert_eq!(f64::try_from_cbor(&text_cbor).ok(), None);
    }

    #[test]
    fn test_infinity_patterns() {
        let inf_cbor = f64::INFINITY.to_cbor();
        let neg_inf_cbor = f64::NEG_INFINITY.to_cbor();
        let nan_cbor = f64::NAN.to_cbor();
        let regular_cbor = 42.0.to_cbor();
        let text_cbor = "not a number".to_cbor();

        // Test positive infinity pattern
        let inf_pattern = NumberPattern::infinity();
        assert!(inf_pattern.matches(&inf_cbor));
        assert!(!inf_pattern.matches(&neg_inf_cbor));
        assert!(!inf_pattern.matches(&nan_cbor));
        assert!(!inf_pattern.matches(&regular_cbor));
        assert!(!inf_pattern.matches(&text_cbor));

        // Test negative infinity pattern
        let neg_inf_pattern = NumberPattern::neg_infinity();
        assert!(!neg_inf_pattern.matches(&inf_cbor));
        assert!(neg_inf_pattern.matches(&neg_inf_cbor));
        assert!(!neg_inf_pattern.matches(&nan_cbor));
        assert!(!neg_inf_pattern.matches(&regular_cbor));
        assert!(!neg_inf_pattern.matches(&text_cbor));

        // Test that any pattern matches all number types including infinities
        let any_pattern = NumberPattern::any();
        assert!(any_pattern.matches(&inf_cbor));
        assert!(any_pattern.matches(&neg_inf_cbor));
        assert!(any_pattern.matches(&nan_cbor));
        assert!(any_pattern.matches(&regular_cbor));
        assert!(!any_pattern.matches(&text_cbor));

        // Test display formatting
        assert_eq!(inf_pattern.to_string(), "Infinity");
        assert_eq!(neg_inf_pattern.to_string(), "-Infinity");
    }
}