rustloclib 0.15.3

Rust-aware LOC counter that separates production code from tests — even in the same file
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
//! Query set: processed data ready for table rendering.
//!
//! A QuerySet sits between raw counting/diff results and the final table output.
//! It represents data that has been:
//! - Aggregated to the requested level (crate, module, file)
//! - Filtered to include only requested line types
//! - Sorted according to the ordering preference
//!
//! After construction with `from_result`, two chainable methods further
//! reduce the row set:
//!
//! - [`CountQuerySet::filter`] / [`DiffQuerySet::filter`] — keep only rows
//!   satisfying every supplied [`Predicate`] (AND-combined). For diffs,
//!   each predicate is evaluated against the row's net change
//!   (added − removed).
//! - [`CountQuerySet::top`] / [`DiffQuerySet::top`] — truncate to the first
//!   N rows after sorting.
//!
//! Order of application is whatever the caller chains.
//! `.filter(...).top(N)` keeps the top N of the matching rows (the CLI's
//! convention); `.top(N).filter(...)` filters within an already-truncated
//! slice. `total` and `total_items` always describe the underlying data
//! set, not the post-filter/post-top slice — that lets the footer render
//! "top X of Y" honestly.
//!
//! The data pipeline is:
//! 1. Raw Data (CountResult, DiffResult)
//! 2. QuerySet (filtered, aggregated, sorted, optionally truncated)
//! 3. LOCTable (formatted strings for display)
//!
//! [`Predicate`]: crate::query::options::Predicate

use serde::{Deserialize, Serialize};

use std::collections::HashMap;

use crate::data::counter::{compute_module_name, CountResult};
use crate::data::diff::{DiffResult, LocsDiff};
use crate::data::stats::Locs;

use super::options::{Aggregation, Field, LineTypes, OrderBy, OrderDirection, Ordering, Predicate};

/// A single item in a query set (one row of data before string formatting).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryItem<T> {
    /// Row label (file path, crate name, module name, etc.)
    pub label: String,
    /// Statistics for this item
    pub stats: T,
}

/// Query set for count results.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CountQuerySet {
    /// Aggregation level used
    pub aggregation: Aggregation,
    /// Line types included
    pub line_types: LineTypes,
    /// Data rows (filtered and sorted; possibly truncated by `top`)
    pub items: Vec<QueryItem<Locs>>,
    /// Total across all items in the underlying data set (not affected by `top` or `filter`)
    pub total: Locs,
    /// Number of files analyzed
    pub file_count: usize,
    /// Count of rows before any user-driven reduction (`top` or `filter`).
    /// Equals `items.len()` unless one of those was applied.
    #[serde(default)]
    pub total_items: usize,
    /// True iff `top` was applied. Distinguishes top-truncation from
    /// filter-elimination so the footer can render "top X of Y" vs plain
    /// "X of Y" appropriately.
    #[serde(default)]
    pub top_applied: bool,
}

/// Query set for diff results.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffQuerySet {
    /// Aggregation level used
    pub aggregation: Aggregation,
    /// Line types included
    pub line_types: LineTypes,
    /// Data rows (filtered and sorted; possibly truncated by `top`)
    pub items: Vec<QueryItem<LocsDiff>>,
    /// Total diff across all items in the underlying data set (not affected by `top` or `filter`)
    pub total: LocsDiff,
    /// Number of files changed
    pub file_count: usize,
    /// Source commit
    pub from_commit: String,
    /// Target commit
    pub to_commit: String,
    /// Lines added in non-Rust files
    #[serde(default)]
    pub non_rust_added: u64,
    /// Lines removed in non-Rust files
    #[serde(default)]
    pub non_rust_removed: u64,
    /// Count of rows before any user-driven reduction (`top` or `filter`).
    /// Equals `items.len()` unless one of those was applied.
    #[serde(default)]
    pub total_items: usize,
    /// True iff `top` was applied. See [`CountQuerySet::top_applied`].
    #[serde(default)]
    pub top_applied: bool,
}

