Skip to content

Commit

Permalink
New parser flags
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 4, 2024
1 parent 1701551 commit 76e6a3b
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 85 deletions.
328 changes: 296 additions & 32 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ description = "LaTeX formatter written in Rust"
clap = { version = "=4.4.0", features = ["derive"] }
lazy_static = "1.4.0"
regex = "1.10.3"
rstest = "0.19.0"
rstest_reuse = "0.6.0"
9 changes: 1 addition & 8 deletions notes.org
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@
* Tests
** Look for problem cases in other latex documents
* Features
** Flags
*** -d Dry run
*** -p Print to STDOUT
*** -v Info verbose
*** -vv Debug verbose
** Implement new flags
** Ignore source lines
*** Line-by-line ignore
*** Block ignore
** Log file
** Documentation
** Tidy up code
** Find todos
** Tests better output
** Better file backup
* Bugs
** Better errors including line numbers in source file
Expand Down
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pkgs.mkShell {
rustfmt
clippy
cargo-flamegraph
#cargo-nextest
cacert
hyperfine
texlive.combined.scheme-full
Expand Down
2 changes: 1 addition & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn format_file(file: &str, debug: bool) -> String {
new_file = wrap(&new_file);
new_file = remove_trailing_spaces(&new_file);
new_file = apply_indent(&new_file, debug);
};
}

new_file
}
12 changes: 8 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use clap::Parser;
#[allow(unused_imports)]
use rstest::rstest;
#[allow(unused_imports)]
use rstest_reuse::{self, *};
use std::fs;

const TAB: i8 = 2;
Expand All @@ -22,10 +26,10 @@ mod tests;

fn main() {
// get arguments
let args = Cli::parse();
let mut print = args.print;
let mut args = Cli::parse();
if args.debug {
print = true;
args.print = true;
args.verbose = true;
};

// check files are in correct format
Expand All @@ -47,7 +51,7 @@ fn main() {

let new_file = format_file(&file, args.debug);

if print {
if args.print {
print_file(&new_file);
} else {
backup_file(&filename);
Expand Down
13 changes: 6 additions & 7 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ use clap::Parser;

#[derive(Parser)]
pub struct Cli {
#[arg(long, short, help = "Print to stdout, do not modify files")]
#[arg(long, short, help = "Indent only, do not modify line breaks")]
pub indent: bool,
#[arg(long, short, help = "Print to STDOUT, do not modify files")]
pub print: bool,
#[arg(
long,
short,
help = "Debug mode, disable checks and do not modify files"
)]
#[arg(long, short, help = "Increase verbosity")]
pub verbose: bool,
#[arg(long, short, help = "Debug, do not modify files")]
pub debug: bool,
#[arg(required = true)]
pub filenames: Vec<String>,
}

57 changes: 32 additions & 25 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
mod tests {

use crate::apply;
use crate::format_file;
use crate::rstest;
use crate::template;
use std::fs;

const YELLOW: &str = "\x1b[33m\x1b[1m";
Expand All @@ -9,13 +12,31 @@ mod tests {
const WHITE: &str = "\x1b[37m\x1b[1m";
const RESET: &str = "\x1b[00m\x1b[0m";

fn test_file(filename: &str, extension: &str) {
#[template]
#[rstest]
#[case::brackets("brackets", "tex")]
#[case::comments("comments", "tex")]
#[case::document("document", "tex")]
#[case::environment_lines("environment_lines", "tex")]
#[case::lists("lists", "tex")]
#[case::masters_dissertation("masters_dissertation", "tex")]
#[case::phd_dissertation("phd_dissertation", "tex")]
#[case::phd_dissertation_refs("phd_dissertation_refs", "bib")]
#[case::pu_thesis("pu_thesis", "cls")]
#[case::readme("readme", "tex")]
#[case::short_document("short_document", "tex")]
#[case::tikz_network("tikz_network", "sty")]
#[case::verbatim("verbatim", "tex")]
#[case::wrap("wrap", "tex")]
fn test_file(#[case] filename: &str, #[case] extension: &str) {}

#[apply(test_file)]
fn test_in_file(filename: &str, extension: &str) {
let in_filename = format!("tests/{}_in.{}", filename, extension);
let out_filename = format!("tests/{}_out.{}", filename, extension);
let in_file = fs::read_to_string(&in_filename).expect("");
let out_file = fs::read_to_string(&out_filename).expect("");
let fmt_in_file = format_file(&in_file, false);
let fmt_out_file = format_file(&out_file, false);
assert!(fmt_in_file == out_file,
"\n{}Test failed: {}{}{} -> {}{}{}\n\n{}Output:\n{}{}{}\nDesired:\n{}{}",
&RED,
Expand All @@ -31,6 +52,14 @@ mod tests {
&YELLOW,
&RESET,
&out_file);
println!("{}Pass: {}{}", &GREEN, &RESET, &in_filename);
}

#[apply(test_file)]
fn test_out_file(filename: &str, extension: &str) {
let out_filename = format!("tests/{}_out.{}", filename, extension);
let out_file = fs::read_to_string(&out_filename).expect("");
let fmt_out_file = format_file(&out_file, false);
assert!(fmt_out_file == out_file,
"\n{}Test failed: {}{}{} -> {}{}{}\n\n{}Output:\n{}{}{}\nDesired:\n{}{}",
&RED,
Expand All @@ -46,28 +75,6 @@ mod tests {
&YELLOW,
&RESET,
&out_file);
println!("{}Pass: {}{}", &GREEN, &RESET, &in_filename);
}

#[test]
fn test_files() {
let filenames: Vec<String> = fs::read_dir("tests/")
.unwrap()
.map(|f| f.unwrap().file_name().into_string().unwrap())
.filter(|f| f.contains("_in."))
.collect();
let extensions: Vec<String> = filenames
.iter()
.map(|f| f[(f.len() - 3)..f.len()].to_string())
.collect();
let filenames: Vec<String> = filenames
.iter()
.map(|f| f[0..(f.len() - 7)].to_string())
.collect();
for i in 0..filenames.len() {
let filename = &filenames[i];
let extension = &extensions[i];
test_file(&filename, &extension);
}
println!("{}Pass: {}{}", &GREEN, &RESET, &out_filename);
}
}
11 changes: 5 additions & 6 deletions src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ pub fn wrap_line(line: &str) -> String {
Some(c) => {
if p > c {
"%"
}
else {
} else {
""
}
}
None => ""
None => "",
};
new_line.push_str(&remaining_line[0..p]);
new_line.push('\n');
remaining_line = remaining_line[p..remaining_line.len()].to_string();
remaining_line =
remaining_line[p..remaining_line.len()].to_string();
remaining_line.insert_str(0, line_start);
},
}
None => {
can_wrap = false;
println!("long line cannot be wrapped!");
Expand All @@ -77,7 +77,6 @@ pub fn wrap(file: &str) -> String {
}

#[cfg(test)]

#[test]
fn test_wrap_line() {
// no comment
Expand Down
3 changes: 1 addition & 2 deletions src/write.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::fs;
use std::path;
use std::env::temp_dir;
use std::path;

pub fn backup_file(filename: &str) {
let filepath = path::Path::new(&filename).canonicalize().unwrap();
Expand All @@ -15,4 +15,3 @@ pub fn write_file(filename: &str, new_file: &str) {
let filepath = path::Path::new(&filename).canonicalize().unwrap();
fs::write(filepath, new_file).unwrap();
}

0 comments on commit 76e6a3b

Please sign in to comment.