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", "clap 4.0.15",
"fs-err", "fs-err",
"rustdoc-json-types", "rustdoc-json-types",
"serde",
"serde_json", "serde_json",
] ]

View File

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

View File

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

View File

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