Skip to content

Commit da6e64e

Browse files
committed
tools/docgen: simplify comment_and_requirement
Exploit some aspects of how we know cbindgen produces items. Verified to produce no diff compared to previous: ``` $ cargo run -p rustls-ffi-tools --bin docgen > website/static/api.json <snipped> $ diff website/static/api.json website/static/api.ef4e484.json && echo "no diff" no diff ```
1 parent 90634b7 commit da6e64e

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

tools/src/bin/docgen/main.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -187,31 +187,29 @@ fn comment_and_requirement(
187187
node: Node,
188188
src: &[u8],
189189
) -> Result<(Option<Comment>, Option<Feature>), Box<dyn Error>> {
190-
let mut maybe_comment = Comment::new(node, src).ok();
190+
// Node is a comment, potentially with a feature requirement ahead of it.
191+
if let Ok(comment) = Comment::new(node, src) {
192+
return Ok(match node.prev_named_sibling() {
193+
Some(prev) => (Some(comment), Feature::new(prev, src).ok()),
194+
None => (Some(comment), None)
195+
});
196+
}
191197

192198
// If node wasn't a comment, see if it was an expression_statement
193199
// that itself was preceded by a comment. This skips over
194200
// expression-like preprocessor attributes on function decls.
195-
if let (None, "expression_statement", Some(prev)) =
196-
(&maybe_comment, node.kind(), node.prev_sibling())
197-
{
198-
maybe_comment = Comment::new(prev, src).ok();
199-
}
200-
201-
// If prev wasn't a comment, see if it was a feature requirement.
202-
if maybe_comment.is_none() {
203-
return Ok(match Feature::new(node, src).ok() {
204-
Some(feat_req) => (None, Some(feat_req)),
205-
None => (None, None),
201+
if let ("expression_statement", Some(prev)) =
202+
(node.kind(), node.prev_sibling()) {
203+
return Ok(match prev.prev_named_sibling() {
204+
// The comment may also be preceded by a feature requirement
205+
Some(prev_prev) => (Comment::new(prev, src).ok(), Feature::new(prev_prev, src).ok()),
206+
None => (Comment::new(prev, src).ok(), None),
206207
});
207208
}
208209

209-
// Otherwise, check the prev of the comment for a feature requirement
210-
let Some(prev) = node.prev_named_sibling() else {
211-
return Ok((maybe_comment, None));
212-
};
213-
214-
Ok((maybe_comment, Feature::new(prev, src).ok()))
210+
// Node wasn't a comment or preprocessor attribute, might be a
211+
// bare feature requirement.
212+
Ok((None, Feature::new(node, src).ok()))
215213
}
216214

217215
#[derive(Debug, Default, Serialize)]

0 commit comments

Comments
 (0)