@@ -27,10 +27,7 @@ fn main() -> Result<(), Box<dyn Error>> {
2727
2828 // Collect up all items we want to document. Returns an error if any
2929 // items we expect to be documented are missing associated block comments.
30- let mut docs = find_doc_items ( root, header_file_bytes) ?;
31-
32- // Cross-link items in comments.
33- docs. crosslink_comments ( ) ?;
30+ let docs = find_doc_items ( root, header_file_bytes) ?;
3431
3532 // Render JSON data.
3633 println ! ( "{}" , serde_json:: to_string_pretty( & docs) ?) ;
@@ -48,69 +45,6 @@ struct ApiDocs {
4845 aliases : Vec < TypeAliasItem > ,
4946}
5047
51- impl ApiDocs {
52- fn crosslink_comments ( & mut self ) -> Result < ( ) , Box < dyn Error > > {
53- // Put all anchors into a set. Error for any duplicates.
54- let mut anchor_set = HashSet :: new ( ) ;
55- for a in self . all_anchors ( ) {
56- if !anchor_set. insert ( a. to_string ( ) ) {
57- return Err ( format ! ( "duplicate anchor: {a}" ) . into ( ) ) ;
58- }
59- }
60-
61- // For each item of each type, crosslink its comment.
62- for s in & mut self . structs {
63- if let Some ( comment) = & mut s. metadata . comment {
64- comment. crosslink ( & anchor_set) ?;
65- }
66- }
67- for f in & mut self . functions {
68- if let Some ( comment) = & mut f. metadata . comment {
69- comment. crosslink ( & anchor_set) ?;
70- }
71- }
72- for cb in & mut self . callbacks {
73- if let Some ( comment) = & mut cb. metadata . comment {
74- comment. crosslink ( & anchor_set) ?;
75- }
76- }
77- for e in & mut self . enums {
78- if let Some ( comment) = & mut e. metadata . comment {
79- comment. crosslink ( & anchor_set) ?;
80- }
81- for v in & mut e. variants {
82- if let Some ( comment) = & mut v. comment {
83- comment. crosslink ( & anchor_set) ?;
84- }
85- }
86- }
87- for e in & mut self . externs {
88- if let Some ( comment) = & mut e. metadata . comment {
89- comment. crosslink ( & anchor_set) ?;
90- }
91- }
92- for a in & mut self . aliases {
93- if let Some ( comment) = & mut a. metadata . comment {
94- comment. crosslink ( & anchor_set) ?;
95- }
96- }
97-
98- Ok ( ( ) )
99- }
100-
101- fn all_anchors ( & self ) -> impl Iterator < Item = & str > {
102- // return all item anchors as a chained iterator
103- self . structs
104- . iter ( )
105- . map ( |s| s. anchor . as_str ( ) )
106- . chain ( self . functions . iter ( ) . map ( |f| f. anchor . as_str ( ) ) )
107- . chain ( self . callbacks . iter ( ) . map ( |cb| cb. anchor . as_str ( ) ) )
108- . chain ( self . enums . iter ( ) . map ( |e| e. anchor . as_str ( ) ) )
109- . chain ( self . externs . iter ( ) . map ( |e| e. anchor . as_str ( ) ) )
110- . chain ( self . aliases . iter ( ) . map ( |a| a. anchor . as_str ( ) ) )
111- }
112- }
113-
11448fn find_doc_items ( root : Node , source_code : & [ u8 ] ) -> Result < ApiDocs , Box < dyn Error > > {
11549 // We document all:
11650 // * type definitions
@@ -159,8 +93,17 @@ fn find_doc_items(root: Node, source_code: &[u8]) -> Result<ApiDocs, Box<dyn Err
15993 return Err ( format ! ( "{errors} errors produced while documenting header file" ) . into ( ) ) ;
16094 }
16195
96+ // Put all anchors into a set. Error for any duplicates.
97+ let mut anchor_set = HashSet :: new ( ) ;
98+ for item in & items {
99+ if !anchor_set. insert ( item. anchor ( ) . to_string ( ) ) {
100+ return Err ( format ! ( "duplicate anchor: {}" , item. anchor( ) ) . into ( ) ) ;
101+ }
102+ }
103+
162104 let mut api = ApiDocs :: default ( ) ;
163- for item in items {
105+ for mut item in items {
106+ item. metadata_mut ( ) . crosslink ( & anchor_set) ?;
164107 match item {
165108 Item :: Enum ( e) => api. enums . push ( e) ,
166109 Item :: Struct ( s) => api. structs . push ( s) ,
@@ -250,6 +193,14 @@ impl ItemMetadata {
250193 feature : Feature :: new ( prev, src) . ok ( ) ,
251194 } )
252195 }
196+
197+ // If the metadata has a comment, update the content with crosslinks using the provided anchors
198+ fn crosslink ( & mut self , anchors : & HashSet < String > ) -> Result < ( ) , Box < dyn Error > > {
199+ match & mut self . comment {
200+ Some ( comment) => comment. crosslink ( anchors) ,
201+ None => Ok ( ( ) ) ,
202+ }
203+ }
253204}
254205
255206#[ derive( Debug , Default , Serialize ) ]
@@ -465,6 +416,30 @@ enum Item {
465416 Extern ( ExternItem ) ,
466417}
467418
419+ impl Item {
420+ fn anchor ( & self ) -> & str {
421+ match self {
422+ Item :: Enum ( item) => & item. anchor ,
423+ Item :: Struct ( item) => & item. anchor ,
424+ Item :: TypeAlias ( item) => & item. anchor ,
425+ Item :: Callback ( item) => & item. anchor ,
426+ Item :: Function ( item) => & item. anchor ,
427+ Item :: Extern ( item) => & item. anchor ,
428+ }
429+ }
430+
431+ fn metadata_mut ( & mut self ) -> & mut ItemMetadata {
432+ match self {
433+ Item :: Enum ( item) => & mut item. metadata ,
434+ Item :: Struct ( item) => & mut item. metadata ,
435+ Item :: TypeAlias ( item) => & mut item. metadata ,
436+ Item :: Callback ( item) => & mut item. metadata ,
437+ Item :: Function ( item) => & mut item. metadata ,
438+ Item :: Extern ( item) => & mut item. metadata ,
439+ }
440+ }
441+ }
442+
468443impl From < EnumItem > for Item {
469444 fn from ( item : EnumItem ) -> Self {
470445 Self :: Enum ( item)
0 commit comments