provenant-cli 0.0.29

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
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
// SPDX-FileCopyrightText: Provenant contributors
// SPDX-License-Identifier: Apache-2.0

//! Token string to integer ID mapping.
//!
//! TokenDictionary maps token strings to unique integer IDs. This enables
//! efficient token-based matching and indexing.

use std::collections::{BTreeMap, HashMap};

use rkyv::Archive;
use rkyv::Archived;
use serde::{Deserialize, Serialize};

#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Hash,
    PartialOrd,
    Ord,
    Serialize,
    Deserialize,
    Archive,
    rkyv::Serialize,
    rkyv::Deserialize,
)]
#[rkyv(derive(Hash, Eq, PartialEq, PartialOrd, Ord))]
pub struct TokenId(u16);

impl TokenId {
    pub const fn new(raw: u16) -> Self {
        Self(raw)
    }

    pub const fn raw(self) -> u16 {
        self.0
    }

    pub const fn as_usize(self) -> usize {
        self.0 as usize
    }

    pub const fn to_le_bytes(self) -> [u8; 2] {
        self.0.to_le_bytes()
    }
}

#[cfg(test)]
pub const fn tid(raw: u16) -> TokenId {
    TokenId::new(raw)
}

impl From<u16> for TokenId {
    fn from(value: u16) -> Self {
        Self(value)
    }
}

impl From<TokenId> for u16 {
    fn from(value: TokenId) -> Self {
        value.0
    }
}

impl PartialEq<u16> for TokenId {
    fn eq(&self, other: &u16) -> bool {
        self.0 == *other
    }
}

impl PartialOrd<u16> for TokenId {
    fn partial_cmp(&self, other: &u16) -> Option<std::cmp::Ordering> {
        self.0.partial_cmp(other)
    }
}

impl PartialEq<TokenId> for u16 {
    fn eq(&self, other: &TokenId) -> bool {
        *self == other.0
    }
}

impl PartialOrd<TokenId> for u16 {
    fn partial_cmp(&self, other: &TokenId) -> Option<std::cmp::Ordering> {
        self.partial_cmp(&other.0)
    }
}

#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Hash,
    Serialize,
    Deserialize,
    Archive,
    rkyv::Serialize,
    rkyv::Deserialize,
)]
pub enum TokenKind {
    Legalese,
    Regular,
}

#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Hash,
    Serialize,
    Deserialize,
    Archive,
    rkyv::Serialize,
    rkyv::Deserialize,
)]
pub struct KnownToken {
    pub id: TokenId,
    pub kind: TokenKind,
    pub is_digit_only: bool,
    pub is_short_or_digit: bool,
}

#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Hash,
    Serialize,
    Deserialize,
    Archive,
    rkyv::Serialize,
    rkyv::Deserialize,
)]
pub enum QueryToken {
    Known(KnownToken),
    Unknown,
    Stopword,
}

#[derive(
    Debug, Clone, Copy, Serialize, Deserialize, Archive, rkyv::Serialize, rkyv::Deserialize,
)]
pub struct TokenMetadata {
    pub kind: TokenKind,
    pub is_digit_only: bool,
    pub is_short_or_digit: bool,
}

/// Token dictionary mapping token strings to unique integer IDs.
///
/// Token IDs are assigned as follows:
/// - IDs 0 to len_legalese-1: Reserved for legalese tokens (high-value words)
/// - IDs len_legalese and above: Assigned to other tokens as encountered
///
/// The `len_legalese` delimiter allows the matching engine to distinguish
/// between high-value (legalese) tokens and regular tokens.
///
/// Based on the Python ScanCode Toolkit implementation at:
/// reference/scancode-toolkit/src/licensedcode/index.py
#[derive(Debug, Clone, Archive, rkyv::Serialize, rkyv::Deserialize)]
pub struct TokenDictionary {
    /// Mapping from token string to token ID
    tokens_to_ids: HashMap<String, TokenId>,

    token_metadata: Vec<Option<TokenMetadata>>,

    /// Number of legalese tokens (lower IDs = higher value)
    len_legalese: usize,

    /// Next token ID to assign (for non-legalese tokens)
    next_id: TokenId,
}

impl TokenDictionary {
    const DEFAULT_METADATA: TokenMetadata = TokenMetadata {
        kind: TokenKind::Regular,
        is_digit_only: false,
        is_short_or_digit: false,
    };

