provenant-cli 0.0.23

Rust-based ScanCode-compatible scanner for licenses, package metadata, SBOMs, and provenance 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
// SPDX-FileCopyrightText: Provenant contributors
// SPDX-License-Identifier: Apache-2.0

//! Position span types for license detection.

use crate::license_detection::position_set::PositionSet;

pub enum SpanIter<'a> {
    Range(std::ops::Range<usize>),
    Slice(std::iter::Copied<std::slice::Iter<'a, usize>>),
}

impl<'a> Iterator for SpanIter<'a> {
    type Item = usize;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            SpanIter::Range(range) => range.next(),
            SpanIter::Slice(iter) => iter.next(),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match self {
            SpanIter::Range(range) => range.size_hint(),
            SpanIter::Slice(iter) => iter.size_hint(),
        }
    }
}

#[derive(Debug, Clone)]
pub enum PositionSpan {
    Range { start: usize, end: usize },
    Discrete(Vec<usize>),
}

impl PartialEq for PositionSpan {
    /// Compare two PositionSpans for semantic equality.
    ///
    /// Returns true if both spans contain exactly the same positions,
    /// regardless of representation (Range vs Discrete).
    ///
    /// Performance:
    /// - Range vs Range: O(1)
    /// - Discrete vs Discrete: O(n)
    /// - Range vs Discrete: O(n) with early exit on length mismatch
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (
                PositionSpan::Range { start: s1, end: e1 },
                PositionSpan::Range { start: s2, end: e2 },
            ) => s1 == s2 && e1 == e2,
            (PositionSpan::Discrete(p1), PositionSpan::Discrete(p2)) => p1 == p2,
            (PositionSpan::Range { start, end }, PositionSpan::Discrete(positions)) => {
                let range_len = end.saturating_sub(*start);
                if range_len != positions.len() {
                    return false;
                }
                if positions.is_empty() {
                    return true;
                }
                positions.iter().all(|&p| *start <= p && p < *end)
            }
            (PositionSpan::Discrete(_), PositionSpan::Range { .. }) => other == self,
        }
    }
}

impl PositionSpan {
    pub fn range(start: usize, end: usize) -> Self {
        Self::Range { start, end }
    }

    pub fn new(start: usize, end: usize) -> Self {
        Self::range(start, end)
    }

    pub fn from_positions(positions: impl IntoIterator<Item = usize>) -> Self {
        let mut iter = positions.into_iter();
        let Some(first) = iter.next() else {
            return Self::empty();
        };

        let mut positions = vec![first];
        positions.extend(iter);

        if positions.len() == 1 {
            return Self::Range {
                start: positions[0],
                end: positions[0] + 1,
            };
        }

        positions.sort_unstable();
        positions.dedup();

        let is_contiguous = positions.windows(2).all(|w| w[1] == w[0] + 1);

        if is_contiguous {
            Self::Range {
                start: positions[0],
                end: positions[positions.len() - 1] + 1,
            }
        } else {
            Self::Discrete(positions)
        }
    }

    pub fn empty() -> Self {
        Self::Range { start: 0, end: 0 }
    }

