cribra 0.2.0

Privacy-first Rust core for detecting, querying, and safely transforming secrets and sensitive data
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
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
//! Borrowed query view over findings contained in [`ScanResults`](crate::ScanResults).

use std::cmp::Ordering;

use crate::{Confidence, Finding, ScanEntry, ScanSort, Severity};

#[derive(Debug, Copy, Clone)]
enum QueryConstraint<T> {
    Exact(T),
    AtLeast(T),
}

/// Borrowed query over findings produced by a batch scan.
///
/// Queries are lazy and allocation-free until sorting or collection is
/// explicitly requested.
#[derive(Debug, Copy, Clone)]
#[must_use = "queries do nothing until they are iterated, inspected, collected, or sorted"]
pub struct ScanQuery<'a, K> {
    entries: &'a [ScanEntry<K>],
    severity: Option<QueryConstraint<Severity>>,
    confidence: Option<QueryConstraint<Confidence>>,
    rule_id: Option<&'a str>,
}

impl<'a, K> ScanQuery<'a, K> {
    pub(crate) const fn new(entries: &'a [ScanEntry<K>]) -> Self {
        Self {
            entries,
            severity: None,
            confidence: None,
            rule_id: None,
        }
    }

    /// Restricts the query to findings with exactly `severity`.
    ///
    /// This replaces any previously configured severity constraint.
    pub const fn severity(mut self, severity: Severity) -> Self {
        self.severity = Some(QueryConstraint::Exact(severity));
        self
    }

    /// Restricts the query to findings whose severity is at least `severity`.
    ///
    /// Severity follows its documented natural ordering:
    /// `Info < Low < Medium < High < Critical`.
    ///
    /// This replaces any previously configured severity constraint.
    pub const fn minimum_severity(mut self, severity: Severity) -> Self {
        self.severity = Some(QueryConstraint::AtLeast(severity));
        self
    }

    /// Restricts the query to findings with exactly `confidence`.
    ///
    /// This replaces any previously configured confidence constraint.
    pub const fn confidence(mut self, confidence: Confidence) -> Self {
        self.confidence = Some(QueryConstraint::Exact(confidence));
        self
    }

    /// Restricts the query to findings whose confidence is at least
    /// `confidence`.
    ///
    /// Confidence follows its documented natural ordering:
    /// `Low < Medium < High`.
    ///
    /// This replaces any previously configured confidence constraint.
    pub const fn minimum_confidence(mut self, confidence: Confidence) -> Self {
        self.confidence = Some(QueryConstraint::AtLeast(confidence));
        self
    }

    /// Restricts the query to findings produced by the exact rule identifier.
    ///
    /// The identifier is borrowed by the query and is compared without
    /// allocation.
    pub const fn rule_id(mut self, rule_id: &'a str) -> Self {
        self.rule_id = Some(rule_id);
        self
    }

    /// Restricts the query to critical findings.
    pub const fn critical(self) -> Self {
        self.severity(Severity::Critical)
    }

    /// Restricts the query to high- or critical-severity findings.
    pub const fn high_priority(self) -> Self {
        self.minimum_severity(Severity::High)
    }

    /// Restricts the query to high-confidence findings.
    pub const fn high_confidence(self) -> Self {
        self.confidence(Confidence::High)
    }