    /// Create a new token dictionary initialized with legalese tokens.
    ///
    /// This follows the Python ScanCode Toolkit pattern where the dictionary
    /// starts with pre-defined legalese words that get low IDs (high value).
    ///
    /// # Arguments
    /// * `legalese` - Archived BTreeMap of word → u16 pairs for legalese words.
    ///   Values are bare u16 (not TokenId) because the rkyv artifact is built
    ///   by `build.rs` which cannot depend on this crate's types.
    ///
    /// # Returns
    /// A new TokenDictionary instance with legalese tokens pre-populated
    pub fn new_with_legalese(legalese: &Archived<BTreeMap<String, u16>>) -> Self {
        let mut tokens_to_ids = HashMap::new();
        let max_existing_id = legalese
            .iter()
            .map(|(_, id)| id.to_native() as usize)
            .max()
            .unwrap_or(0);
        let mut token_metadata = vec![None; max_existing_id.saturating_add(1)];

        for (word, id) in legalese.iter() {
            let native_id = TokenId::new(id.to_native());
            tokens_to_ids.insert(word.to_string(), native_id);
            token_metadata[native_id.as_usize()] = Some(TokenMetadata {
                kind: TokenKind::Legalese,
                is_digit_only: word.chars().all(|c: char| c.is_ascii_digit()),
                is_short_or_digit: word.len() == 1
                    || word.chars().all(|c: char| c.is_ascii_digit()),
            });
        }

        let len_legalese = legalese.len();
        let next_id = TokenId::new((max_existing_id + 1).max(len_legalese) as u16);

        Self {
            tokens_to_ids,
            token_metadata,
            len_legalese,
            next_id,
        }
    }

    /// Create a new token dictionary initialized with legalese token pairs.
    ///
    /// Convenience constructor for tests that don't use the rkyv-archived legalese data.
    pub fn new_with_legalese_pairs(legalese_entries: &[(&str, u16)]) -> Self {
        let mut tokens_to_ids = HashMap::new();
        let max_existing_id = legalese_entries
            .iter()
            .map(|(_, token_id)| *token_id as usize)
            .max()
            .unwrap_or(0);
        let mut token_metadata = vec![None; max_existing_id.saturating_add(1)];

        for (word, token_id) in legalese_entries {
            let id = TokenId::from(*token_id);
            tokens_to_ids.insert(word.to_string(), id);
            token_metadata[id.as_usize()] = Some(TokenMetadata {
                kind: TokenKind::Legalese,
                is_digit_only: word.chars().all(|c| c.is_ascii_digit()),
                is_short_or_digit: word.len() == 1 || word.chars().all(|c| c.is_ascii_digit()),
            });
        }

        let len_legalese = legalese_entries.len();
        let next_id = TokenId::new((max_existing_id + 1).max(len_legalese) as u16);

        Self {
            tokens_to_ids,
            token_metadata,
            len_legalese,
            next_id,
        }
    }

    /// Create a new empty token dictionary (for testing).
    ///
    /// # Arguments
    /// * `legalese_count` - Number of reserved legalese token IDs
    ///
    /// # Returns
    /// A new TokenDictionary instance
    pub fn new(legalese_count: usize) -> Self {
        Self {
            tokens_to_ids: HashMap::new(),
            token_metadata: Vec::new(),
            len_legalese: legalese_count,
            next_id: TokenId::new(legalese_count as u16),
        }
    }

    fn metadata_for(&self, id: TokenId) -> TokenMetadata {
        self.token_metadata
            .get(id.as_usize())
            .and_then(|meta| *meta)
            .unwrap_or(Self::DEFAULT_METADATA)
    }

    fn build_known_token(&self, id: TokenId) -> KnownToken {
        let metadata = self.metadata_for(id);
        KnownToken {
            id,
            kind: metadata.kind,
            is_digit_only: metadata.is_digit_only,
            is_short_or_digit: metadata.is_short_or_digit,
        }
    }

    fn insert_metadata(&mut self, id: TokenId, kind: TokenKind, token: &str) {
        let raw = id.as_usize();
        if self.token_metadata.len() <= raw {
            self.token_metadata.resize(raw + 1, None);
        }
        self.token_metadata[raw] = Some(TokenMetadata {
            kind,
            is_digit_only: token.chars().all(|c| c.is_ascii_digit()),
            is_short_or_digit: token.len() == 1 || token.chars().all(|c| c.is_ascii_digit()),
        });
    }

    pub fn intern(&mut self, token: &str) -> KnownToken {
        if let Some(&id) = self.tokens_to_ids.get(token) {
            return self.build_known_token(id);
        }

        let id = self.next_id;
        self.next_id = TokenId::new(self.next_id.raw() + 1);
        self.tokens_to_ids.insert(token.to_string(), id);
        self.insert_metadata(id, TokenKind::Regular, token);
        self.build_known_token(id)
    }

