Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(doc): add support for @custom:variant documentation for enums #9905

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/doc/src/parser/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum CommentTag {
/// Copies all missing tags from the base function (must be followed by the contract name)
Inheritdoc,
/// Custom tag, semantics is application-defined
Variant,
Copy link
Contributor

@srdtrk srdtrk Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every element of this enum is one of the natspec tags defined in the specs here. And variant isn't one of them.

Instead, is it not possible to parse this as Custom("variant") and do the filtering in the writer?

Custom(String),
}

Expand All @@ -41,6 +42,7 @@ impl CommentTag {
let custom_tag = trimmed.trim_start_matches("custom:").trim();
match custom_tag {
"param" => Self::Param,
"variant" => Self::Variant,
_ => Self::Custom(custom_tag.to_owned()),
}
}
Expand Down
45 changes: 43 additions & 2 deletions crates/doc/src/writer/as_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,26 @@ impl AsDoc for Document {
writer.write_subtitle("Enums")?;
enums.into_iter().try_for_each(|(item, comments, code)| {
writer.write_heading(&item.name.safe_unwrap().name)?;
writer.write_section(comments, code)
writer.write_section(comments, code)?;

let variants: Vec<_> = comments

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to format your code, with cargo +nightly fmt, to avoid this indentation issue. Also, you can use collect::<Vec<_>>() instead of : Vec<_>

.include_tag(CommentTag::Variant)
.iter()
.filter_map(|c| {
let (name, desc) = c.value.split_once(' ')?;
Some((name.trim().to_string(), desc.trim().to_string()))
})
.collect();
if !variants.is_empty() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can add a break line before and after the if statement

writer.writeln()?;
writer.write_bold("Cases:")?;
writer.writeln_raw("| Name | Description |")?;
writer.writeln_raw("|------|-------------|")?;
for (name, desc) in variants {
writer.writeln_raw(&format!("| `{}` | {} |", name, desc))?;
}
}
Ok::<_, std::fmt::Error>(())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the type needs to be explicit

})?;
}
}
Expand Down Expand Up @@ -273,7 +292,29 @@ impl AsDoc for Document {
writer.write_section(&item.comments, &item.code)?;
writer.try_write_errors_table(&err.fields, &item.comments)?;
}
ParseSource::Variable(_) | ParseSource::Enum(_) | ParseSource::Type(_) => {
ParseSource::Enum(_) => {
writer.writeln_doc(&item.comments)?;
writer.write_code(&item.code)?;

let variants: Vec<_> = item.comments

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here for the Vec<_> type

.include_tag(CommentTag::Variant)
.iter()
.filter_map(|c| {
let (name, desc) = c.value.split_once(' ')?;
Some((name.trim().to_string(), desc.trim().to_string()))
})
.collect();
if !variants.is_empty() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A breakline before the if statement can be appreciate

writer.writeln()?;
writer.write_bold("Cases:")?;
writer.writeln_raw("| Name | Description |")?;
writer.writeln_raw("|------|-------------|")?;
for (name, desc) in variants {
writer.writeln_raw(&format!("| `{}` | {} |", name, desc))?;
}
}
}
ParseSource::Variable(_) | ParseSource::Type(_) => {
writer.write_section(&item.comments, &item.code)?;
}
}
Expand Down