impl CountQuerySet {
    /// Create a QuerySet from a CountResult.
    ///
    /// Applies aggregation level, line type filters, and ordering.
    pub fn from_result(
        result: &CountResult,
        aggregation: Aggregation,
        line_types: LineTypes,
        ordering: Ordering,
    ) -> Self {
        let items = build_count_items(result, &aggregation, &line_types, &ordering);
        let total = result.total.filter(line_types);
        let total_items = items.len();

        CountQuerySet {
            aggregation,
            line_types,
            items,
            total,
            file_count: result.file_count,
            total_items,
            top_applied: false,
        }
    }

    /// Keep only the first `n` items after ordering.
    ///
    /// Applied after `from_result` so the truncation runs on already-sorted
    /// rows. With `n` larger than the current row count this is a no-op.
    /// `total` and `file_count` are intentionally not changed — the displayed
    /// rows are a slice, but the underlying counts still describe the full
    /// data set.
    #[must_use]
    pub fn top(mut self, n: usize) -> Self {
        self.items.truncate(n);
        self.top_applied = true;
        self
    }

    /// Keep only items satisfying every predicate (AND-combined).
    ///
    /// `Field::Total` reads `Locs::total` directly so filtering on `total`
    /// matches the displayed `Total` column regardless of the active
    /// `LineTypes`. To filter on a subset, use the per-field flags
    /// (`--code-gte`, `--tests-gte`, …) which always operate on the
    /// underlying line type, not a "currently-visible" view of it.
    ///
    /// `total_items` is intentionally NOT updated — it tracks the row count
    /// before any *user-driven* truncation (`top` and `filter`), so the
    /// footer can render "top X of Y" honestly even when the visible slice
    /// is filtered down. The full data set is still summarized in `total`.
    #[must_use]
    pub fn filter(mut self, preds: &[Predicate]) -> Self {
        if preds.is_empty() {
            return self;
        }
        self.items
            .retain(|item| preds.iter().all(|p| matches_locs(p, &item.stats)));
        self
    }
}

/// Saturating `u64 -> i64`. Values past `i64::MAX` clamp to `i64::MAX`
/// rather than wrapping into the negative range. The cap is ~9.2 × 10^18 —
/// well past any realistic LOC count, but better to clamp than to silently
/// produce wrong filter results if a caller passes a huge value.
fn u64_to_i64_sat(v: u64) -> i64 {
    i64::try_from(v).unwrap_or(i64::MAX)
}

/// Resolve the integer value a predicate's field refers to in a `Locs`.
///
/// `Field::Total` reads `Locs::total` directly — the same precomputed
/// all-types sum that the displayed `Total` column shows. Filtering on
/// `total` matches what the user sees, regardless of which line types
/// the active `LineTypes` happens to enable for column display.
fn locs_field_value(locs: &Locs, field: Field) -> i64 {
    let v: u64 = match field {
        Field::Code => locs.code,
        Field::Tests => locs.tests,
        Field::Examples => locs.examples,
        Field::Docs => locs.docs,
        Field::Comments => locs.comments,
        Field::Blanks => locs.blanks,
        Field::Total => locs.total,
    };
    u64_to_i64_sat(v)
}

fn matches_locs(pred: &Predicate, locs: &Locs) -> bool {
    let lhs = locs_field_value(locs, pred.field);
    pred.op.evaluate(lhs, u64_to_i64_sat(pred.value))
}

/// Resolve the (signed) net diff value a predicate's field refers to.
///
/// `Field::Total` uses `LocsDiff::net_total()` — the all-types net change.
/// Same WYSIWYF rationale as `locs_field_value`.
fn diff_field_value(diff: &LocsDiff, field: Field) -> i64 {
    match field {
        Field::Code => diff.net_code(),
        Field::Tests => diff.net_tests(),
        Field::Examples => diff.net_examples(),
        Field::Docs => diff.net_docs(),
        Field::Comments => diff.net_comments(),
        Field::Blanks => diff.net_blanks(),
        Field::Total => diff.net_total(),
    }
}

fn matches_diff(pred: &Predicate, diff: &LocsDiff) -> bool {
    let lhs = diff_field_value(diff, pred.field);
    pred.op.evaluate(lhs, u64_to_i64_sat(pred.value))
}

