@@ -10,22 +10,24 @@ use rustdoc_json_types::{
10
10
11
11
use crate :: { item_kind:: Kind , Error , ErrorKind } ;
12
12
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.
13
21
#[ derive( Debug ) ]
14
22
pub struct Validator < ' a > {
15
23
pub ( crate ) errs : Vec < Error > ,
16
24
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.
17
28
seen_ids : HashSet < & ' a Id > ,
29
+ /// Ids that have already been reported missing.
18
30
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
- }
29
31
}
30
32
31
33
impl < ' a > Validator < ' a > {
@@ -82,6 +84,8 @@ impl<'a> Validator<'a> {
82
84
}
83
85
}
84
86
}
87
+ } else {
88
+ assert ! ( self . krate. paths. contains_key( id) ) ;
85
89
}
86
90
}
87
91
@@ -336,17 +340,12 @@ impl<'a> Validator<'a> {
336
340
fp. generic_params . iter ( ) . for_each ( |gpd| self . check_generic_param_def ( gpd) ) ;
337
341
}
338
342
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
-
346
343
fn add_id_checked ( & mut self , id : & ' a Id , valid : fn ( Kind ) -> bool , expected : & str ) {
347
344
if let Some ( kind) = self . kind_of ( id) {
348
345
if valid ( kind) {
349
- self . add_id ( id) ;
346
+ if !self . seen_ids . contains ( id) {
347
+ self . todo . insert ( id) ;
348
+ }
350
349
} else {
351
350
self . fail_expecting ( id, expected) ;
352
351
}
@@ -402,3 +401,12 @@ impl<'a> Validator<'a> {
402
401
}
403
402
}
404
403
}
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