Skip to content

Commit 0002f04

Browse files
committed
docgen: tidy up comment crosslinking
1 parent 31a57b0 commit 0002f04

File tree

1 file changed

+43
-68
lines changed

1 file changed

+43
-68
lines changed

tools/src/bin/docgen/main.rs

Lines changed: 43 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
11448
fn find_doc_items(root: Node, source_code: &[u8]) -> Result<ApiDocs, Box<dyn Error>> {
11549
// We document all:
11650
// * type definitions
@@ -154,8 +88,17 @@ fn find_doc_items(root: Node, source_code: &[u8]) -> Result<ApiDocs, Box<dyn Err
15488
return Err(format!("{errors} errors produced while documenting header file").into());
15589
}
15690

91+
// Put all anchors into a set. Error for any duplicates.
92+
let mut anchor_set = HashSet::new();
93+
for item in &items {
94+
if !anchor_set.insert(item.anchor().to_string()) {
95+
return Err(format!("duplicate anchor: {}", item.anchor()).into());
96+
}
97+
}
98+
15799
let mut api = ApiDocs::default();
158-
for item in items {
100+
for mut item in items {
101+
item.metadata_mut().crosslink(&anchor_set)?;
159102
match item {
160103
Item::Enum(e) => api.enums.push(e),
161104
Item::Struct(s) => api.structs.push(s),
@@ -245,6 +188,14 @@ impl ItemMetadata {
245188
feature: Feature::new(prev, src).ok(),
246189
})
247190
}
191+
192+
// If the metadata has a comment, update the content with crosslinks using the provided anchors
193+
fn crosslink(&mut self, anchors: &HashSet<String>) -> Result<(), Box<dyn Error>> {
194+
match &mut self.comment {
195+
Some(comment) => comment.crosslink(anchors),
196+
None => Ok(()),
197+
}
198+
}
248199
}
249200

250201
#[derive(Debug, Default, Serialize)]
@@ -460,6 +411,30 @@ enum Item {
460411
Extern(ExternItem),
461412
}
462413

414+
impl Item {
415+
fn anchor(&self) -> &str {
416+
match self {
417+
Item::Enum(item) => &item.anchor,
418+
Item::Struct(item) => &item.anchor,
419+
Item::TypeAlias(item) => &item.anchor,
420+
Item::Callback(item) => &item.anchor,
421+
Item::Function(item) => &item.anchor,
422+
Item::Extern(item) => &item.anchor,
423+
}
424+
}
425+
426+
fn metadata_mut(&mut self) -> &mut ItemMetadata {
427+
match self {
428+
Item::Enum(item) => &mut item.metadata,
429+
Item::Struct(item) => &mut item.metadata,
430+
Item::TypeAlias(item) => &mut item.metadata,
431+
Item::Callback(item) => &mut item.metadata,
432+
Item::Function(item) => &mut item.metadata,
433+
Item::Extern(item) => &mut item.metadata,
434+
}
435+
}
436+
}
437+
463438
impl From<EnumItem> for Item {
464439
fn from(item: EnumItem) -> Self {
465440
Self::Enum(item)

0 commit comments

Comments
 (0)