/// Compute a relative path label for a file.
/// Returns the path relative to the workspace root, falling back to the full path if strip fails.
fn relative_path_label(path: &std::path::Path, root: &std::path::Path) -> String {
    path.strip_prefix(root)
        .map(|p| p.to_string_lossy().to_string())
        .unwrap_or_else(|_| path.to_string_lossy().to_string())
}

impl DiffQuerySet {
    /// Create a QuerySet from a DiffResult.
    ///
    /// Applies aggregation level, line type filters, and ordering.
    pub fn from_result(
        result: &DiffResult,
        aggregation: Aggregation,
        line_types: LineTypes,
        ordering: Ordering,
    ) -> Self {
        let items = build_diff_items(result, &aggregation, &line_types, &ordering);
        let total = result.total.filter(line_types);
        let total_items = items.len();

        DiffQuerySet {
            aggregation,
            line_types,
            items,
            total,
            file_count: result.files.len(),
            from_commit: result.from_commit.clone(),
            to_commit: result.to_commit.clone(),
            non_rust_added: result.non_rust_added,
            non_rust_removed: result.non_rust_removed,
            total_items,
            top_applied: false,
        }
    }

    /// Keep only the first `n` items after ordering.
    ///
    /// See [`CountQuerySet::top`] for semantics — `total` and `file_count`
    /// describe the full data set, not the truncated slice.
    #[must_use]
    pub fn top(mut self, n: usize) -> Self {
        self.items.truncate(n);
        self.top_applied = true;
        self
    }

    /// Keep only items satisfying every predicate (AND-combined).
    ///
    /// Comparisons are made against the **net** change for each field
    /// (added − removed), so `--code-lt 0` matches files with net code
    /// removed, and `--code-gte 100` matches files where added − removed ≥ 100.
    /// `Field::Total` uses `LocsDiff::net_total()` so it matches the
    /// displayed Total column. See [`CountQuerySet::filter`] for the
    /// rationale on why `total_items` isn't updated.
    #[must_use]
    pub fn filter(mut self, preds: &[Predicate]) -> Self {
        if preds.is_empty() {
            return self;
        }
        self.items
            .retain(|item| preds.iter().all(|p| matches_diff(p, &item.stats)));
        self
    }
}

/// Get sort key for Locs based on OrderBy.
///
/// `OrderBy::Total` reads `Locs::total` so sort order matches the displayed
/// `Total` column (which is also `Locs::total`). See `locs_field_value` for
/// the same rationale on the predicate side.
fn count_sort_key(locs: &Locs, order_by: &OrderBy) -> u64 {
    match order_by {
        OrderBy::Label => 0, // Label sorting handled separately
        OrderBy::Code => locs.code,
        OrderBy::Tests => locs.tests,
        OrderBy::Examples => locs.examples,
        OrderBy::Docs => locs.docs,
        OrderBy::Comments => locs.comments,
        OrderBy::Blanks => locs.blanks,
        OrderBy::Total => locs.total,
    }
}

/// Build query items from CountResult based on aggregation level.
fn build_count_items(
    result: &CountResult,
    aggregation: &Aggregation,
    line_types: &LineTypes,
    ordering: &Ordering,
) -> Vec<QueryItem<Locs>> {
    let mut items: Vec<(String, Locs)> = match aggregation {
        Aggregation::Total => return vec![],
        Aggregation::ByCrate => result
            .crates
            .iter()
            .map(|c| (c.name.clone(), c.stats.filter(*line_types)))
            .collect(),
        Aggregation::ByModule => result
            .modules
            .iter()
            .map(|m| {
                let label = if m.name.is_empty() {
                    "(root)".to_string()
                } else {
                    m.name.clone()
                };
                (label, m.stats.filter(*line_types))
            })
            .collect(),
        Aggregation::ByFile => result
            .files
            .iter()
            .map(|f| {
                (
                    relative_path_label(&f.path, &result.root),
                    f.stats.filter(*line_types),
                )
            })
            .collect(),
    };

    // Sort based on ordering
    match ordering.by {
        OrderBy::Label => {
            items.sort_by(|a, b| a.0.cmp(&b.0));
        }
        _ => {
            items.sort_by(|a, b| {
                let key_a = count_sort_key(&a.1, &ordering.by);
                let key_b = count_sort_key(&b.1, &ordering.by);
                key_a.cmp(&key_b)
            });
        }
    }

    // Reverse if descending
    if ordering.direction == OrderDirection::Descending {
        items.reverse();
    }

    // Map to QueryItems
    items
        .into_iter()
        .map(|(label, stats)| QueryItem { label, stats })
        .collect()
}

