Skip to content

Commit

Permalink
Only print and write files if contents has changed
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenperera committed Dec 20, 2023
1 parent 19c2b9e commit 71470ed
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,20 @@ fn run_on_file_paths(file_path: &Path, options: &Options) {
Ok(contents) => {
if sorter::has_classes(&contents, options) {
let sorted_content = sorter::sort_file_contents(&contents, options);
let contents_changed = sorted_content != contents;

match &options.write_mode {
WriteMode::ToStdOut => (),
WriteMode::DryRun => print_file_name(file_path, options),
WriteMode::ToFile => write_to_file(file_path, &sorted_content, options),
WriteMode::ToConsole => print_file_contents(&sorted_content),
WriteMode::CheckFormatted => {
print_changed_files(file_path, &sorted_content, &contents, options);
match (contents_changed, &options.write_mode) {
(_, WriteMode::ToStdOut) => (),
(_, WriteMode::DryRun) => print_file_name(file_path, options),

(true, WriteMode::ToFile) => write_to_file(file_path, &sorted_content, options),
(false, WriteMode::ToFile) => print_file_name(file_path, options),

(true, WriteMode::ToConsole) => print_file_contents(&sorted_content),
(false, WriteMode::ToConsole) => (),

(_, WriteMode::CheckFormatted) => {
print_changed_files(file_path, contents_changed, options);
}
}
}
Expand All @@ -170,13 +176,8 @@ fn run_on_file_paths(file_path: &Path, options: &Options) {
}
}

fn print_changed_files(
file_path: &Path,
sorted_content: &str,
original_content: &str,
options: &Options,
) {
if sorted_content != original_content {
fn print_changed_files(file_path: &Path, contents_changed: bool, options: &Options) {
if contents_changed {
if !EXIT_ERROR.load(Ordering::Relaxed) {
EXIT_ERROR.store(true, Ordering::Relaxed);
}
Expand Down

0 comments on commit 71470ed

Please sign in to comment.