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"
}
This commit is contained in:
Nixon Enraght-Moony 2023-01-02 20:15:45 +00:00
parent 95329080d3
commit 226ab7fd75
4 changed files with 26 additions and 4 deletions

View file

@ -2115,6 +2115,7 @@ dependencies = [
"clap 4.0.15",
"fs-err",
"rustdoc-json-types",
"serde",
"serde_json",
]

View file

@ -10,4 +10,5 @@ anyhow = "1.0.62"
clap = { version = "4.0.15", features = ["derive"] }
fs-err = "2.8.1"
rustdoc-json-types = { version = "0.1.0", path = "../../rustdoc-json-types" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.85"

View file

@ -1,8 +1,9 @@
use std::fmt::Write;
use serde::Serialize;
use serde_json::Value;
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub enum SelectorPart {
Field(String),
Index(usize),

View file

@ -1,25 +1,34 @@
use std::io::{BufWriter, Write};
use anyhow::{bail, Result};
use clap::Parser;
use fs_err as fs;
use rustdoc_json_types::{Crate, Id, FORMAT_VERSION};
use serde::Serialize;
use serde_json::Value;
pub(crate) mod item_kind;
mod json_find;
mod validator;
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Serialize, Clone)]
struct Error {
kind: ErrorKind,
id: Id,
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Serialize, Clone)]
enum ErrorKind {
NotFound(Vec<json_find::Selector>),
Custom(String),
}
#[derive(Debug, Serialize)]
struct JsonOutput {
path: String,
errors: Vec<Error>,
}
#[derive(Parser)]
struct Cli {
/// The path to the json file to be linted
@ -28,10 +37,13 @@ struct Cli {
/// Show verbose output
#[arg(long)]
verbose: bool,
#[arg(long)]
json_output: Option<String>,
}
fn main() -> Result<()> {
let Cli { path, verbose } = Cli::parse();
let Cli { path, verbose, json_output } = Cli::parse();
let contents = fs::read_to_string(&path)?;
let krate: Crate = serde_json::from_str(&contents)?;
@ -42,6 +54,13 @@ fn main() -> Result<()> {
let mut validator = validator::Validator::new(&krate, krate_json);
validator.check_crate();
if let Some(json_output) = json_output {
let output = JsonOutput { path: path.clone(), errors: validator.errs.clone() };
let mut f = BufWriter::new(fs::File::create(json_output)?);
serde_json::to_writer(&mut f, &output)?;
f.flush()?;
}
if !validator.errs.is_empty() {
for err in validator.errs {
match err.kind {