]>
git.proxmox.com Git - rustc.git/blob - src/doc/book/2018-edition/tools/src/bin/lfp.rs
1 // We have some long regex literals, so:
2 // ignore-tidy-linelength
5 extern crate rustc_serialize
;
9 use std
::{path, fs, io}
;
10 use std
::io
::{BufRead, Write}
;
12 macro_rules
! println_stderr(
14 match writeln
!(&mut ::std
::io
::stderr(), $
($arg
)* ) {
16 Err(x
) => panic
!("Unable to write to stderr: {}", x
),
22 let args
: Args
= Docopt
::new(USAGE
)
23 .and_then(|d
| d
.decode())
24 .unwrap_or_else(|e
| e
.exit());
26 let src_dir
= &path
::Path
::new(&args
.arg_src_dir
);
27 let found_errs
= walkdir
::WalkDir
::new(src_dir
)
34 println_stderr
!("{:?}", err
);
35 std
::process
::exit(911)
40 let path
= entry
.path();
41 if is_file_of_interest(path
) {
42 let err_vec
= lint_file(path
);
45 LintingError
::LineOfInterest(line_num
, ref line
) =>
46 println_stderr
!("{}:{}\t{}", path
.display(), line_num
, line
),
47 LintingError
::UnableToOpenFile
=>
48 println_stderr
!("Unable to open {}.", path
.display()),
58 .any(|result
| *result
);
67 const USAGE
: &'
static str = "
73 -h --help Show this screen.
76 #[derive(Debug, RustcDecodable)]
81 fn lint_file(path
: &path
::Path
) -> Vec
<LintingError
> {
82 match fs
::File
::open(path
) {
83 Ok(file
) => lint_lines(io
::BufReader
::new(&file
).lines()),
84 Err(_
) => vec
![LintingError
::UnableToOpenFile
],
88 fn lint_lines
<I
>(lines
: I
) -> Vec
<LintingError
>
89 where I
: Iterator
<Item
=io
::Result
<String
>> {
92 .map(|(line_num
, line
)| {
93 let raw_line
= line
.unwrap();
94 if is_line_of_interest(&raw_line
) {
95 Err(LintingError
::LineOfInterest(line_num
, raw_line
))
100 .filter(|result
| result
.is_err())
101 .map(|result
| result
.unwrap_err())
105 fn is_file_of_interest(path
: &path
::Path
) -> bool
{
107 .map_or(false, |ext
| ext
== "md")
110 fn is_line_of_interest(line
: &str) -> bool
{
111 !line
.split_whitespace()
113 sub_string
.contains("file://") &&
114 !sub_string
.contains("file:///projects/")
123 LineOfInterest(usize, String
)
132 fn lint_file_returns_a_vec_with_errs_when_lines_of_interest_are_found() {
135 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
136 Running `target/guessing_game`
138 The secret number is: 61
139 Please input your guess.
143 Please input your guess.
147 Please input your guess.
149 Please input your guess.
154 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
155 Running `target/debug/guessing_game`
157 The secret number is: 7
158 Please input your guess.
162 Running `target/debug/guessing_game`
164 The secret number is: 83
165 Please input your guess.
168 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
169 Running `target/debug/guessing_game`
173 let raw_lines
= string
.to_string();
174 let lines
= raw_lines
.lines().map(|line
| {
178 let result_vec
= super::lint_lines(lines
);
180 assert
!(!result_vec
.is_empty());
181 assert_eq
!(3, result_vec
.len());
185 fn lint_file_returns_an_empty_vec_when_no_lines_of_interest_are_found() {
188 Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
189 Running `target/guessing_game`
191 The secret number is: 61
192 Please input your guess.
196 Please input your guess.
200 Please input your guess.
202 Please input your guess.
208 let raw_lines
= string
.to_string();
209 let lines
= raw_lines
.lines().map(|line
| {
213 let result_vec
= super::lint_lines(lines
);
215 assert
!(result_vec
.is_empty());
219 fn is_file_of_interest_returns_false_when_the_path_is_a_directory() {
220 let uninteresting_fn
= "src/img";
222 assert
!(!super::is_file_of_interest(path
::Path
::new(uninteresting_fn
)));
226 fn is_file_of_interest_returns_false_when_the_filename_does_not_have_the_md_extension() {
227 let uninteresting_fn
= "src/img/foo1.png";
229 assert
!(!super::is_file_of_interest(path
::Path
::new(uninteresting_fn
)));
233 fn is_file_of_interest_returns_true_when_the_filename_has_the_md_extension() {
234 let interesting_fn
= "src/ch01-00-introduction.md";
236 assert
!(super::is_file_of_interest(path
::Path
::new(interesting_fn
)));
240 fn is_line_of_interest_does_not_report_a_line_if_the_line_contains_a_file_url_which_is_directly_followed_by_the_project_path() {
241 let sample_line
= "Compiling guessing_game v0.1.0 (file:///projects/guessing_game)";
243 assert
!(!super::is_line_of_interest(sample_line
));
247 fn is_line_of_interest_reports_a_line_if_the_line_contains_a_file_url_which_is_not_directly_followed_by_the_project_path() {
248 let sample_line
= "Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)";
250 assert
!(super::is_line_of_interest(sample_line
));