1 use std::io::{BufWriter, Write};
3 use anyhow::{bail, Result};
6 use rustdoc_json_types::{Crate, Id, FORMAT_VERSION};
10 pub(crate) mod item_kind;
14 #[derive(Debug, PartialEq, Eq, Serialize, Clone)]
20 #[derive(Debug, PartialEq, Eq, Serialize, Clone)]
22 NotFound(Vec<json_find::Selector>),
26 #[derive(Debug, Serialize)]
34 /// The path to the json file to be linted
37 /// Show verbose output
42 json_output: Option<String>,
45 fn main() -> Result<()> {
46 let Cli { path, verbose, json_output } = Cli::parse();
48 let contents = fs::read_to_string(&path)?;
49 let krate: Crate = serde_json::from_str(&contents)?;
50 assert_eq!(krate.format_version, FORMAT_VERSION);
52 let krate_json: Value = serde_json::from_str(&contents)?;
54 let mut validator = validator::Validator::new(&krate, krate_json);
55 validator.check_crate();
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)?;
64 if !validator.errs.is_empty() {
65 for err in validator.errs {
67 ErrorKind::NotFound(sels) => match &sels[..] {
70 "id {:?} must be in crate, or it wouldn't be reported as not found",
75 "{} not in index or paths, but refered to at '{}'",
77 json_find::to_jsonpath(&sel)
83 .map(json_find::to_jsonpath)
84 .map(|i| format!("'{i}'"))
88 "{} not in index or paths, but refered to at {sels}",
93 "{} not in index or paths, but refered to at '{}' and {} more",
95 json_find::to_jsonpath(&sel),
101 ErrorKind::Custom(msg) => eprintln!("{}: {}", err.id.0, msg),
104 bail!("Errors validating json {path}");