    /// Iterates over every finding while retaining its source key.
    ///
    /// The iterator is borrowed and allocation-free. Findings are yielded in
    /// source input order and in each report's deterministic finding order.
    pub fn iter(&self) -> impl Iterator<Item = (&'a K, &'a Finding)> + '_ {
        self.entries
            .iter()
            .flat_map(|entry| {
                entry
                    .report()
                    .iter()
                    .map(move |finding| (entry.key(), finding))
            })
            .filter(|(_, finding)| self.matches(finding))
    }

    /// Returns `true` when `finding` satisfies every configured filter.
    #[must_use]
    fn matches(&self, finding: &Finding) -> bool {
        if !matches_constraint(finding.severity(), self.severity) {
            return false;
        }

        if !matches_constraint(finding.confidence(), self.confidence) {
            return false;
        }

        if let Some(rule_id) = self.rule_id
            && finding.rule_id().as_str() != rule_id
        {
            return false;
        }

        true
    }

    /// Materializes and sorts the findings selected by this query.
    ///
    /// Filtering remains lazy until this method is called. Sorting allocates a
    /// vector containing borrowed `(source, finding)` pairs, but it never
    /// clones source keys or findings and never mutates the underlying
    /// [`ScanResults`](crate::ScanResults).
    ///
    /// Source sorting requires `K: Ord`. The same bound is applied to the
    /// complete sorting API so one `sort(ScanSort::...)` method can cover every
    /// supported ordering consistently.
    pub fn sort(&self, sort: ScanSort) -> SortedScanQuery<'a, K>
    where
        K: Ord,
    {
        let mut findings = self.collect();

        findings.sort_by(|left, right| compare_findings(*left, *right, sort));

        SortedScanQuery::new(findings)
    }

    /// Returns the number of findings selected by this query.
    #[must_use]
    pub fn count(&self) -> usize {
        self.iter().count()
    }

    /// Returns the first finding selected by this query, if any.
    #[must_use]
    pub fn first(&self) -> Option<(&'a K, &'a Finding)> {
        self.iter().next()
    }

    /// Returns the last finding selected by this query, if any.
    ///
    /// The query remains allocation-free. This walks the selected findings to
    /// completion because the underlying filtered iterator is not required to
    /// be double-ended.
    #[must_use]
    pub fn last(&self) -> Option<(&'a K, &'a Finding)> {
        self.iter().last()
    }

    /// Returns `true` when this query selects at least one finding.
    ///
    /// Evaluation short-circuits at the first match.
    #[must_use]
    pub fn has_matches(&self) -> bool {
        self.iter().next().is_some()
    }

    /// Returns `true` when this query selects no findings.
    ///
    /// Evaluation short-circuits at the first match.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        !self.has_matches()
    }

    /// Materializes the selected findings as borrowed `(source, finding)`
    /// pairs.
    ///
    /// Neither source keys nor findings are cloned.
    #[must_use]
    pub fn collect(&self) -> Vec<(&'a K, &'a Finding)> {
        self.iter().collect()
    }
}

fn matches_constraint<T>(value: T, constraint: Option<QueryConstraint<T>>) -> bool
where
    T: Copy + Ord,
{
    match constraint {
        None => true,
        Some(QueryConstraint::Exact(expected)) => value == expected,
        Some(QueryConstraint::AtLeast(minimum)) => value >= minimum,
    }
}

/// Materialized, sorted view of findings selected by a [`ScanQuery`].
///
/// This type owns only the vector of borrowed `(source, finding)` pairs.
/// Source keys and findings remain owned by the originating
/// [`ScanResults`](crate::ScanResults).
#[derive(Debug, Clone)]
#[must_use = "sorted queries should be inspected, iterated, or consumed"]
pub struct SortedScanQuery<'a, K> {
    findings: Vec<(&'a K, &'a Finding)>,
}

impl<'a, K> SortedScanQuery<'a, K> {
    pub(crate) const fn new(findings: Vec<(&'a K, &'a Finding)>) -> Self {
        Self { findings }
    }

