1+ #![ warn( clippy:: pedantic) ]
12use clap:: Parser ;
23use compact_security_detectors:: all_detectors;
34use compact_security_detectors_sdk:: {
@@ -7,7 +8,7 @@ use compact_security_detectors_sdk::{
78use libloading:: { Library , Symbol } ;
89use parser:: Cli ;
910use serde_json:: { json, Map } ;
10- use std:: collections:: HashMap ;
11+ use std:: { collections:: HashMap , path :: PathBuf } ;
1112
1213mod parser;
1314
@@ -46,17 +47,18 @@ fn main() {
4647 corpus. insert ( path. to_string_lossy ( ) . to_string ( ) , file_content) ;
4748 }
4849 }
50+ let mut files_scanned = Vec :: new ( ) ;
51+ let mut detector_responses = Map :: new ( ) ;
4952 if !corpus. is_empty ( ) {
50- let result = execute_detectors ( & corpus, detectors, load_lib) ;
53+ let result = execute_detectors ( & corpus, detectors. as_ref ( ) , load_lib) ;
5154
52- let files_scanned: Vec < String > = corpus
55+ files_scanned = corpus
5356 . keys ( )
54- . map ( |k| relative_file_path ( k, & project_root) )
57+ . map ( |k| relative_file_path ( k, project_root. as_ref ( ) ) )
5558 . collect ( ) ;
5659
57- let mut detector_responses = Map :: new ( ) ;
5860 for ( detector_name, errors) in result {
59- let instances = detector_result_to_json ( errors, & project_root) ;
61+ let instances = detector_result_to_json ( errors, project_root. as_ref ( ) ) ;
6062
6163 let detector_response = json ! ( {
6264 "findings" : [
@@ -69,15 +71,14 @@ fn main() {
6971 } ) ;
7072 detector_responses. insert ( detector_name, detector_response) ;
7173 }
72-
73- let res = json ! ( {
74- "errors" : [ ] ,
75- "scanned" : files_scanned,
76- "detector_responses" : detector_responses,
77- } ) ;
78-
79- println ! ( "{}" , serde_json:: to_string_pretty( & res) . unwrap( ) ) ;
8074 }
75+ let res = json ! ( {
76+ "errors" : [ ] ,
77+ "scanned" : files_scanned,
78+ "detector_responses" : detector_responses,
79+ } ) ;
80+
81+ println ! ( "{}" , serde_json:: to_string_pretty( & res) . unwrap( ) ) ;
8182 }
8283 parser:: Commands :: Metadata => {
8384 println ! ( "{}" , get_scanner_metadata( ) ) ;
@@ -87,7 +88,7 @@ fn main() {
8788
8889fn execute_detectors (
8990 files : & HashMap < String , String > ,
90- rules : Option < Vec < String > > ,
91+ rules : Option < & Vec < String > > ,
9192 load_lib : Option < std:: path:: PathBuf > ,
9293) -> HashMap < String , Vec < DetectorResult > > {
9394 let codebase = build_codebase ( files) . unwrap ( ) ;
@@ -107,7 +108,7 @@ fn execute_detectors(
107108 let selected_detectors: Vec < _ > = available_detectors ( )
108109 . into_iter ( )
109110 . filter ( |detector| {
110- if let Some ( ref rules) = rules {
111+ if let Some ( rules) = rules {
111112 rules. contains ( & detector. id ( ) . to_string ( ) )
112113 || ( rules. len ( ) == 1 && rules[ 0 ] . eq_ignore_ascii_case ( "all" ) )
113114 } else {
@@ -127,7 +128,7 @@ fn execute_detectors(
127128
128129fn detector_result_to_json (
129130 errors : Vec < DetectorResult > ,
130- project_root : & Option < std :: path :: PathBuf > ,
131+ project_root : Option < & PathBuf > ,
131132) -> serde_json:: Value {
132133 let mut json_errors = Vec :: new ( ) ;
133134 for error in errors {
@@ -145,7 +146,7 @@ fn detector_result_to_json(
145146 json ! ( json_errors)
146147}
147148
148- fn relative_file_path ( file_path : & str , project_root : & Option < std :: path :: PathBuf > ) -> String {
149+ fn relative_file_path ( file_path : & str , project_root : Option < & PathBuf > ) -> String {
149150 if let Some ( root) = project_root {
150151 if let Ok ( relative_path) = std:: path:: Path :: new ( file_path) . strip_prefix ( root) {
151152 relative_path. to_string_lossy ( ) . to_string ( )
@@ -186,7 +187,7 @@ fn get_scanner_metadata() -> String {
186187 "report" : {
187188 "severity" : detector. severity( ) ,
188189 "tags" : detector. tags( ) ,
189- "template" : yml_string_to_json( detector. template( ) )
190+ "template" : yml_string_to_json( & detector. template( ) )
190191 }
191192 } ) ;
192193 detectors. push ( json_detector) ;
@@ -202,6 +203,6 @@ fn get_scanner_metadata() -> String {
202203 serde_json:: to_string_pretty ( & scanner_json) . unwrap ( )
203204}
204205
205- fn yml_string_to_json ( yml_string : String ) -> Option < serde_json:: Value > {
206- serde_yaml:: from_str :: < serde_json:: Value > ( & yml_string) . ok ( )
206+ fn yml_string_to_json ( yml_string : & str ) -> Option < serde_json:: Value > {
207+ serde_yaml:: from_str :: < serde_json:: Value > ( yml_string) . ok ( )
207208}
0 commit comments