    pub fn iter(&self) -> SpanIter<'_> {
        match self {
            PositionSpan::Range { start, end } => SpanIter::Range(*start..*end),
            PositionSpan::Discrete(positions) => SpanIter::Slice(positions.iter().copied()),
        }
    }

    pub fn len(&self) -> usize {
        match self {
            PositionSpan::Range { start, end } => end.saturating_sub(*start),
            PositionSpan::Discrete(positions) => positions.len(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn bounds(&self) -> (usize, usize) {
        match self {
            PositionSpan::Range { start, end } => (*start, *end),
            PositionSpan::Discrete(positions) => {
                if positions.is_empty() {
                    return (0, 0);
                }
                let min = positions.iter().copied().min().unwrap_or(0);
                let max = positions.iter().copied().max().unwrap_or(0);
                (min, max + 1)
            }
        }
    }

    pub fn contains(&self, pos: usize) -> bool {
        match self {
            PositionSpan::Range { start, end } => *start <= pos && pos < *end,
            PositionSpan::Discrete(positions) => positions.binary_search(&pos).is_ok(),
        }
    }

    pub fn overlaps_set(&self, set: &PositionSet) -> bool {
        let (my_min, my_max) = self.bounds();
        if self.is_empty() {
            return false;
        }
        if !set.may_overlap_range(my_min, my_max) {
            return false;
        }
        self.iter().any(|p| set.contains(p))
    }

    pub fn to_position_set(&self) -> PositionSet {
        match self {
            PositionSpan::Range { start, end } => (*start..*end).collect(),
            PositionSpan::Discrete(positions) => positions.iter().copied().collect(),
        }
    }

    pub fn to_vec(&self) -> Vec<usize> {
        match self {
            PositionSpan::Range { start, end } => (*start..*end).collect(),
            PositionSpan::Discrete(positions) => positions.clone(),
        }
    }

    /// Returns true if the positions form a contiguous range with no gaps.
    /// This is always true for `Range` variants, and checks adjacency for `Discrete`.
    pub fn is_contiguous(&self) -> bool {
        match self {
            PositionSpan::Range { .. } => true,
            PositionSpan::Discrete(positions) => {
                if positions.len() <= 1 {
                    return true;
                }
                positions.windows(2).all(|w| w[1] == w[0] + 1)
            }
        }
    }
}

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

    #[test]
    fn test_range_creation() {
        let span = PositionSpan::range(5, 10);
        assert_eq!(span.len(), 5);
        assert!(!span.is_empty());
        assert_eq!(span.bounds(), (5, 10));
        assert!(span.is_contiguous());
    }

    #[test]
    fn test_new_backwards_compatible() {
        let span = PositionSpan::new(3, 7);
        assert_eq!(span.len(), 4);
        assert_eq!(span.to_vec(), vec![3, 4, 5, 6]);
    }

    #[test]
    fn test_empty() {
        let span = PositionSpan::empty();
        assert_eq!(span.len(), 0);
        assert!(span.is_empty());
        assert_eq!(span.bounds(), (0, 0));
    }

    #[test]
    fn test_from_positions_empty() {
        let span = PositionSpan::from_positions(vec![]);
        assert!(span.is_empty());
    }

    #[test]
    fn test_from_positions_single() {
        let span = PositionSpan::from_positions(vec![5]);
        assert_eq!(span.len(), 1);
        assert!(span.is_contiguous());
        assert!(span.contains(5));
    }

    #[test]
    fn test_from_positions_contiguous() {
        let span = PositionSpan::from_positions(vec![3, 4, 5]);
        assert!(span.is_contiguous());
        assert_eq!(span.len(), 3);
        assert_eq!(span.bounds(), (3, 6));
    }

    #[test]
    fn test_from_positions_non_contiguous() {
        let span = PositionSpan::from_positions(vec![1, 3, 5]);
        assert!(!span.is_contiguous());
        assert_eq!(span.len(), 3);
        assert_eq!(span.bounds(), (1, 6));
    }

    #[test]
    fn test_from_positions_unsorted_with_duplicates() {
        let span = PositionSpan::from_positions(vec![5, 3, 4, 3, 5]);
        assert!(span.is_contiguous());
        assert_eq!(span.to_vec(), vec![3, 4, 5]);
    }

    #[test]
    fn test_contains_range() {
        let span = PositionSpan::range(5, 10);
        assert!(!span.contains(4));
        assert!(span.contains(5));
        assert!(span.contains(7));
        assert!(span.contains(9));
        assert!(!span.contains(10));
    }

    #[test]
    fn test_contains_discrete() {
        let span = PositionSpan::from_positions(vec![1, 3, 5]);
        assert!(span.contains(1));
        assert!(!span.contains(2));
        assert!(span.contains(3));
        assert!(!span.contains(4));
        assert!(span.contains(5));
    }

    #[test]
    fn test_iter_range() {
        let span = PositionSpan::range(2, 5);
        let positions: Vec<_> = span.iter().collect();
        assert_eq!(positions, vec![2, 3, 4]);
    }

    #[test]
    fn test_iter_discrete() {
        let span = PositionSpan::from_positions(vec![1, 3, 5]);
        let positions: Vec<_> = span.iter().collect();
        assert_eq!(positions, vec![1, 3, 5]);
    }

    #[test]
    fn test_to_vec_range() {
        let span = PositionSpan::range(1, 4);
        assert_eq!(span.to_vec(), vec![1, 2, 3]);
    }

    #[test]
    fn test_to_vec_discrete() {
        let span = PositionSpan::from_positions(vec![2, 4, 6]);
        assert_eq!(span.to_vec(), vec![2, 4, 6]);
    }

    #[test]
    fn test_to_position_set() {
        let span = PositionSpan::range(1, 4);
        let set = span.to_position_set();
        assert_eq!(set.len(), 3);
        assert!(set.contains(1));
        assert!(set.contains(2));
        assert!(set.contains(3));
    }

    #[test]
    fn test_is_contiguous_discrete_single() {
        let span = PositionSpan::from_positions(vec![5]);
        assert!(span.is_contiguous());
    }

    #[test]
    fn test_is_contiguous_discrete_gap() {
        let span = PositionSpan::from_positions(vec![1, 2, 4]);
        assert!(!span.is_contiguous());
    }

    // ============================================================
    // Comprehensive equality tests: all 2x2 combinations
    // ============================================================

    // ---- Range vs Range ----

    #[test]
    fn test_eq_range_range_both_empty() {
        let a = PositionSpan::range(0, 0);
        let b = PositionSpan::range(0, 0);
        assert_eq!(a, b);
    }

    #[test]
    fn test_eq_range_range_one_empty() {
        let a = PositionSpan::range(0, 0);
        let b = PositionSpan::range(0, 3);
        assert_ne!(a, b);
    }

    #[test]
    fn test_eq_range_range_equal() {
        let a = PositionSpan::range(2, 5);
        let b = PositionSpan::range(2, 5);
        assert_eq!(a, b);
    }

    #[test]
    fn test_eq_range_range_disjoint() {
        let a = PositionSpan::range(0, 3);
        let b = PositionSpan::range(5, 8);
        assert_ne!(a, b);
    }

    #[test]
    fn test_eq_range_range_overlapping() {
        let a = PositionSpan::range(0, 5);
        let b = PositionSpan::range(3, 8);
        assert_ne!(a, b);
    }

    // ---- Discrete vs Discrete ----

    #[test]
    fn test_eq_discrete_discrete_both_empty() {
        let a = PositionSpan::Discrete(vec![]);
        let b = PositionSpan::Discrete(vec![]);
        assert_eq!(a, b);
    }

    #[test]
    fn test_eq_discrete_discrete_one_empty() {
        let a = PositionSpan::Discrete(vec![]);
        let b = PositionSpan::Discrete(vec![0, 1, 2]);
        assert_ne!(a, b);
    }

    #[test]
    fn test_eq_discrete_discrete_equal() {
        let a = PositionSpan::Discrete(vec![0, 1, 2]);
        let b = PositionSpan::Discrete(vec![0, 1, 2]);
        assert_eq!(a, b);
    }

    #[test]
    fn test_eq_discrete_discrete_disjoint() {
        let a = PositionSpan::Discrete(vec![0, 1, 2]);
        let b = PositionSpan::Discrete(vec![5, 6, 7]);
        assert_ne!(a, b);
    }

    #[test]
    fn test_eq_discrete_discrete_overlapping() {
        let a = PositionSpan::Discrete(vec![0, 1, 2, 3]);
        let b = PositionSpan::Discrete(vec![2, 3, 4, 5]);
        assert_ne!(a, b);
    }

    // ---- Range vs Discrete ----

    #[test]
    fn test_eq_range_discrete_both_empty() {
        let range = PositionSpan::range(0, 0);
        let discrete = PositionSpan::Discrete(vec![]);
        assert_eq!(range, discrete);
        assert_eq!(discrete, range);
    }

    #[test]
    fn test_eq_range_discrete_range_empty() {
        let range = PositionSpan::range(0, 0);
        let discrete = PositionSpan::Discrete(vec![0, 1, 2]);
        assert_ne!(range, discrete);
        assert_ne!(discrete, range);
    }

    #[test]
    fn test_eq_range_discrete_discrete_empty() {
        let range = PositionSpan::range(0, 3);
        let discrete = PositionSpan::Discrete(vec![]);
        assert_ne!(range, discrete);
        assert_ne!(discrete, range);
    }

    #[test]
    fn test_eq_range_discrete_equal() {
        let range = PositionSpan::range(2, 5);
        let discrete = PositionSpan::Discrete(vec![2, 3, 4]);
        assert_eq!(range, discrete);
        assert_eq!(discrete, range);
    }

    #[test]
    fn test_eq_range_discrete_disjoint() {
        let range = PositionSpan::range(0, 3);
        let discrete = PositionSpan::Discrete(vec![5, 6, 7]);
        assert_ne!(range, discrete);
        assert_ne!(discrete, range);
    }

    #[test]
    fn test_eq_range_discrete_overlapping_partial() {
        let range = PositionSpan::range(0, 5);
        let discrete = PositionSpan::Discrete(vec![2, 3, 4, 5, 6]);
        assert_ne!(range, discrete);
        assert_ne!(discrete, range);
    }

    #[test]
    fn test_eq_range_discrete_subset() {
        // Discrete is subset of range
        let range = PositionSpan::range(0, 10);
        let discrete = PositionSpan::Discrete(vec![2, 3, 4]);
        assert_ne!(range, discrete);
        assert_ne!(discrete, range);
    }

    #[test]
    fn test_eq_range_discrete_superset() {
        // Discrete extends beyond range
        let range = PositionSpan::range(5, 10);
        let discrete = PositionSpan::Discrete(vec![3, 4, 5, 6, 7]);
        assert_ne!(range, discrete);
        assert_ne!(discrete, range);
    }

    // ---- Mixed empty tests ----

    #[test]
    fn test_eq_empty_different_representation() {
        let a = PositionSpan::range(0, 0);
        let b = PositionSpan::Discrete(vec![]);
        assert_eq!(a, b);
        assert_eq!(b, a);
    }
}