Skip to content

Commit

Permalink
Better file extension checking
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 12, 2024
1 parent 32a1d97 commit 73d1c32
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
27 changes: 10 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod regexes;
mod subs;
mod wrap;
mod write;
use crate::colors::*;
use crate::format::*;
use crate::logging::*;
use crate::parse::*;
Expand All @@ -36,31 +37,23 @@ fn main() {
args.verbose = true;
};

// initialize logger
init_logger(&args);

// check files are in correct format
let extensions = [".tex", ".bib", ".sty", ".cls"];
for f in &args.filenames {
let mut extension_valid = false;
for extension in extensions {
if f.ends_with(extension) {
extension_valid = true;
}
}
if !extension_valid {
log::error!("File type invalid for {}", f);
panic!();
}
}

print_script_name();

for filename in &args.filenames {
if args.verbose {
print_file_name(filename);
}

// check file is in correct format
match check_extension(filename) {
Ok(_) => (),
Err(_) => {
log::error!("File type invalid for {}{}", WHITE, filename);
continue;
}
};

// read lines from file
let file =
fs::read_to_string(filename).expect("Should have read the file");
Expand Down
10 changes: 10 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clap::Parser;

const EXTENSIONS: [&str; 4] = [".tex", ".bib", ".sty", ".cls"];

#[derive(Parser)]
pub struct Cli {
#[arg(long, short, help = "Indent only, do not modify line breaks")]
Expand All @@ -26,3 +28,11 @@ impl Cli {
}
}
}

pub fn check_extension(filename: &str) -> Result<(), ()> {
let extension_valid = EXTENSIONS.iter().any(|e| filename.ends_with(e));
match extension_valid {
true => Ok(()),
false => Err(()),
}
}

0 comments on commit 73d1c32

Please sign in to comment.