@@ -11,30 +11,26 @@ use std::path::PathBuf;
1111use thiserror:: Error ;
1212
1313/// A wrapper interpreter that applies filtering to recognized compiler commands.
14- pub struct FilteringInterpreter < T : Interpreter > {
14+ pub ( super ) struct FilteringInterpreter < T : Interpreter > {
1515 inner : T ,
16- filter : Filter ,
16+ filter : Option < Filter > ,
1717}
1818
1919impl < T : Interpreter > FilteringInterpreter < T > {
20- /// Creates a new filtering interpreter that wraps another interpreter.
21- fn new ( inner : T , filter : Filter ) -> Self {
22- Self { inner, filter }
23- }
24-
2520 /// Creates a filtering interpreter from configuration.
26- pub fn from_config (
27- inner : T ,
28- compilers : & [ config:: Compiler ] ,
29- sources : & config:: SourceFilter ,
30- ) -> Result < Self , InterpreterConfigError > {
31- let filter = Filter :: try_from ( ( compilers, sources) ) ?;
32- Ok ( Self :: new ( inner, filter) )
21+ pub fn from_filter ( inner : T , filter : Filter ) -> Self {
22+ Self {
23+ inner,
24+ filter : Some ( filter) ,
25+ }
3326 }
3427
3528 /// Creates a pass-through filtering interpreter (no filtering applied).
3629 pub fn pass_through ( inner : T ) -> Self {
37- Self :: new ( inner, Filter :: Skip )
30+ Self {
31+ inner,
32+ filter : None ,
33+ }
3834 }
3935}
4036
@@ -43,53 +39,45 @@ impl<T: Interpreter> Interpreter for FilteringInterpreter<T> {
4339 // First, let the inner interpreter recognize the command
4440 let command = self . inner . recognize ( execution) ?;
4541
46- match command {
47- Command :: Compiler ( compiler_cmd) => {
48- // Apply filtering to the compiler command
49- match self . filter . filter_command ( & compiler_cmd) {
50- Ok ( ( ) ) => Some ( Command :: Compiler ( compiler_cmd) ) ,
51- Err ( reason) => Some ( Command :: Filtered ( reason) ) ,
42+ if let Some ( filter) = & self . filter {
43+ // If a filter is set, apply it to the recognized command
44+ match command {
45+ Command :: Compiler ( compiler_cmd) => {
46+ // Apply filtering to the compiler command
47+ match filter. filter_command ( & compiler_cmd) {
48+ Ok ( _) => Some ( Command :: Compiler ( compiler_cmd) ) ,
49+ Err ( reason) => Some ( Command :: Filtered ( reason) ) ,
50+ }
5251 }
52+ // Pass through other command types unchanged
53+ other => Some ( other) ,
5354 }
54- // Pass through other command types unchanged
55- other => Some ( other) ,
55+ } else {
56+ // If no filter is set, return the command as is
57+ Some ( command)
5658 }
5759 }
5860}
5961
6062#[ derive( Debug ) ]
61- enum Filter {
62- /// Apply filtering according to the configuration
63- Filter {
64- compiler_filters : HashMap < PathBuf , config:: IgnoreOrConsider > ,
65- source_filters : Vec < config:: DirectoryFilter > ,
66- } ,
67- /// Skip filtering (pass through unchanged)
68- Skip ,
63+ pub ( super ) struct Filter {
64+ compiler_filters : HashMap < PathBuf , config:: IgnoreOrConsider > ,
65+ source_filters : Vec < config:: DirectoryFilter > ,
6966}
7067
7168impl Filter {
7269 fn filter_command ( & self , cmd : & CompilerCommand ) -> Result < ( ) , String > {
73- match self {
74- Filter :: Skip => Ok ( ( ) ) ,
75- Filter :: Filter {
76- compiler_filters,
77- source_filters,
78- } => {
79- // Check if the compiler should be filtered
80- if let Some ( reason) = self . should_filter_compiler ( & cmd. executable , compiler_filters)
81- {
82- return Err ( reason) ;
83- }
84-
85- // Check if any source files should be filtered
86- if let Some ( reason) = self . should_filter_sources ( cmd, source_filters) {
87- return Err ( reason) ;
88- }
70+ // Check if the compiler should be filtered
71+ if let Some ( reason) = self . should_filter_compiler ( & cmd. executable , & self . compiler_filters ) {
72+ return Err ( reason) ;
73+ }
8974
90- Ok ( ( ) )
91- }
75+ // Check if any source files should be filtered
76+ if let Some ( reason) = self . should_filter_sources ( cmd, & self . source_filters ) {
77+ return Err ( reason) ;
9278 }
79+
80+ Ok ( ( ) )
9381 }
9482
9583 fn should_filter_compiler (
@@ -282,7 +270,7 @@ impl TryFrom<(&[config::Compiler], &config::SourceFilter)> for Filter {
282270 // Validate source configuration
283271 let source_filters = Self :: validate_source_configuration ( sources) ?;
284272
285- Ok ( Self :: Filter {
273+ Ok ( Self {
286274 compiler_filters,
287275 source_filters,
288276 } )
@@ -355,8 +343,9 @@ mod tests {
355343 . times ( 1 )
356344 . return_const ( Some ( Command :: Compiler ( mock_cmd) ) ) ;
357345
358- let sut =
359- FilteringInterpreter :: from_config ( mock_interpreter, & compilers, & sources) . unwrap ( ) ;
346+ let filter =
347+ Filter :: try_from ( ( compilers. as_slice ( ) , & sources) ) . expect ( "Failed to create filter" ) ;
348+ let sut = FilteringInterpreter :: from_filter ( mock_interpreter, filter) ;
360349
361350 let execution = Execution :: from_strings (
362351 "/usr/bin/gcc" ,
@@ -392,8 +381,9 @@ mod tests {
392381 . times ( 1 )
393382 . return_const ( Some ( Command :: Compiler ( mock_cmd) ) ) ;
394383
395- let sut =
396- FilteringInterpreter :: from_config ( mock_interpreter, & compilers, & sources) . unwrap ( ) ;
384+ let filter =
385+ Filter :: try_from ( ( compilers. as_slice ( ) , & sources) ) . expect ( "Failed to create filter" ) ;
386+ let sut = FilteringInterpreter :: from_filter ( mock_interpreter, filter) ;
397387
398388 let execution = Execution :: from_strings (
399389 "/usr/bin/gcc" ,
0 commit comments