bc-envelope-pattern 0.14.0

Pattern matcher for Gordian Envelope
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
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
mod common;

use bc_envelope::prelude::*;
use bc_envelope_pattern::{
    FormatPathsOpts, Matcher, Pattern, format_paths_with_captures_opt,
};
use indoc::indoc;

/// Test simple dcbor captures within `cbor` patterns
#[test]
fn test_simple_dcbor_capture() {
    // Create envelope with number 42
    let envelope = Envelope::new(42);

    // Create `cbor` pattern with dcbor capture: /@num(42)/
    let pattern = Pattern::parse("cbor(/@num(42)/)").unwrap();

    // Execute pattern
    let (paths, captures) = pattern.paths_with_captures(&envelope);

    // Verify basic expectations
    assert_eq!(paths.len(), 1, "Should find exactly one path");
    assert_eq!(captures.len(), 1, "Should have exactly one capture group");
    assert!(captures.contains_key("num"), "Should have 'num' capture");
    assert_eq!(captures["num"].len(), 1, "Should capture one instance");

    // Verify formatted output follows rubric
    let actual = format_paths_with_captures_opt(
        &paths,
        &captures,
        FormatPathsOpts::default(),
    );

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        @num
            7f83f7bd LEAF 42
        7f83f7bd LEAF 42
    "#}.trim();

    assert_actual_expected!(
        actual,
        expected,
        "`cbor` pattern should capture and format dcbor captures correctly"
    );
}

/// Test dcbor captures with search patterns
#[test]
fn test_dcbor_capture_with_search() {
    // Create envelope with array [1, 2, 3]
    let envelope = Envelope::new(vec![1, 2, 3]);

    let pattern = Pattern::parse("cbor(/@values(search(number))/)").unwrap();

    // Execute pattern
    let (paths, captures) = pattern.paths_with_captures(&envelope);

    // Verify basic expectations
    assert_eq!(paths.len(), 3, "Should find exactly three paths");
    assert_eq!(captures.len(), 1, "Should have exactly one capture group");
    assert!(
        captures.contains_key("values"),
        "Should have 'values' capture"
    );
    assert_eq!(
        captures["values"].len(),
        3,
        "Should capture three instances (one per number)"
    );

    // Verify formatted output follows rubric
    let actual = format_paths_with_captures_opt(
        &paths,
        &captures,
        FormatPathsOpts::default(),
    );

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        @values
            4abc3113 LEAF [1, 2, 3]
                4bf5122f LEAF 1
            4abc3113 LEAF [1, 2, 3]
                dbc1b4c9 LEAF 2
            4abc3113 LEAF [1, 2, 3]
                084fed08 LEAF 3
        4abc3113 LEAF [1, 2, 3]
            4bf5122f LEAF 1
        4abc3113 LEAF [1, 2, 3]
            dbc1b4c9 LEAF 2
        4abc3113 LEAF [1, 2, 3]
            084fed08 LEAF 3
    "#}
    .trim();

    assert_actual_expected!(
        actual,
        expected,
        "`cbor` search pattern should capture all number matches"
    );
}

/// Test multiple dcbor captures in single pattern
#[test]
fn test_multiple_dcbor_captures() {
    // Create envelope with simple mixed-type array for now
    // This simulates a map-like structure using alternating key-value pairs
    let envelope = Envelope::new(vec!["name", "Alice", "age", "30"]);

    let pattern = Pattern::parse("cbor(/@names(search(text))/)").unwrap();

    // Execute pattern
    let (paths, captures) = pattern.paths_with_captures(&envelope);

    // Verify basic expectations - `search` returns multiple paths (one per text
    // string)
    assert_eq!(
        paths.len(),
        4,
        "Should find exactly four paths (one per text string)"
    );
    assert_eq!(captures.len(), 1, "Should have exactly one capture group");
    assert!(
        captures.contains_key("names"),
        "Should have 'names' capture"
    );
    // Due to `search` behavior, we get one capture per text string found
    assert_eq!(
        captures["names"].len(),
        4,
        "Should capture four instances (one per text string)"
    );

    // Verify formatted output follows rubric
    let actual = format_paths_with_captures_opt(
        &paths,
        &captures,
        FormatPathsOpts::default(),
    );

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        @names
            ce1042d4 LEAF ["name", "Alice", "age", "30"]
                800a0588 LEAF "name"
            ce1042d4 LEAF ["name", "Alice", "age", "30"]
                13941b48 LEAF "Alice"
            ce1042d4 LEAF ["name", "Alice", "age", "30"]
                5943be12 LEAF "age"
            ce1042d4 LEAF ["name", "Alice", "age", "30"]
                08e52634 LEAF "30"
        ce1042d4 LEAF ["name", "Alice", "age", "30"]
            800a0588 LEAF "name"
        ce1042d4 LEAF ["name", "Alice", "age", "30"]
            13941b48 LEAF "Alice"
        ce1042d4 LEAF ["name", "Alice", "age", "30"]
            5943be12 LEAF "age"
        ce1042d4 LEAF ["name", "Alice", "age", "30"]
            08e52634 LEAF "30"
    "#}
    .trim();

    assert_actual_expected!(
        actual,
        expected,
        "`cbor` pattern should capture all text values found"
    );
}

