Skip to content

Commit

Permalink
Implement formatting of files with no extension
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Sep 3, 2024
1 parent 2664753 commit a13133d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
28 changes: 5 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#![allow(clippy::module_name_repetitions)]

use clap::Parser;
use log::Level::Error;
use std::fs;
use std::process::exit;

Expand Down Expand Up @@ -51,29 +50,12 @@ fn main() {

for file in &args.files {
let mut logs = Vec::<Log>::new();
let extension_valid = check_extension_valid(file);
if extension_valid {
if let Ok(text) = fs::read_to_string(file) {
let new_text = format_file(&text, file, &args, &mut logs);
if args.print {
println!("{}", &new_text);
} else if args.check && text != new_text {
record_file_log(
&mut logs,
Error,
file,
"Incorrect formatting.",
);
exit_code = 1;
} else if text != new_text {
write_file(file, &new_text);
}
} else {
record_file_log(&mut logs, Error, file, "Could not open file.");
exit_code = 1;
}
if let Some(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 {
record_file_log(&mut logs, Error, file, "File type invalid.");
exit_code = 1;
};

Expand Down
28 changes: 22 additions & 6 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Utilities for reading the command line arguments

use crate::logging::*;
use crate::regexes::*;
use clap::Parser;

/// Acceptable file extensions
const EXTENSIONS: [&str; 4] = [".tex", ".bib", ".sty", ".cls"];
use log::Level::Error;
use std::fs;

/// Command line arguments
#[allow(missing_docs)]
Expand Down Expand Up @@ -46,7 +47,22 @@ impl Cli {
}
}

/// Verify the file extension
pub fn check_extension_valid(file: &str) -> bool {
EXTENSIONS.iter().any(|e| file.ends_with(e))
/// Add a missing extension and read the file
pub fn read(file: &str, logs: &mut Vec<Log>) -> Option<String> {
// check if file has an accepted extension
let has_ext = EXTENSIONS.iter().any(|e| file.ends_with(e));
// if no valid extension, try adding .tex
let mut new_file = file.to_owned();
if !has_ext {
new_file.push_str(".tex");
};
if let Ok(text) = fs::read_to_string(&new_file) {
return Some(text);
}
if has_ext {
record_file_log(logs, Error, file, "Could not open file.");
} else {
record_file_log(logs, Error, file, "File type invalid.");
}
None
}
3 changes: 3 additions & 0 deletions src/regexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub const DOC_END: &str = "\\end{document}";
pub const ENV_BEGIN: &str = "\\begin{";
/// Match a LaTeX \end{...}
pub const ENV_END: &str = "\\end{";
/// Acceptable LaTeX file extensions
pub const EXTENSIONS: [&str; 4] = [".tex", ".bib", ".sty", ".cls"];

/// Names of LaTeX list environments
const LISTS: [&str; 5] = [
Expand All @@ -30,6 +32,7 @@ const LISTS: [&str; 5] = [
/// Names of LaTeX verbatim environments
const VERBATIMS: [&str; 4] = ["verbatim", "Verbatim", "lstlisting", "minted"];

// Regexes
lazy_static! {
pub static ref RE_NEWLINES: Regex =
Regex::new(&format!(r"{LINE_END}{LINE_END}({LINE_END})+")).unwrap();
Expand Down
26 changes: 25 additions & 1 deletion src/write.rs
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
}

0 comments on commit a13133d

Please sign in to comment.