    /// Returns the number of sorted findings.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.findings.len()
    }

    /// Returns the number of sorted findings.
    #[must_use]
    pub const fn count(&self) -> usize {
        self.findings.len()
    }

    /// Returns `true` when the sorted query contains at least one finding.
    #[must_use]
    pub const fn has_matches(&self) -> bool {
        !self.findings.is_empty()
    }

    /// Returns `true` when the sorted query contains no findings.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.findings.is_empty()
    }

    /// Iterates over sorted `(source, finding)` pairs.
    pub fn iter(
        &self,
    ) -> impl DoubleEndedIterator<Item = (&'a K, &'a Finding)> + ExactSizeIterator + '_ {
        self.findings.iter().copied()
    }

    /// Returns the first sorted finding, if any.
    #[must_use]
    pub fn first(&self) -> Option<(&'a K, &'a Finding)> {
        self.findings.first().copied()
    }

    /// Returns the last sorted finding, if any.
    #[must_use]
    pub fn last(&self) -> Option<(&'a K, &'a Finding)> {
        self.findings.last().copied()
    }

    /// Returns the sorted findings as a borrowed slice.
    #[must_use]
    pub fn as_slice(&self) -> &[(&'a K, &'a Finding)] {
        &self.findings
    }

    /// Returns the sorted borrowed pairs as a new vector.
    ///
    /// Only references are copied; source keys and findings are not cloned.
    #[must_use]
    pub fn to_vec(&self) -> Vec<(&'a K, &'a Finding)> {
        self.findings.clone()
    }

    /// Consumes the sorted query and returns its borrowed pairs without copying
    /// the vector.
    #[must_use]
    pub fn into_vec(self) -> Vec<(&'a K, &'a Finding)> {
        self.findings
    }
}

impl<'a, K> IntoIterator for SortedScanQuery<'a, K> {
    type Item = (&'a K, &'a Finding);
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.findings.into_iter()
    }
}

impl<'query, 'a, K> IntoIterator for &'query SortedScanQuery<'a, K> {
    type Item = (&'a K, &'a Finding);
    type IntoIter = std::iter::Copied<std::slice::Iter<'query, (&'a K, &'a Finding)>>;

    fn into_iter(self) -> Self::IntoIter {
        self.findings.iter().copied()
    }
}

fn compare_findings<K: Ord>(
    left: (&K, &Finding),
    right: (&K, &Finding),
    sort: ScanSort,
) -> Ordering {
    let primary = match sort {
        ScanSort::RuleId => left.1.rule_id().as_str().cmp(right.1.rule_id().as_str()),
        ScanSort::RuleIdDescending => right.1.rule_id().as_str().cmp(left.1.rule_id().as_str()),
        ScanSort::Source => left.0.cmp(right.0),
        ScanSort::SourceDescending => right.0.cmp(left.0),
        ScanSort::Severity => left.1.severity().cmp(&right.1.severity()),
        ScanSort::SeverityDescending => right.1.severity().cmp(&left.1.severity()),
        ScanSort::Confidence => left.1.confidence().cmp(&right.1.confidence()),
        ScanSort::ConfidenceDescending => right.1.confidence().cmp(&left.1.confidence()),
        ScanSort::Location => left.1.location().cmp(right.1.location()),
        ScanSort::LocationDescending => right.1.location().cmp(left.1.location()),
    };

    if primary != Ordering::Equal {
        return primary;
    }

    // Preserve a deterministic total ordering when multiple findings share the
    // selected sort key. The tie-break chain is intentionally independent from
    // the chosen direction so repeated queries produce a stable result.
    left.0
        .cmp(right.0)
        .then_with(|| left.1.location().cmp(right.1.location()))
        .then_with(|| left.1.rule_id().as_str().cmp(right.1.rule_id().as_str()))
        .then_with(|| left.1.severity().cmp(&right.1.severity()))
        .then_with(|| left.1.confidence().cmp(&right.1.confidence()))
}

#[cfg(test)]
mod tests {
    use crate::{Confidence, Location, RuleId, ScanEntry, ScanReport, Severity};

    use super::*;

    fn finding(rule: &str, severity: Severity, confidence: Confidence) -> Finding {
        Finding::new(
            RuleId::from(rule),
            Location::from_span(0, 1),
            severity,
            confidence,
            None,
        )
    }

    fn report(rule: &str, severity: Severity) -> ScanReport {
        ScanReport::new_with_candidates(vec![finding(rule, severity, Confidence::High)], Vec::new())
    }

    #[test]
    fn iterates_all_findings_without_cloning() {
        let entries = [
            ScanEntry::new("a", 1, report("one", Severity::High)),
            ScanEntry::new("b", 1, report("two", Severity::Medium)),
        ];
        let query = ScanQuery::new(&entries);

        let findings = query.collect();

        assert_eq!(findings.len(), 2);
        assert_eq!(*findings[0].0, "a");
        assert_eq!(findings[0].1.rule_id().as_str(), "one");
        assert_eq!(*findings[1].0, "b");
        assert_eq!(findings[1].1.rule_id().as_str(), "two");
    }

