Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Sep 2, 2024
1 parent e93f52c commit 09d2fc4
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ pub fn remove_comment(line: &str, comment: Option<usize>) -> String {
}

pub fn get_comment(line: &str, comment: Option<usize>) -> String {
comment.map_or_else(|| "".to_string(), |c| line.chars().skip(c).collect())
comment.map_or_else(String::new, |c| line.chars().skip(c).collect())
}
14 changes: 8 additions & 6 deletions src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn get_diff(line: &str) -> i8 {
diff += 1;
for re_list_begin in RE_LISTS_BEGIN.iter() {
if re_list_begin.is_match(line) {
diff += 1
diff += 1;
};
}
} else if line.contains(ENV_END) {
Expand All @@ -52,14 +52,16 @@ fn get_diff(line: &str) -> i8 {
diff -= 1;
for re_list_end in RE_LISTS_END.iter() {
if re_list_end.is_match(line) {
diff -= 1
diff -= 1;
};
}
};

// indent for delimiters
diff += line.chars().filter(|x| OPENS.contains(x)).count() as i8;
diff -= line.chars().filter(|x| CLOSES.contains(x)).count() as i8;
diff += i8::try_from(line.chars().filter(|x| OPENS.contains(x)).count())
.unwrap();
diff -= i8::try_from(line.chars().filter(|x| CLOSES.contains(x)).count())
.unwrap();

diff
}
Expand All @@ -71,8 +73,8 @@ fn get_back(line: &str) -> i8 {

// delimiters
for c in line.chars() {
cumul -= OPENS.contains(&c) as i8;
cumul += CLOSES.contains(&c) as i8;
cumul -= i8::from(OPENS.contains(&c));
cumul += i8::from(CLOSES.contains(&c));
back = max(cumul, back);
}

Expand Down
12 changes: 6 additions & 6 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::colors::*;
use crate::Cli;
use env_logger::Builder;
use log::Level;
use log::Level::{Error, Info, Trace, Warn};
use log::Level::{Debug, Error, Info, Trace, Warn};
use log::LevelFilter;
use std::cmp::Reverse;
use std::io::Write;
Expand Down Expand Up @@ -76,7 +76,7 @@ fn get_log_style(log_level: Level) -> String {
Warn => YELLOW.to_string(),
Error => RED.to_string(),
Trace => GREEN.to_string(),
_ => panic!(),
Debug => panic!(),
}
}