/// Test nested dcbor captures in complex structures
#[test]
fn test_nested_dcbor_captures() {
    // Create envelope with nested array structure: [["Alice", "95"], ["Bob",
    // "85"]]
    let envelope = Envelope::new(vec![vec!["Alice", "95"], vec!["Bob", "85"]]);

    // Create `cbor` pattern with nested captures:
    let pattern =
        Pattern::parse("cbor(/@users(search([@name(text), @score(text)]))/)")
            .unwrap();

    // Execute pattern
    let (paths, captures) = pattern.paths_with_captures(&envelope);

    // Verify basic expectations - `search` returns multiple paths
    assert_eq!(
        paths.len(),
        2,
        "Should find exactly two paths (one per nested array)"
    );
    assert_eq!(
        captures.len(),
        3,
        "Should have exactly three capture groups"
    );
    assert!(
        captures.contains_key("users"),
        "Should have 'users' capture"
    );
    assert!(captures.contains_key("name"), "Should have 'name' capture");
    assert!(
        captures.contains_key("score"),
        "Should have 'score' capture"
    );
    assert_eq!(
        captures["users"].len(),
        2,
        "Should capture two user instances (one per nested array)"
    );
    assert_eq!(
        captures["name"].len(),
        2,
        "Should capture two name instances (one per nested array)"
    );
    assert_eq!(
        captures["score"].len(),
        2,
        "Should capture two score instances (one per nested array)"
    );
    // Verify formatted output follows rubric
    let actual = format_paths_with_captures_opt(
        &paths,
        &captures,
        FormatPathsOpts::default(),
    );

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        @name
            7dfc2858 LEAF [["Alice", "95"], ["Bob", "85"]]
                6daf5539 LEAF ["Alice", "95"]
            7dfc2858 LEAF [["Alice", "95"], ["Bob", "85"]]
                43a6ef66 LEAF ["Bob", "85"]
        @score
            7dfc2858 LEAF [["Alice", "95"], ["Bob", "85"]]
                6daf5539 LEAF ["Alice", "95"]
            7dfc2858 LEAF [["Alice", "95"], ["Bob", "85"]]
                43a6ef66 LEAF ["Bob", "85"]
        @users
            7dfc2858 LEAF [["Alice", "95"], ["Bob", "85"]]
                6daf5539 LEAF ["Alice", "95"]
            7dfc2858 LEAF [["Alice", "95"], ["Bob", "85"]]
                43a6ef66 LEAF ["Bob", "85"]
        7dfc2858 LEAF [["Alice", "95"], ["Bob", "85"]]
            6daf5539 LEAF ["Alice", "95"]
        7dfc2858 LEAF [["Alice", "95"], ["Bob", "85"]]
            43a6ef66 LEAF ["Bob", "85"]
    "#}
    .trim();

    assert_actual_expected!(
        actual,
        expected,
        "`cbor` pattern should handle nested captures correctly"
    );
}

/// Test integration of envelope-level and dcbor-level captures
#[test]
fn test_mixed_envelope_and_dcbor_captures() {
    // Create envelope with number 42
    let envelope = Envelope::new(42);

    // Create pattern with envelope capture wrapping `cbor` pattern with dcbor
    // capture @envelope_level(cbor(/@dcbor_level(42)/))
    let cbor_pattern = Pattern::parse("cbor(/@dcbor_level(42)/)").unwrap();
    let pattern = Pattern::capture("envelope_level", cbor_pattern);

    // Execute pattern
    let (paths, captures) = pattern.paths_with_captures(&envelope);

    // Verify basic expectations
    assert_eq!(paths.len(), 1, "Should find exactly one path");
    assert_eq!(captures.len(), 2, "Should have exactly two capture groups");
    assert!(
        captures.contains_key("envelope_level"),
        "Should have 'envelope_level' capture"
    );
    assert!(
        captures.contains_key("dcbor_level"),
        "Should have 'dcbor_level' capture"
    );
    assert_eq!(
        captures["envelope_level"].len(),
        1,
        "Should capture one envelope instance"
    );
    assert_eq!(
        captures["dcbor_level"].len(),
        1,
        "Should capture one dcbor instance"
    );

    // Verify formatted output follows rubric
    let actual = format_paths_with_captures_opt(
        &paths,
        &captures,
        FormatPathsOpts::default(),
    );

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        @dcbor_level
            7f83f7bd LEAF 42
        @envelope_level
            7f83f7bd LEAF 42
        7f83f7bd LEAF 42
    "#}
    .trim();

    assert_actual_expected!(
        actual,
        expected,
        "Pattern should capture at both envelope and dcbor levels"
    );
}

