From 76413891ba50f16719856f2a87fe1888fd14a846 Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Mon, 1 Apr 2024 22:02:02 +0200 Subject: [PATCH] refactor: split parsing from writing file --- src/lib.rs | 45 ++++++++++++++++++++++++--------------------- src/main.rs | 6 +++--- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c7b97f0b..8efcfe84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,17 +42,7 @@ fn write_changed_line(path: &std::path::Path, dur: core::time::Duration) { } #[inline] -pub fn format_file(config: &MdsfConfig, path: &std::path::Path) -> Result<(), MdsfError> { - let time = std::time::Instant::now(); - - let input = std::fs::read_to_string(path)?; - - if input.is_empty() { - let duration = time.elapsed(); - write_unchanged_line(path, duration); - return Ok(()); - } - +fn format_file(config: &MdsfConfig, input: &str) -> (bool, String) { let mut output = String::with_capacity(input.len() + 128); let mut modified = false; @@ -108,8 +98,25 @@ pub fn format_file(config: &MdsfConfig, path: &std::path::Path) -> Result<(), Md modified = true; } + (modified, output) +} + +#[inline] +pub fn handle_file(config: &MdsfConfig, path: &std::path::Path) -> Result<(), MdsfError> { + let time = std::time::Instant::now(); + + let input = std::fs::read_to_string(path)?; + + if input.is_empty() { + let duration = time.elapsed(); + write_unchanged_line(path, duration); + return Ok(()); + } + let duration = time.elapsed(); + let (modified, output) = format_file(config, &input); + if modified && output != input { std::fs::write(path, output)?; @@ -124,8 +131,8 @@ pub fn format_file(config: &MdsfConfig, path: &std::path::Path) -> Result<(), Md } #[cfg(test)] -mod tests { - use crate::{config::MdsfConfig, format_file, formatters::setup_snippet}; +mod test_format_file { + use crate::{config::MdsfConfig, format_file}; #[test] fn it_should_format_the_code() { @@ -144,13 +151,11 @@ fn add(a: i32, b: i32) -> i32 { ``` "; - let file = setup_snippet(input, ".md").expect("it to create a file"); - let config = MdsfConfig::default(); - format_file(&config, file.path()).expect("it to format the file without errors"); + let (modified, output) = format_file(&config, input); - let output = std::fs::read_to_string(file.path()).expect("it to read the file"); + assert!(modified); assert_eq!(output, expected_output); } @@ -203,13 +208,11 @@ fn add(a: i32, b: i32) -> i32 { "; - let file = setup_snippet(input, ".md").expect("it to create a file"); - let config = MdsfConfig::default(); - format_file(&config, file.path()).expect("it to format the file without errors"); + let (modified, output) = format_file(&config, input); - let output = std::fs::read_to_string(file.path()).expect("it to read the file"); + assert!(modified); assert_eq!(output, expected_output); } diff --git a/src/main.rs b/src/main.rs index a4ac4343..7ef1dfa8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use mdsf::{ cli::{Cli, Commands, FormatCommandArguments}, config::MdsfConfig, error::MdsfError, - format_file, + handle_file, }; fn format_command(args: FormatCommandArguments) -> Result<(), MdsfError> { @@ -12,7 +12,7 @@ fn format_command(args: FormatCommandArguments) -> Result<(), MdsfError> { let conf = MdsfConfig::load(); if args.path.is_file() { - format_file(&conf, &args.path)?; + handle_file(&conf, &args.path)?; } else { for entry in ignore::WalkBuilder::new(args.path) .git_ignore(true) @@ -24,7 +24,7 @@ fn format_command(args: FormatCommandArguments) -> Result<(), MdsfError> { let file_path = entry.path(); if file_path.extension() == Some(&OsStr::from("md")) { - format_file(&conf, file_path)?; + handle_file(&conf, file_path)?; } } }