forked from rust-rosetta/rust-rosetta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
51 lines (42 loc) · 1.52 KB
/
build.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
// This build script checks that all files in the `src` directory have lines of at most 100
// characters and don't contain trailing whitespace.
//
// In case we find a line that doesn't comply with this rules, the build will fail and indicate
// the cause of the problem.
use std::fs::{self, File, metadata};
use std::io::Read;
use std::path::Path;
fn main() {
let files = fs::read_dir("src").unwrap()
.map(|e| e.unwrap());
for f in files {
let path = f.path();
if metadata(&path).unwrap().is_file() {
check(&path);
}
}
}
fn check(path: &Path) {
let mut content = String::new();
File::open(&path).unwrap().read_to_string(&mut content).unwrap();
for (i, mut line) in content.lines().enumerate() {
// Ignore '\r'
if let Some('\r') = line.chars().rev().next() {
line = &line[..line.len() - 1];
}
// Check length
if line.len() > 100 {
line_error(i + 1, path, "line is longer than 100 characters");
}
// Check trailing whitespace
if let Some(last_char) = line.chars().rev().next() {
if last_char.is_whitespace() {
line_error(i + 1, path, "line has trailing whitespace");
}
}
}
}
fn line_error(line: usize, path: &Path, msg: &str) {
panic!("Formatting error, {} (line {} of file \"{}\")",
msg, line, path.to_str().unwrap())
}