Skip to content

Commit 226ab7f

Browse files
committed
jsondoclint: Add option to dump errors as json.
The output looks like: { "errors": [ { "id": "2:2017:1833", "kind": { "NotFound": [ [ {"Field": "index"}, {"Field": "0:0:1571"}, {"Field": "links"}, {"Field": "pointer::read"} ] ] } } ], "path": "/home/nixon/dev/rust/rust/build/x86_64-unknown-linux-gnu/test/rustdoc-json/intra-doc-links/pointer_method/pointer_method.json" }
1 parent 9532908 commit 226ab7f

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -2115,6 +2115,7 @@ dependencies = [
21152115
"clap 4.0.15",
21162116
"fs-err",
21172117
"rustdoc-json-types",
2118+
"serde",
21182119
"serde_json",
21192120
]
21202121

src/tools/jsondoclint/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ anyhow = "1.0.62"
1010
clap = { version = "4.0.15", features = ["derive"] }
1111
fs-err = "2.8.1"
1212
rustdoc-json-types = { version = "0.1.0", path = "../../rustdoc-json-types" }
13+
serde = { version = "1.0", features = ["derive"] }
1314
serde_json = "1.0.85"

src/tools/jsondoclint/src/json_find.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::fmt::Write;
22

3+
use serde::Serialize;
34
use serde_json::Value;
45

5-
#[derive(Debug, Clone, PartialEq, Eq)]
6+
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
67
pub enum SelectorPart {
78
Field(String),
89
Index(usize),

src/tools/jsondoclint/src/main.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1+
use std::io::{BufWriter, Write};
2+
13
use anyhow::{bail, Result};
24
use clap::Parser;
35
use fs_err as fs;
46
use rustdoc_json_types::{Crate, Id, FORMAT_VERSION};
7+
use serde::Serialize;
58
use serde_json::Value;
69

710
pub(crate) mod item_kind;
811
mod json_find;
912
mod validator;
1013

11-
#[derive(Debug, PartialEq, Eq)]
14+
#[derive(Debug, PartialEq, Eq, Serialize, Clone)]
1215
struct Error {
1316
kind: ErrorKind,
1417
id: Id,
1518
}
1619

17-
#[derive(Debug, PartialEq, Eq)]
20+
#[derive(Debug, PartialEq, Eq, Serialize, Clone)]
1821
enum ErrorKind {
1922
NotFound(Vec<json_find::Selector>),
2023
Custom(String),
2124
}
2225

26+
#[derive(Debug, Serialize)]
27+
struct JsonOutput {
28+
path: String,
29+
errors: Vec<Error>,
30+
}
31+
2332
#[derive(Parser)]
2433
struct Cli {
2534
/// The path to the json file to be linted
@@ -28,10 +37,13 @@ struct Cli {
2837
/// Show verbose output
2938
#[arg(long)]
3039
verbose: bool,
40+
41+
#[arg(long)]
42+
json_output: Option<String>,
3143
}
3244

3345
fn main() -> Result<()> {
34-
let Cli { path, verbose } = Cli::parse();
46+
let Cli { path, verbose, json_output } = Cli::parse();
3547

3648
let contents = fs::read_to_string(&path)?;
3749
let krate: Crate = serde_json::from_str(&contents)?;
@@ -42,6 +54,13 @@ fn main() -> Result<()> {
4254
let mut validator = validator::Validator::new(&krate, krate_json);
4355
validator.check_crate();
4456

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+
4564
if !validator.errs.is_empty() {
4665
for err in validator.errs {
4766
match err.kind {

0 commit comments

Comments
 (0)