fix: resolve mutable borrow conflicts in text meta plugin

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-26 19:49:57 -03:00
parent adc16bd761
commit 6becdb4fbd

View File

@@ -185,16 +185,20 @@ impl MetaPlugin for TextMetaPlugin {
// If we haven't determined if content is binary yet, build buffer and check // If we haven't determined if content is binary yet, build buffer and check
if self.is_binary_content.is_none() { if self.is_binary_content.is_none() {
if let Some(buffer) = &mut self.buffer { let should_finalize = if let Some(ref mut buffer) = self.buffer {
// Add data to our buffer up to max_buffer_size // Add data to our buffer up to max_buffer_size
let remaining_capacity = self.max_buffer_size.saturating_sub(buffer.len()); let remaining_capacity = self.max_buffer_size.saturating_sub(buffer.len());
let bytes_to_take = std::cmp::min(data.len(), remaining_capacity); let bytes_to_take = std::cmp::min(data.len(), remaining_capacity);
buffer.extend_from_slice(&data[..bytes_to_take]); buffer.extend_from_slice(&data[..bytes_to_take]);
// If we have enough data to make a binary determination, do it now // If we have enough data to make a binary determination, do it now
if buffer.len() >= std::cmp::min(1024, self.max_buffer_size) { let buffer_len = buffer.len();
let (binary_metadata, is_binary) = self.perform_binary_detection(buffer); if buffer_len >= std::cmp::min(1024, self.max_buffer_size) {
// Clone the buffer data for binary detection to avoid borrowing conflicts
let buffer_clone = buffer.clone();
let (binary_metadata, is_binary) = self.perform_binary_detection(&buffer_clone);
metadata.extend(binary_metadata); metadata.extend(binary_metadata);
self.is_binary_content = Some(is_binary);
// If it's binary, we're done with this plugin // If it's binary, we're done with this plugin
if is_binary { if is_binary {
@@ -210,14 +214,27 @@ impl MetaPlugin for TextMetaPlugin {
self.count_text_stats(&data[..bytes_to_take]); self.count_text_stats(&data[..bytes_to_take]);
// If we've reached our buffer limit, drop the buffer and finalize // If we've reached our buffer limit, drop the buffer and finalize
if buffer.len() >= self.max_buffer_size { if buffer_len >= self.max_buffer_size {
self.buffer = None; // Drop the buffer self.buffer = None; // Drop the buffer
self.is_finalized = true; self.is_finalized = true;
true
} else {
false
} }
} else { } else {
// Still building up buffer, count words and lines for this chunk // Still building up buffer, count words and lines for this chunk
self.count_text_stats(&data[..bytes_to_take]); self.count_text_stats(&data[..bytes_to_take]);
false
} }
} else {
false
};
if should_finalize {
return MetaPluginResponse {
metadata,
is_finalized: true,
};
} }
} else if self.is_binary_content == Some(false) { } else if self.is_binary_content == Some(false) {
// We've already determined it's text, just count words and lines // We've already determined it's text, just count words and lines
@@ -253,8 +270,11 @@ impl MetaPlugin for TextMetaPlugin {
if self.is_binary_content.is_none() { if self.is_binary_content.is_none() {
if let Some(buffer) = &self.buffer { if let Some(buffer) = &self.buffer {
if !buffer.is_empty() { if !buffer.is_empty() {
let (binary_metadata, is_binary) = self.perform_binary_detection(buffer); // Clone the buffer data for binary detection to avoid borrowing conflicts
let buffer_clone = buffer.clone();
let (binary_metadata, is_binary) = self.perform_binary_detection(&buffer_clone);
metadata.extend(binary_metadata); metadata.extend(binary_metadata);
self.is_binary_content = Some(is_binary);
// If it's binary, we're done // If it's binary, we're done
if is_binary { if is_binary {