    pub fn lookup(&self, token: &str) -> Option<KnownToken> {
        self.tokens_to_ids
            .get(token)
            .copied()
            .map(|id| self.build_known_token(id))
    }

    pub fn classify_query_token(&self, token: &str) -> QueryToken {
        self.lookup(token)
            .map_or(QueryToken::Unknown, QueryToken::Known)
    }

    pub fn token_kind(&self, token_id: TokenId) -> TokenKind {
        self.metadata_for(token_id).kind
    }

    pub fn is_digit_only_token(&self, token_id: TokenId) -> bool {
        self.metadata_for(token_id).is_digit_only
    }

    #[cfg(test)]
    pub fn get_or_assign(&mut self, token: &str) -> TokenId {
        self.intern(token).id
    }

    /// Get the token ID for a token string if it exists.
    ///
    /// # Arguments
    /// * `token` - The token string
    ///
    /// # Returns
    /// Some(token_id) if the token exists, None otherwise
    pub fn get_token_id(&self, token: &str) -> Option<TokenId> {
        self.lookup(token).map(|token| token.id)
    }

    /// Get the token ID (alias for backward compatibility).
    #[inline]
    pub fn get(&self, token: &str) -> Option<TokenId> {
        self.get_token_id(token)
    }

    /// Get the number of legalese tokens.
    pub const fn legalese_count(&self) -> usize {
        self.len_legalese
    }

    /// Get an iterator over all token string and ID pairs.
    #[cfg(test)]
    pub fn tokens_to_ids(&self) -> impl Iterator<Item = (&String, &TokenId)> {
        self.tokens_to_ids.iter()
    }

    /// Get the number of tokens in the dictionary.
    // This method will be used by the embedded index roundtrip tests in upcoming phases.
    #[allow(dead_code)]
    pub fn tokens_to_ids_len(&self) -> usize {
        self.tokens_to_ids.len()
    }
}

