diff --git a/src/filter_plugin/mod.rs b/src/filter_plugin/mod.rs index 95d6e1c..4ccb6b6 100644 --- a/src/filter_plugin/mod.rs +++ b/src/filter_plugin/mod.rs @@ -31,7 +31,10 @@ impl FilterChain { pub fn process(&mut self, data: &[u8]) -> Result> { let mut current_data = data.to_vec(); for plugin in &mut self.plugins { - current_data = plugin.process(¤t_data)?; + // Process the current data through the plugin + let processed = plugin.process(¤t_data)?; + current_data = processed; + // Early exit if no data remains if current_data.is_empty() { break; @@ -42,20 +45,15 @@ impl FilterChain { pub fn finish(&mut self) -> Result> { let mut result = Vec::new(); - let mut all_data = Vec::new(); - + + // Process each plugin's finish method and collect results for plugin in &mut self.plugins { - let processed = plugin.finish()?; - if !processed.is_empty() { - all_data.extend(processed); + let finished_data = plugin.finish()?; + if !finished_data.is_empty() { + result.extend(finished_data); } } - - // If we have any data from finish, use it - if !all_data.is_empty() { - result = all_data; - } - + Ok(result) } } diff --git a/src/services/filter_service.rs b/src/services/filter_service.rs index a3ff476..089d911 100644 --- a/src/services/filter_service.rs +++ b/src/services/filter_service.rs @@ -31,4 +31,29 @@ impl FilterService { Ok(Vec::new()) } } + + // Add a method to process data through the filter chain and handle finish automatically + pub fn process_all_data(&self, chain: &mut Option, data: &[u8]) -> Result> { + let mut processed = if let Some(chain) = chain { + chain.process(data)? + } else { + data.to_vec() + }; + + // If we have a chain, also get any remaining data from finish() + if chain.is_some() { + let finished = self.finish_processing(chain)?; + if !finished.is_empty() { + processed.extend(finished); + } + } + + Ok(processed) + } + + // Helper method to create and process data with a filter string in one call + pub fn process_with_filter(&self, data: &[u8], filter_str: Option<&str>) -> Result> { + let mut chain = self.create_filter_chain(filter_str)?; + self.process_all_data(&mut chain, data) + } }