fix: remove unused imports and fix tail filter errors

Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-29 10:18:18 -03:00
parent b7237595df
commit 3cfcbf0edd
4 changed files with 23 additions and 23 deletions

View File

@@ -29,19 +29,21 @@ impl FilterPlugin for GrepFilter {
// Split into lines // Split into lines
for (i, &byte) in self.buffer.iter().enumerate() { for (i, &byte) in self.buffer.iter().enumerate() {
if byte == b'\n' { if byte == b'\n' {
lines.push(&self.buffer[start..=i]); let line = &self.buffer[start..=i];
lines.push(line.to_vec());
start = i + 1; start = i + 1;
} }
} }
// Keep the remaining data in buffer // Keep the remaining data in buffer
self.buffer.drain(0..start); let remaining = self.buffer.split_off(start);
self.buffer = remaining;
// Filter lines that match the regex // Filter lines that match the regex
for line in lines { for line in lines {
if let Ok(line_str) = std::str::from_utf8(line) { if let Ok(line_str) = std::str::from_utf8(&line) {
if self.regex.is_match(line_str) { if self.regex.is_match(line_str) {
result.extend_from_slice(line); result.extend_from_slice(&line);
} }
} }
} }

View File

@@ -1,6 +1,4 @@
use std::io::{Read, Result}; use std::io::Result;
use regex::Regex;
use ringbuf::HeapRb;
pub mod head; pub mod head;
pub mod tail; pub mod tail;
@@ -43,7 +41,7 @@ impl FilterChain {
Ok(current_data) Ok(current_data)
} }
pub fn finish(&mut self) -> Result<Vec<u8>> { fn finish(&mut self) -> Result<Vec<u8>> {
let mut result = Vec::new(); let mut result = Vec::new();
// Process each plugin's finish method and collect results // Process each plugin's finish method and collect results
@@ -73,7 +71,7 @@ pub fn parse_filter_string(filter_str: &str) -> Result<FilterChain> {
($prefix:expr, $suffix:expr, $constructor:expr) => {{ ($prefix:expr, $suffix:expr, $constructor:expr) => {{
if let Some(stripped) = part.strip_prefix($prefix).and_then(|s| s.strip_suffix($suffix)) { if let Some(stripped) = part.strip_prefix($prefix).and_then(|s| s.strip_suffix($suffix)) {
let count = utils::parse_number(stripped)?; let count = utils::parse_number(stripped)?;
chain.add_plugin($constructor(count)); chain.add_plugin(Box::new($constructor(count)));
continue; continue;
} }
}}; }};
@@ -88,12 +86,12 @@ pub fn parse_filter_string(filter_str: &str) -> Result<FilterChain> {
} }
// Handle other filters using the macro // Handle other filters using the macro
parse_filter!("head_bytes(", ")", |count| Box::new(head::HeadBytesFilter::new(count))); parse_filter!("head_bytes(", ")", head::HeadBytesFilter::new);
parse_filter!("head_lines(", ")", |count| Box::new(head::HeadLinesFilter::new(count))); parse_filter!("head_lines(", ")", head::HeadLinesFilter::new);
parse_filter!("tail_bytes(", ")", |count| Box::new(tail::TailBytesFilter::new(count))); parse_filter!("tail_bytes(", ")", tail::TailBytesFilter::new);
parse_filter!("tail_lines(", ")", |count| Box::new(tail::TailLinesFilter::new(count))); parse_filter!("tail_lines(", ")", tail::TailLinesFilter::new);
parse_filter!("skip_bytes(", ")", |count| Box::new(skip::SkipBytesFilter::new(count))); parse_filter!("skip_bytes(", ")", skip::SkipBytesFilter::new);
parse_filter!("skip_lines(", ")", |count| Box::new(skip::SkipLinesFilter::new(count))); parse_filter!("skip_lines(", ")", skip::SkipLinesFilter::new);
// If we get here, the filter wasn't recognized // If we get here, the filter wasn't recognized
return Err(std::io::Error::new( return Err(std::io::Error::new(

View File

@@ -1,6 +1,6 @@
use super::FilterPlugin; use super::FilterPlugin;
use std::io::Result; use std::io::Result;
use ringbuf::HeapRb; use ringbuf::{HeapRb, Rb};
pub struct TailBytesFilter { pub struct TailBytesFilter {
ring_buffer: HeapRb<u8>, ring_buffer: HeapRb<u8>,
@@ -8,11 +8,11 @@ pub struct TailBytesFilter {
} }
impl TailBytesFilter { impl TailBytesFilter {
pub fn new(count: usize) -> Result<Self> { pub fn new(count: usize) -> Self {
Ok(Self { Self {
ring_buffer: HeapRb::new(count), ring_buffer: HeapRb::new(count),
count, count,
}) }
} }
} }
@@ -40,12 +40,12 @@ pub struct TailLinesFilter {
} }
impl TailLinesFilter { impl TailLinesFilter {
pub fn new(count: usize) -> Result<Self> { pub fn new(count: usize) -> Self {
Ok(Self { Self {
ring_buffer: HeapRb::new(count * 256), // Estimate 256 bytes per line ring_buffer: HeapRb::new(count * 256), // Estimate 256 bytes per line
count, count,
lines_found: 0, lines_found: 0,
}) }
} }
} }

View File

@@ -31,5 +31,5 @@ pub fn create_filter_chain(filter_str: &str) -> Result<Option<super::FilterChain
/// Helper function to parse a number from a string with error handling /// Helper function to parse a number from a string with error handling
pub fn parse_number<T: std::str::FromStr>(s: &str) -> Result<T> { pub fn parse_number<T: std::str::FromStr>(s: &str) -> Result<T> {
s.parse::<T>() s.parse::<T>()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e)) .map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid number"))
} }