Skip to content

Commit 65e4edc

Browse files
committed
unify the error message style, skip directives collection
- directives were collected before processing, but there seems to be nothing preventing them from checking right away - the unified error message style follows the style of short diagnostics from the compiler (almost the same was used on the deleted line 39)
1 parent 955b268 commit 65e4edc

File tree

3 files changed

+57
-79
lines changed

3 files changed

+57
-79
lines changed

src/tools/jsondocck/src/config.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,34 @@ pub struct Config {
1111
pub template: String,
1212
}
1313

14-
/// Create [`Config`] from an iterator of command-line arguments.
15-
pub fn parse_config(mut args: Args) -> Option<Config> {
16-
let mut opts = Options::new();
17-
opts.reqopt("", "doc-dir", "Path to the documentation output directory.", "PATH")
18-
.reqopt("", "template", "Path to the input template file.", "PATH")
19-
.optflag("h", "help", "Show this message.");
14+
impl Config {
15+
/// Create [`Config`] from an iterator of command-line arguments.
16+
pub fn parse(mut args: Args) -> Option<Config> {
17+
let mut opts = Options::new();
18+
opts.reqopt("", "doc-dir", "Path to the documentation output directory.", "PATH")
19+
.reqopt("", "template", "Path to the input template file.", "PATH")
20+
.optflag("h", "help", "Show this message.");
2021

21-
let argv0 = args.next().unwrap();
22-
let usage = &*LazyCell::new(|| opts.usage(&format!("Usage: {argv0} <doc-dir> <template>")));
22+
let argv0 = args.next().unwrap();
23+
let usage = &*LazyCell::new(|| opts.usage(&format!("Usage: {argv0} <doc-dir> <template>")));
2324

24-
if args.len() == 0 {
25-
print!("{usage}");
25+
if args.len() == 0 {
26+
print!("{usage}");
2627

27-
return None;
28-
}
28+
return None;
29+
}
2930

30-
let matches = opts.parse(args).unwrap();
31+
let matches = opts.parse(args).unwrap();
3132

32-
if matches.opt_present("h") || matches.opt_present("help") {
33-
print!("{usage}");
33+
if matches.opt_present("h") || matches.opt_present("help") {
34+
print!("{usage}");
3435

35-
return None;
36-
}
36+
return None;
37+
}
3738

38-
Some(Config {
39-
doc_dir: matches.opt_str("doc-dir").unwrap(),
40-
template: matches.opt_str("template").unwrap(),
41-
})
39+
Some(Config {
40+
doc_dir: matches.opt_str("doc-dir").unwrap(),
41+
template: matches.opt_str("template").unwrap(),
42+
})
43+
}
4244
}

src/tools/jsondocck/src/error.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/tools/jsondocck/src/main.rs

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt::Display;
12
use std::process::ExitCode;
23
use std::sync::LazyLock;
34
use std::{env, fs};
@@ -7,41 +8,10 @@ use regex::{Regex, RegexBuilder};
78
mod cache;
89
mod config;
910
mod directive;
10-
mod error;
1111

1212
use cache::Cache;
13-
use config::parse_config;
13+
use config::Config;
1414
use directive::{Directive, DirectiveKind};
15-
use error::CkError;
16-
17-
fn main() -> ExitCode {
18-
let Some(config) = parse_config(env::args()) else {
19-
return ExitCode::FAILURE;
20-
};
21-
22-
let mut failed = Vec::new();
23-
let mut cache = Cache::new(&config);
24-
let Ok(directives) = get_directives(&config.template) else {
25-
eprintln!("Jsondocck failed for {}", &config.template);
26-
return ExitCode::FAILURE;
27-
};
28-
29-
for directive in directives {
30-
if let Err(message) = directive.check(&mut cache) {
31-
failed.push(CkError { directive, message });
32-
}
33-
}
34-
35-
if failed.is_empty() {
36-
ExitCode::SUCCESS
37-
} else {
38-
for i in failed {
39-
eprintln!("{}:{}, directive failed", config.template, i.directive.lineno);
40-
eprintln!("{}", i.message)
41-
}
42-
ExitCode::FAILURE
43-
}
44-
}
4515

4616
static LINE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
4717
RegexBuilder::new(
@@ -58,30 +28,38 @@ static LINE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
5828
.unwrap()
5929
});
6030

61-
static DEPRECATED_LINE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
62-
RegexBuilder::new(
63-
r#"//\s+@"#,
64-
)
65-
.build()
66-
.unwrap()
67-
});
31+
static DEPRECATED_LINE_PATTERN: LazyLock<Regex> =
32+
LazyLock::new(|| RegexBuilder::new(r#"//\s+@"#).build().unwrap());
6833

69-
fn print_err(msg: &str, lineno: usize) {
70-
eprintln!("Invalid directive: {} on line {}", msg, lineno)
34+
struct ErrorReporter<'a> {
35+
/// See [`Config::template`].
36+
template: &'a str,
37+
errors: bool,
38+
}
39+
40+
impl ErrorReporter<'_> {
41+
fn print(&mut self, msg: impl Display, lineno: usize) {
42+
self.errors = true;
43+
44+
eprintln!("{}:{lineno}: {msg}", self.template);
45+
}
7146
}
7247

73-
/// Get a list of directives from a file.
74-
fn get_directives(template: &str) -> Result<Vec<Directive>, ()> {
75-
let mut directives = Vec::new();
76-
let mut errors = false;
48+
fn main() -> ExitCode {
49+
let Some(config @ Config { template, .. }) = &Config::parse(env::args()) else {
50+
return ExitCode::FAILURE;
51+
};
52+
53+
let mut cache = Cache::new(config);
54+
let mut error_reporter = ErrorReporter { errors: false, template };
7755
let file = fs::read_to_string(template).unwrap();
7856

7957
for (mut lineno, line) in file.split('\n').enumerate() {
8058
lineno += 1;
8159

8260
if DEPRECATED_LINE_PATTERN.is_match(line) {
83-
print_err("Deprecated directive syntax, replace `// @` with `//@ `", lineno);
84-
errors = true;
61+
error_reporter.print("Deprecated directive syntax, replace `// @` with `//@ `", lineno);
62+
8563
continue;
8664
}
8765

@@ -93,15 +71,20 @@ fn get_directives(template: &str) -> Result<Vec<Directive>, ()> {
9371

9472
let args_str = &cap["args"];
9573
let Some(args) = shlex::split(args_str) else {
96-
print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno);
97-
errors = true;
74+
error_reporter
75+
.print(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno);
76+
9877
continue;
9978
};
10079

10180
if let Some((kind, path)) = DirectiveKind::parse(&cap["directive"], negated, &args) {
102-
directives.push(Directive { kind, lineno, path: path.to_owned() })
81+
let directive = Directive { kind, lineno, path: path.to_owned() };
82+
83+
if let Err(message) = directive.check(&mut cache) {
84+
error_reporter.print(format_args!("directive failed: {message}"), directive.lineno);
85+
}
10386
}
10487
}
10588

106-
if !errors { Ok(directives) } else { Err(()) }
89+
if error_reporter.errors { ExitCode::FAILURE } else { ExitCode::SUCCESS }
10790
}

0 commit comments

Comments
 (0)