Expand Down Expand Up @@ -125,16 +125,16 @@ pub fn print_logs(mut logs: Vec<Log>) {
for log in logs {
let linum_new = log
.linum_new
.map_or_else(|| "".to_string(), |i| format!("Line {} ", i));
.map_or_else(String::new, |i| format!("Line {i} "));

let linum_old = log
.linum_old
.map_or_else(|| "".to_string(), |i| format!("({}). ", i));
.map_or_else(String::new, |i| format!("({i}). "));

let line = log
.line
.as_ref()
.map_or_else(|| "".to_string(), |l| l.trim_start().to_string());
.map_or_else(String::new, |l| l.trim_start().to_string());

let log_string = format!(
"{}tex-fmt {}{}: {}{}{}{}{} {}{}",
Expand All @@ -155,7 +155,7 @@ pub fn print_logs(mut logs: Vec<Log>) {
Warn => log::warn!("{}", log_string),
Info => log::info!("{}", log_string),
Trace => log::trace!("{}", log_string),
_ => panic!(),
Debug => panic!(),
}
}
}
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//#![warn(missing_docs)]
#![warn(clippy::nursery)]
//#![warn(clippy::cargo)]
#![warn(clippy::cargo)]
//#![warn(clippy::missing_docs_in_private_items)]
//#![warn(clippy::pedantic)]
//#![allow(clippy::wildcard_imports)]
//#![allow(clippy::module_name_repetitions)]
#![warn(clippy::pedantic)]
#![allow(clippy::wildcard_imports)]
#![allow(clippy::struct_excessive_bools)]
#![allow(clippy::module_name_repetitions)]

use clap::Parser;
use log::Level::Error;
Expand Down
13 changes: 6 additions & 7 deletions src/regexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,24 @@ const LEAVES: [&str; 4] = ["verbatim", "Verbatim", "lstlisting", "minted"];

lazy_static! {
pub static ref RE_NEWLINES: Regex =
Regex::new(&format!(r"{}{}({})+", LINE_END, LINE_END, LINE_END))
.unwrap();
Regex::new(&format!(r"{LINE_END}{LINE_END}({LINE_END})+")).unwrap();
pub static ref RE_TRAIL: Regex =
Regex::new(&format!(r" +{}", LINE_END)).unwrap();
Regex::new(&format!(r" +{LINE_END}")).unwrap();
pub static ref RE_LEAVES_BEGIN: Vec<Regex> = LEAVES
.iter()
.map(|l| Regex::new(&format!(r"\\begin\{{{}}}", l)).unwrap())
.map(|l| Regex::new(&format!(r"\\begin\{{{l}}}")).unwrap())
.collect();
pub static ref RE_LEAVES_END: Vec<Regex> = LEAVES
.iter()
.map(|l| Regex::new(&format!(r"\\end\{{{}}}", l)).unwrap())
.map(|l| Regex::new(&format!(r"\\end\{{{l}}}")).unwrap())
.collect();
pub static ref RE_LISTS_BEGIN: Vec<Regex> = LISTS
.iter()
.map(|l| Regex::new(&format!(r"\\begin\{{{}}}", l)).unwrap())
.map(|l| Regex::new(&format!(r"\\begin\{{{l}}}")).unwrap())
.collect();
pub static ref RE_LISTS_END: Vec<Regex> = LISTS
.iter()
.map(|l| Regex::new(&format!(r"\\end\{{{}}}", l)).unwrap())
.map(|l| Regex::new(&format!(r"\\end\{{{l}}}")).unwrap())
.collect();
pub static ref RE_ENV_BEGIN_SHARED_LINE: Regex =
Regex::new(r"(?P<prev>\S.*?)(?P<env>\\begin\{)").unwrap();
Expand Down
8 changes: 4 additions & 4 deletions src/subs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{LINE_END, TAB};
use log::Level::Info;

pub fn remove_extra_newlines(text: &str) -> String {
let double_line_end = format!("{}{}", LINE_END, LINE_END);
let double_line_end = format!("{LINE_END}{LINE_END}");
RE_NEWLINES.replace_all(text, double_line_end).to_string()
}

Expand Down Expand Up @@ -54,13 +54,13 @@ pub fn environments_new_line(
let comment = &get_comment(line, comment_index);
let text = &remove_comment(line, comment_index);
let text = &RE_ENV_BEGIN_SHARED_LINE
.replace_all(text, format!("$prev{}$env", LINE_END))
.replace_all(text, format!("$prev{LINE_END}$env"))
.to_string();
let text = &RE_ENV_END_SHARED_LINE
.replace_all(text, format!("$prev{}$env", LINE_END))
.replace_all(text, format!("$prev{LINE_END}$env"))
.to_string();
let text = &RE_ITEM_SHARED_LINE
.replace_all(text, format!("$prev{}$env", LINE_END))
.replace_all(text, format!("$prev{LINE_END}$env"))
.to_string();
new_text.push_str(text);
new_text.push_str(comment);
Expand Down
34 changes: 14 additions & 20 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn test_file(source_file: &str, target_file: &str) -> bool {

if fmt_source_text != target_text {
println!(
"{}fail {}{} {}-> {}{}{}",
RED, YELLOW, source_file, RESET, YELLOW, target_file, RESET
"{RED}fail {YELLOW}{source_file} {RESET}\
-> {YELLOW}{target_file}{RESET}",
);
let diff = TextDiff::from_lines(&fmt_source_text, &target_text);
for change in diff.iter_all_changes() {
Expand Down Expand Up @@ -58,15 +58,13 @@ fn test_source() {
let mut fail = false;
for file in source_files {
if !test_file(
&format!("tests/source/{}", file),
&format!("tests/target/{}", file),
&format!("tests/source/{file}"),
&format!("tests/target/{file}"),
) {
fail = true
fail = true;
}
}
if fail {
panic!("Some tests failed")
}
assert!(!fail, "Some tests failed");
}

#[test]
Expand All @@ -75,15 +73,13 @@ fn test_target() {
let mut fail = false;
for file in target_files {
if !test_file(
&format!("tests/target/{}", file),
&format!("tests/target/{}", file),
&format!("tests/target/{file}"),
&format!("tests/target/{file}"),
) {
fail = true
fail = true;
}
}
if fail {
panic!("Some tests failed")
}
assert!(!fail, "Some tests failed");
}

#[test]
Expand Down Expand Up @@ -118,13 +114,11 @@ fn test_short() {
let mut fail = false;
for file in files {
if !test_file(
&format!("tests/source/{}", file),
&format!("tests/target/{}", file),
&format!("tests/source/{file}"),
&format!("tests/target/{file}"),
) {
fail = true
fail = true;
}
}
if fail {
panic!("Some tests failed")
}
assert!(!fail, "Some tests failed");
}
2 changes: 1 addition & 1 deletion src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn find_wrap_point(line: &str) -> Option<usize> {
} else if c != '%' {
after_char = true;
}
prev_char = Some(c)
prev_char = Some(c);
}
wrap_point
}
Expand Down

0 comments on commit 09d2fc4

Please sign in to comment.