/// Test capture name uniqueness and conflict detection
#[test]
fn test_capture_name_conflicts() {
    // Create envelope with number 42
    let envelope = Envelope::new(42);

    // Create pattern with same capture name at envelope and dcbor levels
    // @same_name(cbor(/@same_name(42)/))
    let cbor_pattern = Pattern::parse("cbor(/@same_name(42)/)").unwrap();
    let pattern = Pattern::capture("same_name", cbor_pattern);

    // Execute pattern
    let (paths, captures) = pattern.paths_with_captures(&envelope);

    // Verify basic expectations
    assert_eq!(paths.len(), 1, "Should find exactly one path");
    assert_eq!(captures.len(), 1, "Should have exactly one capture group");
    assert!(
        captures.contains_key("same_name"),
        "Should have 'same_name' capture"
    );
    assert_eq!(
        captures["same_name"].len(),
        2,
        "Should capture from both levels"
    );

    // Verify formatted output follows rubric
    let actual = format_paths_with_captures_opt(
        &paths,
        &captures,
        FormatPathsOpts::default(),
    );

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        @same_name
            7f83f7bd LEAF 42
            7f83f7bd LEAF 42
        7f83f7bd LEAF 42
    "#}
    .trim();

    assert_actual_expected!(
        actual,
        expected,
        "Pattern should merge captures with same name from different levels"
    );
}

/// Test `cbor` captures with complex array traversal
#[test]
fn test_array_traversal_captures() {
    // Create envelope with mixed array ["hello", "42", "world", "123"]
    let envelope = Envelope::new(vec!["hello", "42", "world", "123"]); // All strings for simplicity

    let pattern = Pattern::parse("cbor(/@text(search(text))/)").unwrap();

    // Execute pattern
    let (paths, captures) = pattern.paths_with_captures(&envelope);

    // Verify basic expectations - `search` returns multiple paths (one per text
    // element)
    assert_eq!(
        paths.len(),
        4,
        "Should find exactly four paths (one per text element)"
    );
    assert_eq!(captures.len(), 1, "Should have exactly one capture group");
    assert!(captures.contains_key("text"), "Should have 'text' capture");
    assert_eq!(
        captures["text"].len(),
        4,
        "Should capture four instances (one per text element)"
    );

    // Verify formatted output follows rubric
    let actual = format_paths_with_captures_opt(
        &paths,
        &captures,
        FormatPathsOpts::default(),
    );

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        @text
            162867a4 LEAF ["hello", "42", "world", "123"]
                cb835593 LEAF "hello"
            162867a4 LEAF ["hello", "42", "world", "123"]
                9fa6eb00 LEAF "42"
            162867a4 LEAF ["hello", "42", "world", "123"]
                29651e19 LEAF "world"
            162867a4 LEAF ["hello", "42", "world", "123"]
                9bf5bb3e LEAF "123"
        162867a4 LEAF ["hello", "42", "world", "123"]
            cb835593 LEAF "hello"
        162867a4 LEAF ["hello", "42", "world", "123"]
            9fa6eb00 LEAF "42"
        162867a4 LEAF ["hello", "42", "world", "123"]
            29651e19 LEAF "world"
        162867a4 LEAF ["hello", "42", "world", "123"]
            9bf5bb3e LEAF "123"
    "#}
    .trim();

    assert_actual_expected!(
        actual,
        expected,
        "`cbor` pattern should capture all text elements via `search`"
    );
}

/// Test `cbor` captures with no matches
#[test]
fn test_cbor_captures_no_match() {
    // Create envelope with text "hello"
    let envelope = Envelope::new("hello");

    // Create `cbor` pattern that won't match: /@num(number)/
    let pattern = Pattern::parse("cbor(/@num(number)/)").unwrap();

    // Execute pattern
    let (paths, captures) = pattern.paths_with_captures(&envelope);

    // Verify basic expectations
    assert_eq!(paths.len(), 0, "Should find no paths");
    assert_eq!(captures.len(), 0, "Should have no captures");

    // Verify formatted output follows rubric
    let actual = format_paths_with_captures_opt(
        &paths,
        &captures,
        FormatPathsOpts::default(),
    );

    #[rustfmt::skip]
    let expected = "";

    assert_actual_expected!(
        actual,
        expected,
        "`cbor` pattern with no matches should return empty formatted output"
    );
}

