From c3e3ab1e462b4325302983ed83f21039b39c5800 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Thu, 28 Aug 2025 21:59:49 -0300 Subject: [PATCH] feat: implement filter service integration Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) --- src/services/item_service.rs | 73 +++++++++++++++++++++++++----------- src/services/mod.rs | 1 + 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/src/services/item_service.rs b/src/services/item_service.rs index 7da1913..4c15f69 100644 --- a/src/services/item_service.rs +++ b/src/services/item_service.rs @@ -2,6 +2,7 @@ use crate::common::PIPESIZE; use crate::config::Settings; use crate::services::compression_service::CompressionService; use crate::services::error::CoreError; +use crate::services::filter_service::FilterService; use crate::services::meta_service::MetaService; use crate::services::types::{ItemWithContent, ItemWithMeta}; use crate::db::{self, Meta}; @@ -19,6 +20,7 @@ pub struct ItemService { data_path: PathBuf, compression_service: CompressionService, meta_service: MetaService, + filter_service: FilterService, } impl ItemService { @@ -28,6 +30,7 @@ impl ItemService { data_path, compression_service: CompressionService::new(), meta_service: MetaService::new(), + filter_service: FilterService::new(), } } @@ -96,15 +99,43 @@ impl ItemService { /// Helper method to create a filter chain from parameters fn create_filter_chain( &self, - _grep: Option, - _head_bytes: Option, - _head_lines: Option, - _tail_bytes: Option, - _tail_lines: Option, - _filter: Option, - ) -> Result, CoreError> { - // For now, just return None since filter_service doesn't exist - Ok(None) + grep: Option, + head_bytes: Option, + head_lines: Option, + tail_bytes: Option, + tail_lines: Option, + filter: Option, + ) -> Result, CoreError> { + // Build filter string from individual parameters (for backward compatibility) + let mut filter_parts = Vec::new(); + if let Some(pattern) = grep { + filter_parts.push(format!("grep('{}')", pattern.replace('\'', "\\'"))); + } + if let Some(bytes) = head_bytes { + filter_parts.push(format!("head_bytes({})", bytes)); + } + if let Some(lines) = head_lines { + filter_parts.push(format!("head_lines({})", lines)); + } + if let Some(bytes) = tail_bytes { + filter_parts.push(format!("tail_bytes({})", bytes)); + } + if let Some(lines) = tail_lines { + filter_parts.push(format!("tail_lines({})", lines)); + } + + // Use the provided filter string if available, otherwise build from parts + let filter_str = filter.or_else(|| { + if filter_parts.is_empty() { + None + } else { + Some(filter_parts.join(" | ")) + } + }); + + // Create filter chain + self.filter_service.create_filter_chain(filter_str.as_deref()) + .map_err(|e| CoreError::InvalidInput(format!("Failed to create filter chain: {}", e))) } /// Helper method to determine if content is binary @@ -133,15 +164,15 @@ impl ItemService { &self, conn: &Connection, id: i64, - _head_bytes: Option, - _head_words: Option, - _head_lines: Option, - _tail_bytes: Option, - _tail_words: Option, - _tail_lines: Option, - _line_start: Option, - _line_end: Option, - _grep: Option, + head_bytes: Option, + head_words: Option, + head_lines: Option, + tail_bytes: Option, + tail_words: Option, + tail_lines: Option, + line_start: Option, + line_end: Option, + grep: Option, ) -> Result<(Box, String, bool), CoreError> { let item_with_meta = self.get_item(conn, id)?; let item_id = item_with_meta.item.id.ok_or_else(|| CoreError::InvalidInput("Item missing ID".to_string()))?; @@ -159,12 +190,12 @@ impl ItemService { )?; // Create filter chain - let _filter_chain = self.create_filter_chain( + let filter_chain = self.create_filter_chain( grep, head_bytes, head_lines, tail_bytes, tail_lines, None )?; - // For now, just use the original reader since filtering isn't implemented - let filtered_reader = Box::new(reader); + // Wrap the reader with filtering + let filtered_reader = Box::new(FilteringReader::new(reader, filter_chain)); let metadata = item_with_meta.meta_as_map(); let mime_type = metadata diff --git a/src/services/mod.rs b/src/services/mod.rs index 274f47f..058646a 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -1,6 +1,7 @@ pub mod async_item_service; pub mod compression_service; pub mod error; +pub mod filter_service; pub mod item_service; pub mod meta_service; pub mod status_service;