Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
LiHRaM committed Dec 5, 2019
0 parents commit 8556b7a
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
**/*.rs.bk
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Cargo.toml
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"
1 change: 1 addition & 0 deletions src/files/bad.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This document writes several sentences on one line. Like. This.
6 changes: 6 additions & 0 deletions src/files/good.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This.
Is.
Good.
And.
Very.
Diff friendly.
65 changes: 65 additions & 0 deletions src/lib.rs
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());
}
}
15 changes: 15 additions & 0 deletions src/main.rs
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.")),
}
}

0 comments on commit 8556b7a

Please sign in to comment.