impl Default for TokenDictionary {
    fn default() -> Self {
        Self::new(0)
    }
}

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

    #[test]
    fn test_token_dictionary_new() {
        let dict = TokenDictionary::new(10);
        assert_eq!(dict.legalese_count(), 10);
        assert_eq!(dict.tokens_to_ids.len(), 0);
        assert!(dict.tokens_to_ids.is_empty());
    }

    #[test]
    fn test_new_with_legalese() {
        let legalese = [
            ("license".to_string(), 0u16),
            ("copyright".to_string(), 1u16),
            ("permission".to_string(), 2u16),
        ];

        let mut dict = TokenDictionary::new_with_legalese_pairs(
            &legalese
                .iter()
                .map(|(s, i)| (s.as_str(), *i))
                .collect::<Vec<_>>(),
        );

        assert_eq!(dict.legalese_count(), 3);
        assert_eq!(dict.tokens_to_ids.len(), 3);
        assert!(!dict.tokens_to_ids.is_empty());

        // Check that legalese tokens are registered
        assert_eq!(dict.get_token_id("license"), Some(tid(0)));
        assert_eq!(dict.get_token_id("copyright"), Some(tid(1)));
        assert_eq!(dict.get_token_id("permission"), Some(tid(2)));

        // Check that new tokens get IDs starting after legalese
        let test_id = dict.get_or_assign("test");
        assert_eq!(test_id, 3);
    }

    #[test]
    fn test_new_with_legalese_sorted() {
        let legalese = [
            ("copyright".to_string(), 5u16),
            ("license".to_string(), 0u16),
            ("permission".to_string(), 10u16),
        ];

        let mut dict = TokenDictionary::new_with_legalese_pairs(
            &legalese
                .iter()
                .map(|(s, i)| (s.as_str(), *i))
                .collect::<Vec<_>>(),
        );

        assert_eq!(dict.legalese_count(), 3);
        assert_eq!(dict.tokens_to_ids.len(), 3);

        // Check legalese IDs are correct regardless of input order
        assert_eq!(dict.get_token_id("copyright"), Some(tid(5)));
        assert_eq!(dict.get_token_id("license"), Some(tid(0)));
        assert_eq!(dict.get_token_id("permission"), Some(tid(10)));

        // Next ID should advance past the highest explicit legalese token ID.
        let test_id = dict.get_or_assign("test");
        assert_eq!(test_id, tid(11));
    }

    #[test]
    fn test_get_or_assign_new_token() {
        let mut dict = TokenDictionary::new(5);

        let id1 = dict.get_or_assign("hello");
        let id2 = dict.get_or_assign("world");

        // Should assign IDs starting at legalese_count (5)
        assert_eq!(id1, 5);
        assert_eq!(id2, 6);
        assert_eq!(dict.tokens_to_ids.len(), 2);
    }

    #[test]
    fn test_get_or_assign_existing_token() {
        let mut dict = TokenDictionary::new(5);

        let id1 = dict.get_or_assign("hello");
        let id2 = dict.get_or_assign("hello");

        // Should return the same ID for the same token
        assert_eq!(id1, id2);
        assert_eq!(dict.tokens_to_ids.len(), 1);
    }

    #[test]
    fn test_get_or_assign_with_preexisting_legalese() {
        let legalese = [("license".to_string(), 0u16)];
        let mut dict = TokenDictionary::new_with_legalese_pairs(
            &legalese
                .iter()
                .map(|(s, i)| (s.as_str(), *i))
                .collect::<Vec<_>>(),
        );

        // Legalese tokens should already exist
        let id = dict.get_or_assign("license");
        assert_eq!(id, 0);
        assert_eq!(dict.tokens_to_ids.len(), 1);

        // New tokens should get IDs after legalese
        let new_id = dict.get_or_assign("new");
        assert_eq!(new_id, 1);
        assert_eq!(dict.tokens_to_ids.len(), 2);
    }

    #[test]
    fn test_get_existing_token() {
        let mut dict = TokenDictionary::new(5);

        dict.get_or_assign("hello");
        assert_eq!(dict.get_token_id("hello"), Some(tid(5)));
    }

    #[test]
    fn test_get_nonexistent_token() {
        let dict = TokenDictionary::new(5);
        assert_eq!(dict.get_token_id("hello"), None);
    }

    #[test]
    fn test_legalese_range() {
        let dict = TokenDictionary::new(10);

        // IDs 0-9 are legalese
        assert!(0 < dict.legalese_count() as u16);
        assert!(5 < dict.legalese_count() as u16);
        assert!(9 < dict.legalese_count() as u16);

        // ID 10+ are not legalese
        assert!(10 >= dict.legalese_count() as u16);
        assert!(100 >= dict.legalese_count() as u16);
    }

    #[test]
    fn test_legalese_range_with_actual_legalese() {
        let legalese = [
            ("license".to_string(), 0u16),
            ("copyright".to_string(), 1u16),
        ];

        let mut dict = TokenDictionary::new_with_legalese_pairs(
            &legalese
                .iter()
                .map(|(s, i)| (s.as_str(), *i))
                .collect::<Vec<_>>(),
        );

        // Legalese tokens should have IDs in the legalese range
        assert!(dict.get_token_id("license").unwrap() < dict.legalese_count() as u16);
        assert!(dict.get_token_id("copyright").unwrap() < dict.legalese_count() as u16);

        // Regular tokens should not be legalese
        let regular_id = dict.get_or_assign("regular");
        assert!(regular_id >= dict.legalese_count() as u16);
    }

    #[test]
    fn test_token_dictionary_default() {
        let dict = TokenDictionary::default();
        assert_eq!(dict.legalese_count(), 0);
        assert!(dict.tokens_to_ids.is_empty());
    }

    #[test]
    fn test_get_alias() {
        let mut dict = TokenDictionary::new(5);
        dict.get_or_assign("hello");

        // get() should be an alias for get_token_id()
        assert_eq!(dict.get("hello"), dict.get_token_id("hello"));
    }

    #[test]
    fn test_with_actual_legalese_module() {
        use crate::license_detection::rules::legalese;

        let legalese = legalese::archived_legalese();
        assert!(!legalese.is_empty(), "Should have legalese words");

        let mut dict = TokenDictionary::new_with_legalese(legalese);

        // Verify dictionary has the right structure
        assert_eq!(dict.legalese_count(), legalese.len());
        assert_eq!(dict.tokens_to_ids.len(), legalese.len());

        // Verify some legalese words are correctly registered
        let license_id = dict.get_token_id("license");
        assert!(license_id.is_some(), "License should be in dictionary");
        assert!(
            license_id.unwrap() < dict.legalese_count() as u16,
            "License should be a legalese token"
        );

        // Note: Standalone "copyright" is NOT in the Python reference dictionary
        // Only compound words like "copyrighted", "copyrights" are present
        let copyrighted_id = dict.get_token_id("copyrighted");
        assert!(
            copyrighted_id.is_some(),
            "Copyrighted should be in dictionary"
        );
        assert!(
            copyrighted_id.unwrap() < dict.legalese_count() as u16,
            "Copyrighted should be a legalese token"
        );

        // New tokens should get IDs after legalese
        let hello_id = dict.get_or_assign("hello");
        assert!(hello_id >= dict.legalese_count() as u16);
        assert!(hello_id >= dict.legalese_count() as u16);
    }
}