Skip to content

Commit

Permalink
refactor: split parsing from writing file
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Apr 1, 2024
1 parent 3b94fb5 commit 7641389
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
45 changes: 24 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)?;

Expand All @@ -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() {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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)
Expand All @@ -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)?;
}
}
}
Expand Down

0 comments on commit 7641389

Please sign in to comment.