Skip to content

Commit

Permalink
Passing cli args around functions better
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 11, 2024
1 parent 8cb1993 commit 7740a8f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
9 changes: 5 additions & 4 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
use crate::indent::*;
use crate::subs::*;
use crate::wrap::*;
use crate::Cli;

const MAX_WRAP_TRY: u8 = 3;

pub fn format_file(file: &str, debug: bool) -> String {
pub fn format_file(file: &str, args: &Cli) -> String {
let mut new_file = remove_extra_newlines(file);
new_file = begin_end_environments_new_line(&new_file);
new_file = remove_tabs(&new_file);
new_file = remove_trailing_spaces(&new_file);
new_file = apply_indent(&new_file, debug);
new_file = apply_indent(&new_file, args);

let mut wrap_tries = 0;
while needs_wrap(&new_file) && wrap_tries < MAX_WRAP_TRY {
wrap_tries += 1;
new_file = wrap(&new_file);
new_file = wrap(&new_file, args);
new_file = remove_trailing_spaces(&new_file);
new_file = apply_indent(&new_file, debug);
new_file = apply_indent(&new_file, args);
}

new_file
Expand Down
7 changes: 4 additions & 3 deletions src/indent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::comments::*;
use crate::regexes::*;
use crate::TAB;
use crate::Cli;
use core::cmp::max;

const OPENS: [char; 3] = ['(', '[', '{'];
Expand Down Expand Up @@ -105,7 +106,7 @@ pub fn get_indent(line: &str, prev_indent: Indent) -> Indent {
Indent { actual, visual }
}

pub fn apply_indent(file: &str, debug: bool) -> String {
pub fn apply_indent(file: &str, args: &Cli) -> String {
let mut indent = Indent::new();
let mut new_file = "".to_owned();
let mut verbatim_count = 0;
Expand All @@ -119,7 +120,7 @@ pub fn apply_indent(file: &str, debug: bool) -> String {
let comment_index = find_comment_index(line);
let line_strip = remove_comment(line, comment_index);
indent = get_indent(line_strip, indent);
if !debug {
if !args.debug {
assert!(indent.actual >= 0, "line {}: {}", i, line);
assert!(indent.visual >= 0, "line {}: {}", i, line);
};
Expand All @@ -142,7 +143,7 @@ pub fn apply_indent(file: &str, debug: bool) -> String {
}

// check indents return to zero
if !debug {
if !args.debug {
assert!(indent.actual == 0);
assert!(indent.visual == 0);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() {

print_script_name();

for filename in args.filenames {
for filename in &args.filenames {
if args.debug {
print_file_name(&filename);
}
Expand All @@ -49,7 +49,7 @@ fn main() {
let file =
fs::read_to_string(&filename).expect("Should have read the file");

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

if args.print {
print_file(&new_file);
Expand Down
6 changes: 6 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ pub struct Cli {
#[arg(required = true)]
pub filenames: Vec<String>,
}

impl Cli {
pub fn new() -> Self {
Cli{indent: false, print: false, verbose: false, debug: false, filenames: vec![]}
}
}
7 changes: 5 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod tests {

use crate::apply;
use crate::Cli;
use crate::format_file;
use crate::rstest;
use crate::template;
Expand Down Expand Up @@ -32,11 +33,12 @@ mod tests {

#[apply(test_file)]
fn test_in_file(filename: &str, extension: &str) {
let args = Cli::new();
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_in_file = format_file(&in_file, &args);
assert!(fmt_in_file == out_file,
"\n{}Test failed: {}{}{} -> {}{}{}\n\n{}Output:\n{}{}{}\nDesired:\n{}{}",
&RED,
Expand All @@ -56,9 +58,10 @@ mod tests {

#[apply(test_file)]
fn test_out_file(filename: &str, extension: &str) {
let args = Cli::new();
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);
let fmt_out_file = format_file(&out_file, &args);
assert!(fmt_out_file == out_file,
"\n{}Test failed: {}{}{} -> {}{}{}\n\n{}Output:\n{}{}{}\nDesired:\n{}{}",
&RED,
Expand Down
22 changes: 13 additions & 9 deletions src/wrap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::comments::*;
use crate::regexes::*;
use crate::Cli;

const WRAP: usize = 80;

Expand Down Expand Up @@ -28,7 +29,7 @@ pub fn find_wrap_point(line: &str) -> Option<usize> {
wrap_point
}

pub fn wrap_line(line: &str) -> String {
pub fn wrap_line(line: &str, args: &Cli) -> String {
let mut remaining_line = line.to_string();
let mut new_line = "".to_string();
let mut can_wrap = true;
Expand All @@ -55,24 +56,26 @@ pub fn wrap_line(line: &str) -> String {
}
None => {
can_wrap = false;
println!("long line cannot be wrapped!");
println!("{}", remaining_line);
if args.debug {
eprintln!("long line cannot be wrapped!");
eprintln!("{}", remaining_line);
}
}
}
}
new_line.push_str(&remaining_line);
new_line
}

pub fn wrap(file: &str) -> String {
pub fn wrap(file: &str, args: &Cli) -> String {
let mut new_file = "".to_string();
let mut verbatim_count = 0;
for line in file.lines() {
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
if line_needs_wrap(line) && verbatim_count == 0 {
let new_line = wrap_line(line);
let new_line = wrap_line(line, args);
new_file.push_str(&new_line);
} else {
new_file.push_str(line);
Expand All @@ -88,27 +91,28 @@ pub fn wrap(file: &str) -> String {
#[cfg(test)]
#[test]
fn test_wrap_line() {
let args = Cli::new();
// no comment
let s_in = "This line is too long because it has more than eighty characters inside it. \
Therefore it should be split.";
let s_out = "This line is too long because it has more than eighty characters inside it.\n \
Therefore it should be split.";
assert_eq!(wrap_line(s_in), s_out);
assert_eq!(wrap_line(s_in, &args), s_out);
// break before comment
let s_in = "This line is too long because it has more than eighty characters inside it. \
Therefore it % should be split.";
let s_out = "This line is too long because it has more than eighty characters inside it.\n \
Therefore it % should be split.";
assert_eq!(wrap_line(s_in), s_out);
assert_eq!(wrap_line(s_in, &args), s_out);
// break after comment
let s_in = "This line is too long because % it has more than eighty characters inside it. \
Therefore it should be split.";
let s_out = "This line is too long because % it has more than eighty characters inside it.\n\
% Therefore it should be split.";
assert_eq!(wrap_line(s_in), s_out);
assert_eq!(wrap_line(s_in, &args), s_out);
// leading spaces
let s_in = " Thislineistoolongbecauseithasmorethaneightycharactersinsideiteventhoughitstartswithspaces. \
Thereforeitshouldbesplit.";
let s_out = s_in;
assert_eq!(wrap_line(s_in), s_out);
assert_eq!(wrap_line(s_in, &args), s_out);
}

0 comments on commit 7740a8f

Please sign in to comment.