    #[test]
    fn exposes_count_and_first_helpers() {
        let entries = [ScanEntry::new(
            "source",
            1,
            report("rule", Severity::Critical),
        )];
        let query = ScanQuery::new(&entries);

        assert_eq!(query.count(), 1);

        let (key, finding) = query.first().expect("one finding should exist");
        assert_eq!(*key, "source");
        assert_eq!(finding.rule_id().as_str(), "rule");
    }

    #[test]
    fn empty_query_has_no_findings() {
        let entries: [ScanEntry<&str>; 0] = [];
        let query = ScanQuery::new(&entries);

        assert_eq!(query.count(), 0);
        assert!(query.first().is_none());
        assert!(query.collect().is_empty());
    }
    #[test]
    fn filters_exact_severity() {
        let entries = [ScanEntry::new(
            "source",
            3,
            ScanReport::new_with_candidates(
                vec![
                    finding("low", Severity::Low, Confidence::High),
                    finding("high", Severity::High, Confidence::High),
                    finding("critical", Severity::Critical, Confidence::High),
                ],
                Vec::new(),
            ),
        )];

        let findings = ScanQuery::new(&entries).severity(Severity::High).collect();

        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].1.rule_id().as_str(), "high");
    }

    #[test]
    fn filters_minimum_severity() {
        let entries = [ScanEntry::new(
            "source",
            3,
            ScanReport::new_with_candidates(
                vec![
                    finding("medium", Severity::Medium, Confidence::High),
                    finding("high", Severity::High, Confidence::High),
                    finding("critical", Severity::Critical, Confidence::High),
                ],
                Vec::new(),
            ),
        )];

        let findings = ScanQuery::new(&entries)
            .minimum_severity(Severity::High)
            .collect();

        assert_eq!(findings.len(), 2);
        assert_eq!(findings[0].1.rule_id().as_str(), "high");
        assert_eq!(findings[1].1.rule_id().as_str(), "critical");
    }

    #[test]
    fn filters_confidence_and_minimum_confidence() {
        let entries = [ScanEntry::new(
            "source",
            3,
            ScanReport::new_with_candidates(
                vec![
                    finding("low", Severity::High, Confidence::Low),
                    finding("medium", Severity::High, Confidence::Medium),
                    finding("high", Severity::High, Confidence::High),
                ],
                Vec::new(),
            ),
        )];

        let exact = ScanQuery::new(&entries)
            .confidence(Confidence::Medium)
            .collect();
        assert_eq!(exact.len(), 1);
        assert_eq!(exact[0].1.rule_id().as_str(), "medium");

        let minimum = ScanQuery::new(&entries)
            .minimum_confidence(Confidence::Medium)
            .collect();
        assert_eq!(minimum.len(), 2);
    }

    #[test]
    fn filters_exact_rule_identifier() {
        let entries = [ScanEntry::new(
            "source",
            2,
            ScanReport::new_with_candidates(
                vec![
                    finding("github.token", Severity::Critical, Confidence::High),
                    finding("stripe.secret", Severity::Critical, Confidence::High),
                ],
                Vec::new(),
            ),
        )];

        let findings = ScanQuery::new(&entries).rule_id("stripe.secret").collect();

        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].1.rule_id().as_str(), "stripe.secret");
    }

    #[test]
    fn composes_independent_filters() {
        let entries = [ScanEntry::new(
            "source",
            4,
            ScanReport::new_with_candidates(
                vec![
                    finding("target", Severity::High, Confidence::High),
                    finding("target", Severity::High, Confidence::Medium),
                    finding("target", Severity::Medium, Confidence::High),
                    finding("other", Severity::Critical, Confidence::High),
                ],
                Vec::new(),
            ),
        )];

        let findings = ScanQuery::new(&entries)
            .minimum_severity(Severity::High)
            .minimum_confidence(Confidence::High)
            .rule_id("target")
            .collect();

        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].1.rule_id().as_str(), "target");
    }

    #[test]
    fn exact_and_minimum_filters_replace_each_other() {
        let entries = [ScanEntry::new(
            "source",
            2,
            ScanReport::new_with_candidates(
                vec![
                    finding("high", Severity::High, Confidence::High),
                    finding("critical", Severity::Critical, Confidence::High),
                ],
                Vec::new(),
            ),
        )];

        let findings = ScanQuery::new(&entries)
            .severity(Severity::Critical)
            .minimum_severity(Severity::High)
            .collect();

        assert_eq!(findings.len(), 2);
    }

    #[test]
    fn convenience_filters_match_domain_semantics() {
        let entries = [ScanEntry::new(
            "source",
            3,
            ScanReport::new_with_candidates(
                vec![
                    finding("medium", Severity::Medium, Confidence::High),
                    finding("high", Severity::High, Confidence::Medium),
                    finding("critical", Severity::Critical, Confidence::High),
                ],
                Vec::new(),
            ),
        )];

        assert_eq!(ScanQuery::new(&entries).critical().count(), 1);
        assert_eq!(ScanQuery::new(&entries).high_priority().count(), 2);
        assert_eq!(ScanQuery::new(&entries).high_confidence().count(), 2);
    }

    #[test]
    fn sorts_by_rule_identifier_in_both_directions() {
        let entries = [ScanEntry::new(
            "source",
            3,
            ScanReport::new_with_candidates(
                vec![
                    finding("zeta", Severity::High, Confidence::High),
                    finding("alpha", Severity::High, Confidence::High),
                    finding("middle", Severity::High, Confidence::High),
                ],
                Vec::new(),
            ),
        )];

        let ascending = ScanQuery::new(&entries).sort(ScanSort::RuleId);
        let descending = ScanQuery::new(&entries).sort(ScanSort::RuleIdDescending);

        assert_eq!(
            ascending
                .iter()
                .map(|(_, finding)| finding.rule_id().as_str())
                .collect::<Vec<_>>(),
            ["alpha", "middle", "zeta"],
        );
        assert_eq!(
            descending
                .iter()
                .map(|(_, finding)| finding.rule_id().as_str())
                .collect::<Vec<_>>(),
            ["zeta", "middle", "alpha"],
        );
    }

    #[test]
    fn sorts_by_source_key() {
        let entries = [
            ScanEntry::new("z.env", 1, report("z", Severity::High)),
            ScanEntry::new("a.env", 1, report("a", Severity::High)),
            ScanEntry::new("m.env", 1, report("m", Severity::High)),
        ];

        let sorted = ScanQuery::new(&entries).sort(ScanSort::Source);

        assert_eq!(
            sorted.iter().map(|(key, _)| *key).collect::<Vec<_>>(),
            ["a.env", "m.env", "z.env"],
        );
    }

    #[test]
    fn sorts_by_severity_and_confidence() {
        let entries = [ScanEntry::new(
            "source",
            3,
            ScanReport::new_with_candidates(
                vec![
                    finding("medium", Severity::Medium, Confidence::Low),
                    finding("critical", Severity::Critical, Confidence::Medium),
                    finding("high", Severity::High, Confidence::High),
                ],
                Vec::new(),
            ),
        )];

        let severity = ScanQuery::new(&entries).sort(ScanSort::SeverityDescending);
        assert_eq!(
            severity
                .iter()
                .map(|(_, finding)| finding.severity())
                .collect::<Vec<_>>(),
            [Severity::Critical, Severity::High, Severity::Medium],
        );

        let confidence = ScanQuery::new(&entries).sort(ScanSort::ConfidenceDescending);
        assert_eq!(
            confidence
                .iter()
                .map(|(_, finding)| finding.confidence())
                .collect::<Vec<_>>(),
            [Confidence::High, Confidence::Medium, Confidence::Low],
        );
    }

    #[test]
    fn sorts_by_location() {
        let entries = [ScanEntry::new(
            "source",
            12,
            ScanReport::new_with_candidates(
                vec![
                    Finding::new(
                        RuleId::from("later"),
                        Location::from_span(8, 9),
                        Severity::High,
                        Confidence::High,
                        None,
                    ),
                    Finding::new(
                        RuleId::from("first"),
                        Location::from_span(1, 2),
                        Severity::High,
                        Confidence::High,
                        None,
                    ),
                ],
                Vec::new(),
            ),
        )];

        let sorted = ScanQuery::new(&entries).sort(ScanSort::Location);

        assert_eq!(sorted.first().unwrap().1.location().start(), 1);
        assert_eq!(sorted.last().unwrap().1.location().start(), 8);
    }

    #[test]
    fn sorting_happens_after_lazy_filtering() {
        let entries = [ScanEntry::new(
            "source",
            3,
            ScanReport::new_with_candidates(
                vec![
                    finding("zeta", Severity::High, Confidence::High),
                    finding("ignored", Severity::Low, Confidence::High),
                    finding("alpha", Severity::Critical, Confidence::High),
                ],
                Vec::new(),
            ),
        )];

        let sorted = ScanQuery::new(&entries)
            .minimum_severity(Severity::High)
            .sort(ScanSort::RuleId);

        assert_eq!(sorted.len(), 2);
        assert_eq!(sorted.first().unwrap().1.rule_id().as_str(), "alpha");
        assert_eq!(sorted.last().unwrap().1.rule_id().as_str(), "zeta");
    }

    #[test]
    fn sorted_query_exposes_owned_vector_of_borrowed_pairs() {
        let entries = [ScanEntry::new("source", 1, report("rule", Severity::High))];

        let sorted = ScanQuery::new(&entries).sort(ScanSort::RuleId);

        assert!(!sorted.is_empty());
        assert_eq!(sorted.as_slice().len(), 1);

        let pairs = sorted.into_vec();
        assert_eq!(pairs.len(), 1);
        assert_eq!(pairs[0].1.rule_id().as_str(), "rule");
    }

    #[test]
    fn lazy_query_helpers_short_circuit_semantically() {
        let entries = [ScanEntry::new(
            "source",
            2,
            ScanReport::new_with_candidates(
                vec![
                    finding("first", Severity::High, Confidence::High),
                    finding("last", Severity::Critical, Confidence::High),
                ],
                Vec::new(),
            ),
        )];

        let query = ScanQuery::new(&entries).minimum_severity(Severity::High);

        assert!(query.has_matches());
        assert!(!query.is_empty());
        assert_eq!(query.first().unwrap().1.rule_id().as_str(), "first");
        assert_eq!(query.last().unwrap().1.rule_id().as_str(), "last");
        assert_eq!(query.collect().len(), 2);
    }

    #[test]
    fn lazy_query_helpers_handle_no_matches() {
        let entries = [ScanEntry::new(
            "source",
            1,
            report("medium", Severity::Medium),
        )];

        let query = ScanQuery::new(&entries).critical();

        assert!(!query.has_matches());
        assert!(query.is_empty());
        assert!(query.first().is_none());
        assert!(query.last().is_none());
        assert!(query.collect().is_empty());
    }

    #[test]
    fn sorted_query_helpers_match_materialized_state() {
        let entries = [ScanEntry::new(
            "source",
            2,
            ScanReport::new_with_candidates(
                vec![
                    finding("zeta", Severity::High, Confidence::High),
                    finding("alpha", Severity::Critical, Confidence::High),
                ],
                Vec::new(),
            ),
        )];

        let sorted = ScanQuery::new(&entries).sort(ScanSort::RuleId);

        assert_eq!(sorted.len(), 2);
        assert_eq!(sorted.count(), 2);
        assert!(sorted.has_matches());
        assert!(!sorted.is_empty());
        assert_eq!(sorted.to_vec().len(), 2);
        assert_eq!(sorted.first().unwrap().1.rule_id().as_str(), "alpha");
        assert_eq!(sorted.last().unwrap().1.rule_id().as_str(), "zeta");
    }
}