1
+ use std:: io:: { BufWriter , Write } ;
2
+
1
3
use anyhow:: { bail, Result } ;
2
4
use clap:: Parser ;
3
5
use fs_err as fs;
4
6
use rustdoc_json_types:: { Crate , Id , FORMAT_VERSION } ;
7
+ use serde:: Serialize ;
5
8
use serde_json:: Value ;
6
9
7
10
pub ( crate ) mod item_kind;
8
11
mod json_find;
9
12
mod validator;
10
13
11
- #[ derive( Debug , PartialEq , Eq ) ]
14
+ #[ derive( Debug , PartialEq , Eq , Serialize , Clone ) ]
12
15
struct Error {
13
16
kind : ErrorKind ,
14
17
id : Id ,
15
18
}
16
19
17
- #[ derive( Debug , PartialEq , Eq ) ]
20
+ #[ derive( Debug , PartialEq , Eq , Serialize , Clone ) ]
18
21
enum ErrorKind {
19
22
NotFound ( Vec < json_find:: Selector > ) ,
20
23
Custom ( String ) ,
21
24
}
22
25
26
+ #[ derive( Debug , Serialize ) ]
27
+ struct JsonOutput {
28
+ path : String ,
29
+ errors : Vec < Error > ,
30
+ }
31
+
23
32
#[ derive( Parser ) ]
24
33
struct Cli {
25
34
/// The path to the json file to be linted
@@ -28,10 +37,13 @@ struct Cli {
28
37
/// Show verbose output
29
38
#[ arg( long) ]
30
39
verbose : bool ,
40
+
41
+ #[ arg( long) ]
42
+ json_output : Option < String > ,
31
43
}
32
44
33
45
fn main ( ) -> Result < ( ) > {
34
- let Cli { path, verbose } = Cli :: parse ( ) ;
46
+ let Cli { path, verbose, json_output } = Cli :: parse ( ) ;
35
47
36
48
let contents = fs:: read_to_string ( & path) ?;
37
49
let krate: Crate = serde_json:: from_str ( & contents) ?;
@@ -42,6 +54,13 @@ fn main() -> Result<()> {
42
54
let mut validator = validator:: Validator :: new ( & krate, krate_json) ;
43
55
validator. check_crate ( ) ;
44
56
57
+ if let Some ( json_output) = json_output {
58
+ let output = JsonOutput { path : path. clone ( ) , errors : validator. errs . clone ( ) } ;
59
+ let mut f = BufWriter :: new ( fs:: File :: create ( json_output) ?) ;
60
+ serde_json:: to_writer ( & mut f, & output) ?;
61
+ f. flush ( ) ?;
62
+ }
63
+
45
64
if !validator. errs . is_empty ( ) {
46
65
for err in validator. errs {
47
66
match err. kind {
0 commit comments