-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement formatting of files with no extension
- Loading branch information
1 parent
2664753
commit a13133d
Showing
4 changed files
with
55 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,34 @@ | ||
//! Utilities for writing formatted files | ||
|
||
use crate::fs; | ||
use crate::logging::*; | ||
use crate::parse::*; | ||
use log::Level::Error; | ||
use std::path; | ||
|
||
/// Write a formatted file to disk | ||
pub fn write_file(file: &str, text: &str) { | ||
fn write_file(file: &str, text: &str) { | ||
let filepath = path::Path::new(&file).canonicalize().unwrap(); | ||
fs::write(filepath, text).expect("Could not write the file"); | ||
} | ||
|
||
/// Handle the newly formatted file | ||
pub fn process_output( | ||
args: &Cli, | ||
file: &str, | ||
text: &str, | ||
new_text: &str, | ||
exit_code: i32, | ||
logs: &mut Vec<Log>, | ||
) -> i32 { | ||
let mut new_exit_code = exit_code; | ||
if args.print { | ||
println!("{}", &new_text); | ||
} else if args.check && text != new_text { | ||
record_file_log(logs, Error, file, "Incorrect formatting."); | ||
new_exit_code = 1; | ||
} else if text != new_text { | ||
write_file(file, new_text); | ||
} | ||
new_exit_code | ||
} |