/// Get sort key for LocsDiff based on OrderBy (uses net change).
///
/// `OrderBy::Total` uses `LocsDiff::net_total()` for the same WYSIWYF
/// rationale as the count side — sort order matches the displayed Total.
fn diff_sort_key(diff: &LocsDiff, order_by: &OrderBy) -> i64 {
    match order_by {
        OrderBy::Label => 0, // Label sorting handled separately
        OrderBy::Code => diff.net_code(),
        OrderBy::Tests => diff.net_tests(),
        OrderBy::Examples => diff.net_examples(),
        OrderBy::Docs => diff.net_docs(),
        OrderBy::Comments => diff.net_comments(),
        OrderBy::Blanks => diff.net_blanks(),
        OrderBy::Total => diff.net_total(),
    }
}

/// Build query items from DiffResult based on aggregation level.
fn build_diff_items(
    result: &DiffResult,
    aggregation: &Aggregation,
    line_types: &LineTypes,
    ordering: &Ordering,
) -> Vec<QueryItem<LocsDiff>> {
    let mut items: Vec<(String, LocsDiff)> = match aggregation {
        Aggregation::Total => return vec![],
        Aggregation::ByCrate => result
            .crates
            .iter()
            .map(|c| (c.name.clone(), c.diff.filter(*line_types)))
            .collect(),
        Aggregation::ByModule => {
            let mut module_map: HashMap<String, LocsDiff> = HashMap::new();
            for crate_diff in &result.crates {
                let src_root = crate_diff.path.join("src");
                let effective_root = if src_root.exists() {
                    src_root
                } else {
                    crate_diff.path.clone()
                };
                for file in &crate_diff.files {
                    let abs_path = if file.path.is_absolute() {
                        file.path.clone()
                    } else {
                        result.root.join(&file.path)
                    };
                    let local_module = compute_module_name(&abs_path, &effective_root);
                    let full_name = if local_module.is_empty() {
                        crate_diff.name.clone()
                    } else {
                        format!("{}::{}", crate_diff.name, local_module)
                    };
                    let entry = module_map.entry(full_name).or_default();
                    *entry += file.diff.filter(*line_types);
                }
            }
            module_map.into_iter().collect()
        }
        Aggregation::ByFile => result
            .files
            .iter()
            .map(|f| {
                (
                    f.path.to_string_lossy().to_string(),
                    f.diff.filter(*line_types),
                )
            })
            .collect(),
    };

    // Sort based on ordering
    match ordering.by {
        OrderBy::Label => {
            items.sort_by(|a, b| a.0.cmp(&b.0));
        }
        _ => {
            items.sort_by(|a, b| {
                let key_a = diff_sort_key(&a.1, &ordering.by);
                let key_b = diff_sort_key(&b.1, &ordering.by);
                key_a.cmp(&key_b)
            });
        }
    }

    // Reverse if descending
    if ordering.direction == OrderDirection::Descending {
        items.reverse();
    }

    // Map to QueryItems
    items
        .into_iter()
        .map(|(label, stats)| QueryItem { label, stats })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::stats::CrateStats;
    use crate::query::options::{Field, Op, Predicate};
    use std::path::PathBuf;

    fn sample_locs(code: u64, tests: u64) -> Locs {
        Locs {
            code,
            tests,
            examples: 0,
            docs: 0,
            comments: 0,
            blanks: 0,
            total: code + tests,
        }
    }

    fn sample_count_result() -> CountResult {
        CountResult {
            root: PathBuf::from("/workspace"),
            file_count: 4,
            total: sample_locs(200, 100),
            crates: vec![
                CrateStats {
                    name: "alpha".to_string(),
                    path: PathBuf::from("/alpha"),
                    stats: sample_locs(50, 25),
                    files: vec![],
                },
                CrateStats {
                    name: "beta".to_string(),
                    path: PathBuf::from("/beta"),
                    stats: sample_locs(150, 75),
                    files: vec![],
                },
            ],
            files: vec![],
            modules: vec![],
        }
    }

    #[test]
    fn test_count_queryset_by_crate() {
        let result = sample_count_result();
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::default(),
        );

        assert_eq!(qs.items.len(), 2);
        assert_eq!(qs.file_count, 4);
        // Default ordering is by label ascending
        assert_eq!(qs.items[0].label, "alpha");
        assert_eq!(qs.items[1].label, "beta");
    }

    #[test]
    fn test_count_queryset_ordering_by_code() {
        let result = sample_count_result();
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::by_code(), // Descending by default
        );

        // beta has 150 code, alpha has 50
        assert_eq!(qs.items[0].label, "beta");
        assert_eq!(qs.items[0].stats.code, 150);
        assert_eq!(qs.items[1].label, "alpha");
        assert_eq!(qs.items[1].stats.code, 50);
    }

    #[test]
    fn test_count_queryset_total_aggregation() {
        let result = sample_count_result();
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::Total,
            LineTypes::everything(),
            Ordering::default(),
        );

        // Total aggregation returns no items (just uses total)
        assert_eq!(qs.items.len(), 0);
        assert_eq!(qs.total.code, 200);
        assert_eq!(qs.total.tests, 100);
    }

    #[test]
    fn test_count_queryset_filtered_line_types() {
        let result = sample_count_result();
        let line_types = LineTypes::new().with_code();
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            line_types,
            Ordering::default(),
        );

        // Stats should be filtered
        assert_eq!(qs.items[0].stats.code, 50);
        assert_eq!(qs.items[0].stats.tests, 0); // Filtered out
    }

    fn sample_count_result_three_crates() -> CountResult {
        CountResult {
            root: PathBuf::from("/workspace"),
            file_count: 6,
            total: sample_locs(600, 300),
            crates: vec![
                CrateStats {
                    name: "alpha".to_string(),
                    path: PathBuf::from("/alpha"),
                    stats: sample_locs(50, 25),
                    files: vec![],
                },
                CrateStats {
                    name: "beta".to_string(),
                    path: PathBuf::from("/beta"),
                    stats: sample_locs(150, 75),
                    files: vec![],
                },
                CrateStats {
                    name: "gamma".to_string(),
                    path: PathBuf::from("/gamma"),
                    stats: sample_locs(400, 200),
                    files: vec![],
                },
            ],
            files: vec![],
            modules: vec![],
        }
    }

    #[test]
    fn test_count_queryset_top_truncates_after_sort() {
        let result = sample_count_result_three_crates();
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::by_code(), // descending: gamma(400), beta(150), alpha(50)
        )
        .top(2);

        assert_eq!(qs.items.len(), 2);
        assert_eq!(qs.items[0].label, "gamma");
        assert_eq!(qs.items[1].label, "beta");
        // total reflects the full data set, not the truncated slice
        assert_eq!(qs.total.code, 600);
        assert_eq!(qs.file_count, 6);
    }

    #[test]
    fn test_count_queryset_top_larger_than_len_is_noop() {
        let result = sample_count_result_three_crates();
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::by_code(),
        )
        .top(99);

        assert_eq!(qs.items.len(), 3);
    }

    #[test]
    fn test_filter_gte_drops_items_below_threshold() {
        // alpha=50 code, beta=150, gamma=400. Filter --code-gte 100 keeps
        // beta and gamma.
        let result = sample_count_result_three_crates();
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::default(),
        )
        .filter(&[Predicate::new(Field::Code, Op::Gte, 100)]);

        let labels: Vec<_> = qs.items.iter().map(|i| i.label.as_str()).collect();
        assert_eq!(labels, vec!["beta", "gamma"]);
    }

    #[test]
    fn test_filter_eq_and_ne() {
        let result = sample_count_result_three_crates();
        let eq = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::default(),
        )
        .filter(&[Predicate::new(Field::Code, Op::Eq, 150)]);
        assert_eq!(eq.items.len(), 1);
        assert_eq!(eq.items[0].label, "beta");

        let ne = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::default(),
        )
        .filter(&[Predicate::new(Field::Code, Op::Ne, 150)]);
        assert_eq!(ne.items.len(), 2);
    }

    #[test]
    fn test_filter_combines_predicates_with_and() {
        let result = sample_count_result_three_crates();
        // alpha=50/25, beta=150/75, gamma=400/200
        // --code-gt 100 AND --tests-lt 100 -> only beta (150 code, 75 tests)
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::default(),
        )
        .filter(&[
            Predicate::new(Field::Code, Op::Gt, 100),
            Predicate::new(Field::Tests, Op::Lt, 100),
        ]);

        assert_eq!(qs.items.len(), 1);
        assert_eq!(qs.items[0].label, "beta");
    }

    #[test]
    fn test_filter_total_uses_full_total_regardless_of_line_types() {
        // `Field::Total` reads `Locs::total` (the precomputed all-types
        // sum, what the displayed `Total` column shows), independent of
        // which line types `LineTypes` enables for column display. This
        // is the WYSIWYF guarantee: filtering on `total` matches what
        // the user sees in the Total column.
        //
        // alpha total=75, beta total=225, gamma total=600.
        let result = sample_count_result_three_crates();

        // With everything enabled: --total-gte 200 keeps beta + gamma.
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::default(),
        )
        .filter(&[Predicate::new(Field::Total, Op::Gte, 200)]);
        assert_eq!(qs.items.len(), 2);

        // With only `code` enabled, the predicate STILL evaluates against
        // `Locs::total`, so the same 2 rows survive. Before the WYSIWYF
        // fix this returned only `gamma` because `Field::Total` was
        // redefined as "sum of currently-enabled line types", which made
        // it diverge from the visible Total column.
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::new().with_code(),
            Ordering::default(),
        )
        .filter(&[Predicate::new(Field::Total, Op::Gte, 200)]);
        assert_eq!(qs.items.len(), 2);
        let labels: Vec<_> = qs.items.iter().map(|i| i.label.as_str()).collect();
        assert!(labels.contains(&"beta"));
        assert!(labels.contains(&"gamma"));
    }

    #[test]
    fn test_filter_empty_predicates_is_noop() {
        let result = sample_count_result_three_crates();
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::default(),
        )
        .filter(&[]);
        assert_eq!(qs.items.len(), 3);
    }

    #[test]
    fn test_filter_preserves_total_and_total_items() {
        // total_items should still reflect the pre-filter row count, so the
        // table footer can render "top X of Y" honestly.
        let result = sample_count_result_three_crates();
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::default(),
        )
        .filter(&[Predicate::new(Field::Code, Op::Gte, 200)]);

        assert_eq!(qs.items.len(), 1); // only gamma
        assert_eq!(qs.total_items, 3); // pre-filter
        assert_eq!(qs.total.code, 600); // pre-filter
    }

    #[test]
    fn test_filter_chains_with_top() {
        // Filter then top. Filter keeps beta+gamma, top 1 (with default
        // label-asc ordering) keeps beta.
        let result = sample_count_result_three_crates();
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::default(),
        )
        .filter(&[Predicate::new(Field::Code, Op::Gte, 100)])
        .top(1);

        assert_eq!(qs.items.len(), 1);
        assert_eq!(qs.items[0].label, "beta");
        assert_eq!(qs.total_items, 3);
    }

    #[test]
    fn test_count_queryset_top_zero_empties_items() {
        let result = sample_count_result_three_crates();
        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByCrate,
            LineTypes::everything(),
            Ordering::by_code(),
        )
        .top(0);

        assert_eq!(qs.items.len(), 0);
        // Total preserved.
        assert_eq!(qs.total.code, 600);
    }

    #[test]
    fn test_count_queryset_by_file_relative_paths() {
        use crate::data::stats::FileStats;

        let result = CountResult {
            root: PathBuf::from("/workspace"),
            file_count: 2,
            total: sample_locs(100, 50),
            crates: vec![],
            files: vec![
                FileStats::new(PathBuf::from("/workspace/src/main.rs"), sample_locs(50, 25)),
                FileStats::new(
                    PathBuf::from("/workspace/crate-a/src/lib.rs"),
                    sample_locs(50, 25),
                ),
            ],
            modules: vec![],
        };

        let qs = CountQuerySet::from_result(
            &result,
            Aggregation::ByFile,
            LineTypes::everything(),
            Ordering::default(),
        );

        // Labels should be relative to workspace root
        assert_eq!(qs.items.len(), 2);
        assert!(qs.items.iter().any(|item| item.label == "src/main.rs"));
        assert!(qs
            .items
            .iter()
            .any(|item| item.label == "crate-a/src/lib.rs"));
    }

    fn sample_diff_result_two_files() -> crate::data::diff::DiffResult {
        use crate::data::diff::{
            CrateDiffStats, DiffResult, FileChangeType, FileDiffStats, LocsDiff,
        };
        use crate::data::stats::Locs;

        let big = LocsDiff {
            added: Locs {
                code: 200,
                tests: 0,
                examples: 0,
                docs: 0,
                comments: 0,
                blanks: 0,
                total: 200,
            },
            removed: Locs {
                code: 50,
                tests: 0,
                examples: 0,
                docs: 0,
                comments: 0,
                blanks: 0,
                total: 50,
            },
        };
        let small = LocsDiff {
            added: Locs {
                code: 10,
                tests: 0,
                examples: 0,
                docs: 0,
                comments: 0,
                blanks: 0,
                total: 10,
            },
            removed: Locs {
                code: 30,
                tests: 0,
                examples: 0,
                docs: 0,
                comments: 0,
                blanks: 0,
                total: 30,
            },
        };

        let big_file = FileDiffStats {
            path: PathBuf::from("big.rs"),
            change_type: FileChangeType::Modified,
            diff: big,
        };
        let small_file = FileDiffStats {
            path: PathBuf::from("small.rs"),
            change_type: FileChangeType::Modified,
            diff: small,
        };

        DiffResult {
            root: PathBuf::from("/workspace"),
            from_commit: "HEAD~1".to_string(),
            to_commit: "HEAD".to_string(),
            total: big + small,
            crates: vec![CrateDiffStats {
                name: "x".to_string(),
                path: PathBuf::from("/workspace"),
                diff: big + small,
                files: vec![big_file.clone(), small_file.clone()],
            }],
            files: vec![big_file, small_file],
            non_rust_added: 0,
            non_rust_removed: 0,
        }
    }

    #[test]
    fn test_diff_filter_uses_net_value() {
        // big.rs net code = +150, small.rs net code = -20.
        // --code-gt 0 keeps only big.rs.
        let result = sample_diff_result_two_files();
        let qs = DiffQuerySet::from_result(
            &result,
            Aggregation::ByFile,
            LineTypes::everything(),
            Ordering::default(),
        )
        .filter(&[Predicate::new(Field::Code, Op::Gt, 0)]);

        assert_eq!(qs.items.len(), 1);
        assert_eq!(qs.items[0].label, "big.rs");
    }

    #[test]
    fn test_diff_filter_negative_net_via_lt_zero() {
        // big.rs has net code = +150, small.rs has net code = -20.
        // The threshold here is 0 (a valid u64), but the LHS — the net
        // diff value — is signed and can be negative. So `--code-lt 0`
        // matches files whose net code change is below zero, i.e. those
        // with more code removed than added (small.rs only).
        //
        // Predicate.value is u64, so the threshold itself can't be
        // negative; we don't need it to be — the signed net LHS is what
        // makes "less than zero" meaningful.
        let result = sample_diff_result_two_files();
        let qs = DiffQuerySet::from_result(
            &result,
            Aggregation::ByFile,
            LineTypes::everything(),
            Ordering::default(),
        )
        .filter(&[Predicate::new(Field::Code, Op::Lt, 0)]);

        assert_eq!(qs.items.len(), 1);
        assert_eq!(qs.items[0].label, "small.rs");
    }

    #[test]
    fn test_diff_filter_preserves_total_items() {
        let result = sample_diff_result_two_files();
        let qs = DiffQuerySet::from_result(
            &result,
            Aggregation::ByFile,
            LineTypes::everything(),
            Ordering::default(),
        )
        .filter(&[Predicate::new(Field::Code, Op::Gte, 100)]);

        assert_eq!(qs.items.len(), 1);
        assert_eq!(qs.total_items, 2); // pre-filter
    }
}