/// Test `cbor` pattern performance with many captures
#[test]
fn test_cbor_captures_performance() {
    // Create envelope with smaller array to avoid excessive repetition
    let numbers: Vec<i32> = (1..=3).collect(); // Use just 3 numbers for clarity
    let envelope = Envelope::new(numbers);

    let pattern = Pattern::parse("cbor(/@nums(search(number))/)").unwrap();

    // Execute pattern with timing
    let start = std::time::Instant::now();
    let (paths, captures) = pattern.paths_with_captures(&envelope);
    let duration = start.elapsed();

    // Verify basic expectations - `search` pattern returns multiple paths (one
    // per number)
    assert_eq!(
        paths.len(),
        3,
        "Should find exactly three paths (one per number)"
    );
    assert_eq!(captures.len(), 1, "Should have exactly one capture group");
    assert!(captures.contains_key("nums"), "Should have 'nums' capture");
    // Due to `search` behavior, we get one capture per number
    assert_eq!(
        captures["nums"].len(),
        3,
        "Should capture three instances (one per number)"
    );

    // Verify formatted output follows rubric
    let actual = format_paths_with_captures_opt(
        &paths,
        &captures,
        FormatPathsOpts::default(),
    );

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        @nums
            4abc3113 LEAF [1, 2, 3]
                4bf5122f LEAF 1
            4abc3113 LEAF [1, 2, 3]
                dbc1b4c9 LEAF 2
            4abc3113 LEAF [1, 2, 3]
                084fed08 LEAF 3
        4abc3113 LEAF [1, 2, 3]
            4bf5122f LEAF 1
        4abc3113 LEAF [1, 2, 3]
            dbc1b4c9 LEAF 2
        4abc3113 LEAF [1, 2, 3]
            084fed08 LEAF 3
    "#}
    .trim();

    assert_actual_expected!(
        actual,
        expected,
        "`cbor` pattern should capture all numbers with `search` behavior"
    );

    println!(
        "✅ Performance test completed in {}ms",
        duration.as_millis()
    );
}

/// Test comprehensive integration with all `cbor` capture features
#[test]
fn test_comprehensive_cbor_captures() {
    // Create simple but comprehensive envelope structure: ["Alice", "Bob",
    // "Charlie"]
    let envelope = Envelope::new(vec!["Alice", "Bob", "Charlie"]);

    // Create comprehensive `cbor` pattern with search captures
    let cbor_pattern = Pattern::parse("cbor(/@people(search(text))/)").unwrap();
    let pattern = Pattern::capture("data", cbor_pattern);

    // Execute pattern
    let (paths, captures) = pattern.paths_with_captures(&envelope);

    // Verify basic expectations - `search` returns multiple paths (one per
    // person)
    assert_eq!(
        paths.len(),
        3,
        "Should find exactly three paths (one per person)"
    );
    assert_eq!(captures.len(), 2, "Should have exactly two capture groups");
    assert!(captures.contains_key("data"), "Should have 'data' capture");
    assert!(
        captures.contains_key("people"),
        "Should have 'people' capture"
    );
    assert_eq!(
        captures["data"].len(),
        3,
        "Should capture three data instances (one per path)"
    );
    assert_eq!(
        captures["people"].len(),
        3,
        "Should capture three people instances (one per person)"
    );

    // Verify formatted output follows rubric
    let actual = format_paths_with_captures_opt(
        &paths,
        &captures,
        FormatPathsOpts::default(),
    );

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        @data
            aea55aad LEAF ["Alice", "Bob", "Charlie"]
                13941b48 LEAF "Alice"
            aea55aad LEAF ["Alice", "Bob", "Charlie"]
                13b74194 LEAF "Bob"
            aea55aad LEAF ["Alice", "Bob", "Charlie"]
                ee8e3b02 LEAF "Charlie"
        @people
            aea55aad LEAF ["Alice", "Bob", "Charlie"]
                13941b48 LEAF "Alice"
            aea55aad LEAF ["Alice", "Bob", "Charlie"]
                13b74194 LEAF "Bob"
            aea55aad LEAF ["Alice", "Bob", "Charlie"]
                ee8e3b02 LEAF "Charlie"
        aea55aad LEAF ["Alice", "Bob", "Charlie"]
            13941b48 LEAF "Alice"
        aea55aad LEAF ["Alice", "Bob", "Charlie"]
            13b74194 LEAF "Bob"
        aea55aad LEAF ["Alice", "Bob", "Charlie"]
            ee8e3b02 LEAF "Charlie"
    "#}
    .trim();

    assert_actual_expected!(
        actual,
        expected,
        "Comprehensive `cbor` pattern should capture via `search` at multiple levels"
    );
}