1+ use std:: fmt:: Display ;
12use std:: process:: ExitCode ;
23use std:: sync:: LazyLock ;
34use std:: { env, fs} ;
@@ -7,41 +8,10 @@ use regex::{Regex, RegexBuilder};
78mod cache;
89mod config;
910mod directive;
10- mod error;
1111
1212use cache:: Cache ;
13- use config:: parse_config ;
13+ use config:: Config ;
1414use 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
4616static 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