-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathmain.rs
130 lines (119 loc) · 4.03 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::env;
use std::error::Error;
use std::fs;
use std::path::Path;
macro_rules! style_error {
($bad:expr, $path:expr, $($arg:tt)*) => {
*$bad = true;
eprint!("error in {}: ", $path.display());
eprintln!("{}", format_args!($($arg)*));
};
}
fn main() {
let arg = env::args().nth(1).unwrap_or_else(|| {
eprintln!("Please pass a src directory as the first argument");
std::process::exit(1);
});
let mut bad = false;
if let Err(e) = check_directory(&Path::new(&arg), &mut bad) {
eprintln!("error: {}", e);
std::process::exit(1);
}
if bad {
eprintln!("some style checks failed");
std::process::exit(1);
}
eprintln!("passed!");
}
fn check_directory(dir: &Path, bad: &mut bool) -> Result<(), Box<dyn Error>> {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
check_directory(&path, bad)?;
continue;
}
if !matches!(
path.extension().and_then(|p| p.to_str()),
Some("md") | Some("html")
) {
// This may be extended in the future if other file types are needed.
style_error!(bad, path, "expected only md or html in src");
}
let contents = fs::read_to_string(&path)?;
if contents.contains("#![feature") {
style_error!(bad, path, "#![feature] attributes are not allowed");
}
if !cfg!(windows) && contents.contains('\r') {
style_error!(
bad,
path,
"CR characters not allowed, must use LF line endings"
);
}
if contents.contains('\t') {
style_error!(bad, path, "tab characters not allowed, use spaces");
}
if !contents.ends_with('\n') {
style_error!(bad, path, "file must end with a newline");
}
if contents.contains('\u{2013}') {
style_error!(bad, path, "en-dash not allowed, use two dashes like --");
}
if contents.contains('\u{2014}') {
style_error!(bad, path, "em-dash not allowed, use three dashes like ---");
}
if contents.contains('\u{a0}') {
style_error!(
bad,
path,
"don't use 0xa0 no-break-space, use instead"
);
}
for line in contents.lines() {
if line.ends_with(' ') {
style_error!(bad, path, "lines must not end with spaces");
}
}
cmark_check(&path, bad, &contents)?;
}
Ok(())
}
fn cmark_check(path: &Path, bad: &mut bool, contents: &str) -> Result<(), Box<dyn Error>> {
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
macro_rules! cmark_error {
($bad:expr, $path:expr, $range:expr, $($arg:tt)*) => {
*$bad = true;
let lineno = contents[..$range.start].chars().filter(|&ch| ch == '\n').count() + 1;
eprint!("error in {} (line {}): ", $path.display(), lineno);
eprintln!("{}", format_args!($($arg)*));
}
}
let options = Options::all();
let parser = Parser::new_ext(contents, options);
for (event, range) in parser.into_offset_iter() {
match event {
Event::Start(Tag::CodeBlock(CodeBlockKind::Indented)) => {
cmark_error!(
bad,
path,
range,
"indented code blocks should use triple backtick-style \
with a language identifier"
);
}
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(languages))) => {
if languages.is_empty() {
cmark_error!(
bad,
path,
range,
"code block should include an explicit language",
);
}
}
_ => {}
}
}
Ok(())
}