1
+ use std:: fmt:: Display ;
1
2
use std:: process:: ExitCode ;
2
3
use std:: sync:: LazyLock ;
3
4
use std:: { env, fs} ;
@@ -7,41 +8,10 @@ use regex::{Regex, RegexBuilder};
7
8
mod cache;
8
9
mod config;
9
10
mod directive;
10
- mod error;
11
11
12
12
use cache:: Cache ;
13
- use config:: parse_config ;
13
+ use config:: Config ;
14
14
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
- }
45
15
46
16
static LINE_PATTERN : LazyLock < Regex > = LazyLock :: new ( || {
47
17
RegexBuilder :: new (
@@ -58,30 +28,38 @@ static LINE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
58
28
. unwrap ( )
59
29
} ) ;
60
30
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 ( ) ) ;
68
33
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
+ }
71
46
}
72
47
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 } ;
77
55
let file = fs:: read_to_string ( template) . unwrap ( ) ;
78
56
79
57
for ( mut lineno, line) in file. split ( '\n' ) . enumerate ( ) {
80
58
lineno += 1 ;
81
59
82
60
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
+
85
63
continue ;
86
64
}
87
65
@@ -93,15 +71,20 @@ fn get_directives(template: &str) -> Result<Vec<Directive>, ()> {
93
71
94
72
let args_str = & cap[ "args" ] ;
95
73
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
+
98
77
continue ;
99
78
} ;
100
79
101
80
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
+ }
103
86
}
104
87
}
105
88
106
- if ! errors { Ok ( directives ) } else { Err ( ( ) ) }
89
+ if error_reporter . errors { ExitCode :: FAILURE } else { ExitCode :: SUCCESS }
107
90
}
0 commit comments