refactor: extract binary detection and word line count logic into helper methods
Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
@@ -111,27 +111,10 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
self.is_finalized = finalized;
|
||||
}
|
||||
|
||||
fn update(&mut self, data: &[u8]) -> MetaPluginResponse {
|
||||
// If already finalized, don't process more data
|
||||
if self.is_finalized {
|
||||
return MetaPluginResponse {
|
||||
metadata: Vec::new(),
|
||||
is_finalized: true,
|
||||
};
|
||||
}
|
||||
|
||||
/// Helper method to perform binary detection and return appropriate metadata
|
||||
/// Returns (metadata, should_finalize) tuple
|
||||
fn perform_binary_detection(&mut self, buffer: &[u8]) -> (Vec<crate::meta_plugin::MetaData>, bool) {
|
||||
let mut metadata = Vec::new();
|
||||
|
||||
// If we haven't determined if content is binary yet, build buffer and check
|
||||
if self.is_binary_content.is_none() {
|
||||
if let Some(buffer) = &mut self.buffer {
|
||||
// Add data to our buffer up to max_buffer_size
|
||||
let remaining_capacity = self.max_buffer_size.saturating_sub(buffer.len());
|
||||
let bytes_to_take = std::cmp::min(data.len(), remaining_capacity);
|
||||
buffer.extend_from_slice(&data[..bytes_to_take]);
|
||||
|
||||
// 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 is_binary_result = is_binary(buffer);
|
||||
self.is_binary_content = Some(is_binary_result);
|
||||
|
||||
@@ -156,8 +139,64 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
|
||||
(metadata, is_binary_result)
|
||||
}
|
||||
|
||||
/// Helper method to output word and line counts
|
||||
fn output_word_line_counts(&mut self) -> Vec<crate::meta_plugin::MetaData> {
|
||||
let mut metadata = Vec::new();
|
||||
|
||||
// Process any remaining data in utf8_buffer
|
||||
if !self.utf8_buffer.is_empty() {
|
||||
self.count_text_stats(&[]);
|
||||
}
|
||||
|
||||
// Output word and line counts
|
||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
"text_word_count",
|
||||
self.word_count.to_string(),
|
||||
self.base.outputs()
|
||||
) {
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
|
||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
"text_line_count",
|
||||
self.line_count.to_string(),
|
||||
self.base.outputs()
|
||||
) {
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
|
||||
metadata
|
||||
}
|
||||
|
||||
fn update(&mut self, data: &[u8]) -> MetaPluginResponse {
|
||||
// If already finalized, don't process more data
|
||||
if self.is_finalized {
|
||||
return MetaPluginResponse {
|
||||
metadata: Vec::new(),
|
||||
is_finalized: true,
|
||||
};
|
||||
}
|
||||
|
||||
let mut metadata = Vec::new();
|
||||
|
||||
// If we haven't determined if content is binary yet, build buffer and check
|
||||
if self.is_binary_content.is_none() {
|
||||
if let Some(buffer) = &mut self.buffer {
|
||||
// Add data to our buffer up to max_buffer_size
|
||||
let remaining_capacity = self.max_buffer_size.saturating_sub(buffer.len());
|
||||
let bytes_to_take = std::cmp::min(data.len(), remaining_capacity);
|
||||
buffer.extend_from_slice(&data[..bytes_to_take]);
|
||||
|
||||
// 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 (binary_metadata, is_binary) = self.perform_binary_detection(buffer);
|
||||
metadata.extend(binary_metadata);
|
||||
|
||||
// If it's binary, we're done with this plugin
|
||||
if is_binary_result {
|
||||
if is_binary {
|
||||
self.buffer = None; // Drop the buffer
|
||||
self.is_finalized = true;
|
||||
return MetaPluginResponse {
|
||||
@@ -213,31 +252,11 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
if self.is_binary_content.is_none() {
|
||||
if let Some(buffer) = &self.buffer {
|
||||
if !buffer.is_empty() {
|
||||
let is_binary_result = is_binary(buffer);
|
||||
self.is_binary_content = Some(is_binary_result);
|
||||
|
||||
// Output text and binary status
|
||||
let text_value = if is_binary_result { "false".to_string() } else { "true".to_string() };
|
||||
let binary_value = if is_binary_result { "true".to_string() } else { "false".to_string() };
|
||||
|
||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
"text",
|
||||
text_value,
|
||||
self.base.outputs()
|
||||
) {
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
|
||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
"binary",
|
||||
binary_value,
|
||||
self.base.outputs()
|
||||
) {
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
let (binary_metadata, is_binary) = self.perform_binary_detection(buffer);
|
||||
metadata.extend(binary_metadata);
|
||||
|
||||
// If it's binary, we're done
|
||||
if is_binary_result {
|
||||
if is_binary {
|
||||
self.buffer = None; // Drop the buffer
|
||||
self.is_finalized = true;
|
||||
return MetaPluginResponse {
|
||||
@@ -251,27 +270,8 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
|
||||
// If content is text, output word and line counts
|
||||
if self.is_binary_content == Some(false) {
|
||||
// Process any remaining data in utf8_buffer
|
||||
if !self.utf8_buffer.is_empty() {
|
||||
self.count_text_stats(&[]);
|
||||
}
|
||||
|
||||
// Output word and line counts
|
||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
"text_word_count",
|
||||
self.word_count.to_string(),
|
||||
self.base.outputs()
|
||||
) {
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
|
||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
"text_line_count",
|
||||
self.line_count.to_string(),
|
||||
self.base.outputs()
|
||||
) {
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
let word_line_metadata = self.output_word_line_counts();
|
||||
metadata.extend(word_line_metadata);
|
||||
}
|
||||
|
||||
// Drop the buffer since we're done with it
|
||||
|
||||
Reference in New Issue
Block a user