Skip to content

Commit

Permalink
Combine read and read_stdin functions
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Dec 6, 2024
1 parent 43881d3 commit 1812930
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
25 changes: 25 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use crate::args::*;
use crate::ignore::*;
use crate::indent::*;
use crate::logging::*;
use crate::read::*;
use crate::regexes::{ENV_BEGIN, ENV_END, ITEM, RE_SPLITTING};
use crate::subs::*;
use crate::verbatim::*;
use crate::wrap::*;
use crate::write::*;
use crate::LINE_END;
use log::Level::{Info, Warn};
use std::iter::zip;
Expand Down Expand Up @@ -233,3 +235,26 @@ impl Pattern {
const fn indents_return_to_zero(state: &State) -> bool {
state.indent.actual == 0
}

/// Run tex-fmt with the provided arguments
pub fn run(args: &Args, logs: &mut Vec<Log>) -> u8 {
let mut exit_code = 0;
if args.stdin {
if let Some((file, text)) = read_stdin(logs) {
let new_text = format_file(&text, &file, args, logs);
exit_code = process_output(args, &file, &text, &new_text, logs);
} else {
exit_code = 1;
}
} else {
for file in &args.files {
if let Some((file, text)) = read(file, logs) {
let new_text = format_file(&text, &file, args, logs);
exit_code = process_output(args, &file, &text, &new_text, logs);
} else {
exit_code = 1;
}
}
}
exit_code
}
25 changes: 1 addition & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ mod write;
use crate::args::*;
use crate::format::*;
use crate::logging::*;
use crate::read::*;
use crate::write::*;

#[cfg(test)]
mod tests;
Expand All @@ -53,28 +51,7 @@ fn main() -> ExitCode {
let mut exit_code = args.resolve(&mut logs);

if exit_code == 0 {
if args.stdin {
// TODO combine the read and read_stdin functions to simplify this
if let Some((file, text)) = read_stdin(&mut logs) {
let new_text = format_file(&text, &file, &args, &mut logs);
exit_code = process_output(
&args, &file, &text, &new_text, exit_code, &mut logs,
);
} else {
exit_code = 1;
}
} else {
for file in &args.files {
if let Some((file, text)) = read(file, &mut logs) {
let new_text = format_file(&text, &file, &args, &mut logs);
exit_code = process_output(
&args, &file, &text, &new_text, exit_code, &mut logs,
);
} else {
exit_code = 1;
};
}
}
exit_code = run(&args, &mut logs);
}

print_logs(&mut logs);
Expand Down
6 changes: 2 additions & 4 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ pub fn process_output(
file: &str,
text: &str,
new_text: &str,
exit_code: u8,
logs: &mut Vec<Log>,
) -> u8 {
let mut new_exit_code = exit_code;
if args.print {
print!("{}", &new_text);
} else if args.check && text != new_text {
record_file_log(logs, Error, file, "Incorrect formatting.");
new_exit_code = 1;
return 1;
} else if text != new_text {
write_file(file, new_text);
}
new_exit_code
0
}

0 comments on commit 1812930

Please sign in to comment.