diff --git a/rustywind-cli/src/main.rs b/rustywind-cli/src/main.rs index e9af838..b0ad7e1 100644 --- a/rustywind-cli/src/main.rs +++ b/rustywind-cli/src/main.rs @@ -99,8 +99,8 @@ fn main() -> Result<()> { color_eyre::install()?; let cli = Cli::parse(); - let options = Options::new_from_cli(cli)?; - let rustywind = &options.rustywind; + let mut options = Options::new_from_cli(cli)?; + let rustywind = &mut options.rustywind; match &options.write_mode { WriteMode::ToStdOut => (), @@ -132,10 +132,11 @@ fn main() -> Result<()> { eprint!("[WARN] No classes were found in STDIN"); } } else { - options - .search_paths + let search_paths = &options.search_paths.clone(); + + search_paths .iter() - .for_each(|file_path| run_on_file_paths(file_path, &options)); + .for_each(|file_path| run_on_file_paths(file_path, &mut options)); if EXIT_ERROR.load(Ordering::Relaxed) { std::process::exit(1); @@ -145,15 +146,16 @@ fn main() -> Result<()> { Ok(()) } -fn run_on_file_paths(file_path: &Path, options: &Options) { +fn run_on_file_paths(file_path: &Path, options: &mut Options) { // if the file is in the ignored_files list return early if should_ignore_current_file(&options.ignored_files, file_path) { log::debug!("file path {file_path:#?} found in ignored_files, will not sort"); return; } - let rustywind = &options.rustywind; + options.rustywind.check_and_reset_bump(); + let rustywind = &options.rustywind; match rustywind.read_file_contents(file_path) { Ok(contents) => { if rustywind.has_classes(&contents) { diff --git a/rustywind-core/src/app.rs b/rustywind-core/src/app.rs index 13365c4..1a9f9f2 100644 --- a/rustywind-core/src/app.rs +++ b/rustywind-core/src/app.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, fs::File, io::BufRead as _, path::Path}; +use std::{borrow::Cow, fs::File, io::Read as _, path::Path}; use crate::{ class_wrapping::ClassWrapping, @@ -53,17 +53,15 @@ impl RustyWind { } } - /// Read the file contents into a `String`. - pub fn read_file_contents(&self, file_path: &Path) -> eyre::Result { - let file = File::open(file_path)?; - let mut contents = String::new_in(&self.bump); - - for line in std::io::BufReader::new(file).lines() { - contents.push_str(&line?); - contents.push('\n'); - } + /// Resets if needed the bumpalo allocator + pub fn check_and_reset_bump(&mut self) { + self.bump.reset(); + } - Ok(contents) + /// Read the file contents into a `String`. + pub fn read_file_contents(&self, file_path: &Path) -> eyre::Result { + let file_contents = std::fs::read_to_string(file_path)?; + Ok(file_contents) } /// Checks if the file contents have any classes. @@ -167,10 +165,10 @@ impl RustyWind { .collect_in(&self.bump); // sorted varaints - let mut sorted_variant_classes = Vec::new_in(&bump); + let mut sorted_variant_classes = Vec::new_in(bump); for key in VARIANTS.iter() { let (mut sorted_classes, new_custom_classes) = self.sort_variant_classes( - variants.remove(key).unwrap_or_else(|| Vec::new_in(&bump)), + variants.remove(key).unwrap_or_else(|| Vec::new_in(bump)), custom_classes, key.len() + 1, );