Skip to main content

provenant/scanner/
mod.rs

1// SPDX-FileCopyrightText: Provenant contributors
2// SPDX-License-Identifier: Apache-2.0
3
4mod collect;
5pub(crate) mod process;
6
7use crate::license_detection::LicenseDetectionEngine;
8use crate::models::FileInfo;
9
10pub struct ProcessResult {
11    pub files: Vec<FileInfo>,
12    pub excluded_count: usize,
13}
14
15#[derive(Debug, Clone, Copy)]
16pub struct LicenseScanOptions {
17    pub include_text: bool,
18    pub include_text_diagnostics: bool,
19    pub include_diagnostics: bool,
20    pub unknown_licenses: bool,
21    pub enable_sequence_matching: bool,
22    pub min_score: u8,
23}
24
25impl Default for LicenseScanOptions {
26    fn default() -> Self {
27        Self {
28            include_text: false,
29            include_text_diagnostics: false,
30            include_diagnostics: false,
31            unknown_licenses: false,
32            enable_sequence_matching: true,
33            min_score: 0,
34        }
35    }
36}
37
38#[derive(Debug, Clone)]
39pub struct TextDetectionOptions {
40    pub collect_info: bool,
41    pub detect_packages: bool,
42    pub detect_application_packages: bool,
43    pub detect_system_packages: bool,
44    pub detect_packages_in_compiled: bool,
45    pub detect_copyrights: bool,
46    pub detect_generated: bool,
47    pub detect_emails: bool,
48    pub detect_urls: bool,
49    pub max_emails: usize,
50    pub max_urls: usize,
51    pub timeout_seconds: f64,
52}
53
54impl Default for TextDetectionOptions {
55    fn default() -> Self {
56        Self {
57            collect_info: false,
58            detect_packages: false,
59            detect_application_packages: false,
60            detect_system_packages: false,
61            detect_packages_in_compiled: false,
62            detect_copyrights: true,
63            detect_generated: false,
64            detect_emails: false,
65            detect_urls: false,
66            max_emails: 50,
67            max_urls: 50,
68            timeout_seconds: 120.0,
69        }
70    }
71}
72
73pub fn scan_options_fingerprint(
74    text_options: &TextDetectionOptions,
75    license_options: LicenseScanOptions,
76    license_engine: Option<&LicenseDetectionEngine>,
77) -> String {
78    let (license_enabled, dataset_fingerprint, rules_count, first_rule_id, last_rule_id) =
79        match license_engine {
80            Some(engine) => {
81                let rules = &engine.index().rules_by_rid;
82                (
83                    true,
84                    engine
85                        .license_index_provenance()
86                        .map(|provenance| provenance.dataset_fingerprint.as_str())
87                        .unwrap_or(""),
88                    rules.len(),
89                    rules
90                        .first()
91                        .map(|rule| rule.identifier.as_str())
92                        .unwrap_or(""),
93                    rules
94                        .last()
95                        .map(|rule| rule.identifier.as_str())
96                        .unwrap_or(""),
97                )
98            }
99            None => (false, "", 0, "", ""),
100        };
101
102    format!(
103        "tool_version={};info={};packages={};app_packages={};system_packages={};compiled_packages={};copyrights={};generated={};emails={};urls={};max_emails={};max_urls={};timeout={:.6};license_enabled={};license_dataset_fingerprint={};rules_count={};first_rule_id={};last_rule_id={};license_text={};license_text_diagnostics={};license_diagnostics={};unknown_licenses={};sequence_matching={};license_score={}",
104        crate::version::BUILD_VERSION,
105        text_options.collect_info,
106        text_options.detect_packages,
107        text_options.detect_application_packages,
108        text_options.detect_system_packages,
109        text_options.detect_packages_in_compiled,
110        text_options.detect_copyrights,
111        text_options.detect_generated,
112        text_options.detect_emails,
113        text_options.detect_urls,
114        text_options.max_emails,
115        text_options.max_urls,
116        text_options.timeout_seconds,
117        license_enabled,
118        dataset_fingerprint,
119        rules_count,
120        first_rule_id,
121        last_rule_id,
122        license_options.include_text,
123        license_options.include_text_diagnostics,
124        license_options.include_diagnostics,
125        license_options.unknown_licenses,
126        license_options.enable_sequence_matching,
127        license_options.min_score,
128    )
129}
130
131pub use self::collect::{
132    CollectedPaths, CollectionFrontier, CollectionLimits, collect_paths, collect_paths_with_limits,
133    collect_selected_paths, collect_selected_paths_with_limits,
134};
135#[allow(unused_imports)]
136pub use self::process::{
137    MemoryMode, process_collected, process_collected_sequential,
138    process_collected_with_memory_limit, process_collected_with_memory_limit_sequential,
139};
140
141#[cfg(test)]
142mod tests;