-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8556b7a
Showing
7 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
**/*.rs.bk |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "tex_rules" | ||
version = "0.1.0" | ||
authors = ["Hilmar Gústafsson <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
memmem = "0.1.1" | ||
glob = "0.3.0" | ||
anyhow = "1.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This document writes several sentences on one line. Like. This. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
This. | ||
Is. | ||
Good. | ||
And. | ||
Very. | ||
Diff friendly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use glob::glob; | ||
use memmem::Searcher; | ||
use memmem::TwoWaySearcher; | ||
use std::fs::File; | ||
use std::io::prelude::*; | ||
use std::io::BufReader; | ||
use std::path::PathBuf; | ||
use anyhow::Result; | ||
|
||
pub fn find_tex_files() -> impl Iterator<Item = PathBuf> { | ||
glob("./**/*.tex") | ||
.expect("invalid glob pattern") | ||
.filter(|i| i.is_ok()) | ||
.map(|i| i.unwrap()) | ||
} | ||
|
||
pub fn parse_from_path(path: &PathBuf) -> Result<()> { | ||
let file_name = format!("Error: {}", path.to_str().expect("Invalid UTF-8 in path")); | ||
let mut succ = true; | ||
let file = File::open(path).expect("Invalid path from glob"); | ||
let buf_reader = BufReader::new(file); | ||
for (i, line) in buf_reader.lines().enumerate() { | ||
if let Ok(line) = line { | ||
match parse_line(&line) { | ||
Result::Ok(_) => (), | ||
Result::Err(_) => { | ||
succ = false; | ||
println!("{}:{} {:?}", file_name, i, &line) | ||
} | ||
} | ||
} | ||
} | ||
match succ { | ||
true => Ok(()), | ||
false => Err(anyhow::anyhow!("Errors in {:?}", &path)) | ||
} | ||
} | ||
|
||
pub fn parse_line(line: &str) -> Result<(), ()> { | ||
let search = TwoWaySearcher::new(". ".as_bytes()); | ||
let res = search.search_in(line.as_bytes()); | ||
|
||
match res { | ||
None => Ok(()), | ||
Some(_) => Err(()), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
#[test] | ||
fn test_bad_file() { | ||
let file = include_str!("files/bad.tex"); | ||
let result = parse_line(file); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_good_file() { | ||
let file = include_str!("files/good.tex"); | ||
let result = parse_line(file); | ||
assert!(result.is_ok()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use anyhow::Result; | ||
|
||
mod lib; | ||
fn main() -> Result<()> { | ||
let mut succ = true; | ||
for path in lib::find_tex_files() { | ||
if lib::parse_from_path(&path).is_err() { | ||
succ = false; | ||
} | ||
} | ||
match succ { | ||
true => Ok(()), | ||
false => Err(anyhow::anyhow!("Errors were detected.")), | ||
} | ||
} |