Skip to content

Commit 5956b56

Browse files
committed
jsondoclint: Document validator
1 parent c98c7cb commit 5956b56

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/tools/jsondoclint/src/item_kind.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use rustdoc_json_types::{Item, ItemEnum, ItemKind, ItemSummary};
22

3-
// We want a univeral way to represent an `ItemEnum` or `ItemKind`
4-
3+
/// A univeral way to represent an [`ItemEnum`] or [`ItemKind`]
54
#[derive(Debug)]
65
pub(crate) enum Kind {
76
Module,

src/tools/jsondoclint/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ fn main() -> Result<()> {
2525
let path = env::args().nth(1).ok_or_else(|| anyhow!("no path given"))?;
2626
let contents = fs::read_to_string(&path)?;
2727
let krate: Crate = serde_json::from_str(&contents)?;
28-
// TODO: Only load if nessessary.
29-
let krate_json: Value = serde_json::from_str(&contents)?;
3028
assert_eq!(krate.format_version, FORMAT_VERSION);
3129

3230
let mut validator = validator::Validator::new(&krate);
@@ -36,6 +34,8 @@ fn main() -> Result<()> {
3634
for err in validator.errs {
3735
match err.kind {
3836
ErrorKind::NotFound => {
37+
let krate_json: Value = serde_json::from_str(&contents)?;
38+
3939
let sels =
4040
json_find::find_selector(&krate_json, &Value::String(err.id.0.clone()));
4141
match &sels[..] {

src/tools/jsondoclint/src/validator.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@ use rustdoc_json_types::{
1010

1111
use crate::{item_kind::Kind, Error, ErrorKind};
1212

13+
/// The Validator walks over the JSON tree, and ensures it is well formed.
14+
/// It is made of several parts.
15+
///
16+
/// - `check_*`: These take a type from [`rustdoc_json_types`], and check that
17+
/// it is well formed. This involves calling `check_*` functions on
18+
/// fields of that item, and `add_*` functions on [`Id`]s.
19+
/// - `add_*`: These add an [`Id`] to the worklist, after validating it to check if
20+
/// the `Id` is a kind expected in this suituation.
1321
#[derive(Debug)]
1422
pub struct Validator<'a> {
1523
pub(crate) errs: Vec<Error>,
1624
krate: &'a Crate,
25+
/// Worklist of Ids to check.
26+
todo: HashSet<&'a Id>,
27+
/// Ids that have already been visited, so don't need to be checked again.
1728
seen_ids: HashSet<&'a Id>,
29+
/// Ids that have already been reported missing.
1830
missing_ids: HashSet<&'a Id>,
19-
todo: HashSet<&'a Id>,
20-
}
21-
22-
fn set_remove<T: Hash + Eq + Clone>(set: &mut HashSet<T>) -> Option<T> {
23-
if let Some(id) = set.iter().next() {
24-
let id = id.clone();
25-
set.take(&id)
26-
} else {
27-
None
28-
}
2931
}
3032

3133
impl<'a> Validator<'a> {
@@ -82,6 +84,8 @@ impl<'a> Validator<'a> {
8284
}
8385
}
8486
}
87+
} else {
88+
assert!(self.krate.paths.contains_key(id));
8589
}
8690
}
8791

@@ -336,17 +340,12 @@ impl<'a> Validator<'a> {
336340
fp.generic_params.iter().for_each(|gpd| self.check_generic_param_def(gpd));
337341
}
338342

339-
// Aux functions
340-
fn add_id(&mut self, id: &'a Id) {
341-
if !self.seen_ids.contains(id) {
342-
self.todo.insert(id);
343-
}
344-
}
345-
346343
fn add_id_checked(&mut self, id: &'a Id, valid: fn(Kind) -> bool, expected: &str) {
347344
if let Some(kind) = self.kind_of(id) {
348345
if valid(kind) {
349-
self.add_id(id);
346+
if !self.seen_ids.contains(id) {
347+
self.todo.insert(id);
348+
}
350349
} else {
351350
self.fail_expecting(id, expected);
352351
}
@@ -402,3 +401,12 @@ impl<'a> Validator<'a> {
402401
}
403402
}
404403
}
404+
405+
fn set_remove<T: Hash + Eq + Clone>(set: &mut HashSet<T>) -> Option<T> {
406+
if let Some(id) = set.iter().next() {
407+
let id = id.clone();
408+
set.take(&id)
409+
} else {
410+
None
411+
}
412+
}

0 commit comments

Comments
 (0)