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(sol-macro): improve generated docs #338

Merged
merged 1 commit into from
Oct 6, 2023
Merged
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
10 changes: 6 additions & 4 deletions crates/sol-macro/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ pub fn docs(attrs: &[Attribute]) -> impl Iterator<Item = &Attribute> {
attrs.iter().filter(|attr| attr.path().is_ident("doc"))
}

pub fn has_docs(attrs: &[Attribute]) -> bool {
docs(attrs).next().is_some()
}

pub fn derives(attrs: &[Attribute]) -> impl Iterator<Item = &Attribute> {
attrs.iter().filter(|attr| attr.path().is_ident("derive"))
}
Expand All @@ -42,6 +38,7 @@ pub fn derives_mapped(attrs: &[Attribute]) -> impl Iterator<Item = Path> + '_ {
pub struct SolAttrs {
pub all_derives: Option<bool>,
pub extra_methods: Option<bool>,
pub docs: Option<bool>,

// TODO: Implement
pub rename: Option<LitStr>,
Expand Down Expand Up @@ -113,6 +110,7 @@ impl SolAttrs {
match_! {
all_derives => bool()?,
extra_methods => bool()?,
docs => bool()?,

rename => lit()?,
rename_all => CasingStyle::from_lit(&lit()?)?,
Expand Down Expand Up @@ -270,6 +268,10 @@ mod tests {
#[sol(extra_methods)] => Ok(sol_attrs! { extra_methods: true }),
#[sol(extra_methods = true)] => Ok(sol_attrs! { extra_methods: true }),
#[sol(extra_methods = false)] => Ok(sol_attrs! { extra_methods: false }),

#[sol(docs)] => Ok(sol_attrs! { docs: true }),
#[sol(docs = true)] => Ok(sol_attrs! { docs: true }),
#[sol(docs = false)] => Ok(sol_attrs! { docs: false }),
}

rename {
Expand Down
5 changes: 3 additions & 2 deletions crates/sol-macro/src/expand/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, contract: &ItemContract) -> Result<TokenS
.extra_methods
.or(cx.attrs.extra_methods)
.unwrap_or(false);
let docs = sol_attrs.docs.or(cx.attrs.docs).unwrap_or(true);

let bytecode = sol_attrs.bytecode.map(|lit| {
let name = Ident::new("BYTECODE", lit.span());
Expand Down Expand Up @@ -88,8 +89,8 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, contract: &ItemContract) -> Result<TokenS
});

let mod_attrs = attr::docs(&attrs);
let mod_docs = (!attr::has_docs(&attrs))
.then(|| attr::mk_doc("Module containing a contract's types and functions."));
let mod_docs =
docs.then(|| attr::mk_doc("Module containing a contract's types and functions."));
let tokens = quote! {
#mod_docs
#(#mod_attrs)*
Expand Down
8 changes: 5 additions & 3 deletions crates/sol-macro/src/expand/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, error: &ItemError) -> Result<TokenStream>
} = error;
cx.assert_resolved(params)?;

let (_sol_attrs, mut attrs) = crate::attr::SolAttrs::parse(attrs)?;
let (sol_attrs, mut attrs) = crate::attr::SolAttrs::parse(attrs)?;
cx.derives(&mut attrs, params, true);
let docs = sol_attrs.docs.or(cx.attrs.docs).unwrap_or(true);

let tokenize_impl = expand_tokenize_func(params.iter());

Expand All @@ -37,10 +38,11 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, error: &ItemError) -> Result<TokenStream>

let converts = expand_from_into_tuples(&name.0, params);
let fields = expand_fields(params);
let doc = (!attr::has_docs(&attrs)).then(|| {
let doc = docs.then(|| {
let selector = hex::encode_prefixed(selector.array);
attr::mk_doc(format!(
"Custom error with signature `{signature}` and selector `{selector}`."
"Custom error with signature `{signature}` and selector `{selector}`.\n\
```solidity\n{error}\n```"
))
});
let tokens = quote! {
Expand Down
8 changes: 5 additions & 3 deletions crates/sol-macro/src/expand/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, event: &ItemEvent) -> Result<TokenStream>
let ItemEvent { attrs, .. } = event;
let params = event.params();

let (_sol_attrs, mut attrs) = crate::attr::SolAttrs::parse(attrs)?;
let (sol_attrs, mut attrs) = crate::attr::SolAttrs::parse(attrs)?;
cx.derives(&mut attrs, &params, true);
let docs = sol_attrs.docs.or(cx.attrs.docs).unwrap_or(true);

cx.assert_resolved(&params)?;
event.assert_valid()?;
Expand Down Expand Up @@ -105,10 +106,11 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, event: &ItemEvent) -> Result<TokenStream>
.enumerate()
.map(|(i, assign)| quote!(out[#i] = #assign;));

let doc = (!attr::has_docs(&attrs)).then(|| {
let doc = docs.then(|| {
let selector = hex::encode_prefixed(selector.array);
attr::mk_doc(format!(
"Event with signature `{signature}` and selector `{selector}`."
"Event with signature `{signature}` and selector `{selector}`.\n\
```solidity\n{event}\n```"
))
});
let tokens = quote! {
Expand Down
10 changes: 6 additions & 4 deletions crates/sol-macro/src/expand/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result<TokenS
cx.assert_resolved(returns)?;
}

let (_sol_attrs, mut call_attrs) = crate::attr::SolAttrs::parse(attrs)?;
let (sol_attrs, mut call_attrs) = crate::attr::SolAttrs::parse(attrs)?;
let mut return_attrs = call_attrs.clone();
cx.derives(&mut call_attrs, arguments, true);
if !returns.is_empty() {
cx.derives(&mut return_attrs, returns, true);
}
let docs = sol_attrs.docs.or(cx.attrs.docs).unwrap_or(true);

let call_name = cx.call_name(function);
let return_name = cx.return_name(function);
Expand All @@ -67,13 +68,14 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result<TokenS
let selector = crate::utils::selector(&signature);
let tokenize_impl = expand_tokenize_func(arguments.iter());

let call_doc = (!attr::has_docs(attrs)).then(|| {
let call_doc = docs.then(|| {
let selector = hex::encode_prefixed(selector.array);
attr::mk_doc(format!(
"Function with signature `{signature}` and selector `0x{selector}`."
"Function with signature `{signature}` and selector `0x{selector}`.\n\
```solidity\n{function}\n```"
))
});
let return_doc = (!attr::has_docs(&return_attrs)).then(|| {
let return_doc = docs.then(|| {
attr::mk_doc(format!(
"Container type for the return parameters of the [`{signature}`]({call_name}) function."
))
Expand Down
17 changes: 11 additions & 6 deletions crates/sol-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,17 @@ mod utils;
/// but this may change in the future.
///
/// List of all `#[sol(...)]` supported attributes:
/// - `all_derives`: adds all possible `#[derive(...)]` attributes to all
/// generated types. May significantly increase compile times due to all the
/// extra generated code. This is the default behaviour of [`abigen`][abigen]
/// - `extra_methods`: adds extra implementations and methods to all applicable
/// generated types, such as `From` impls and `as_<variant>` methods. May
/// significantly increase compile times due to all the extra generated code
/// - `all_derives [ = <bool = false>]`: adds all possible `#[derive(...)]`
/// attributes to all generated types. May significantly increase compile
/// times due to all the extra generated code. This is the default behaviour
/// of [`abigen`][abigen]
/// - `extra_methods [ = <bool = false>]`: adds extra implementations and
/// methods to all applicable generated types, such as `From` impls and
/// `as_<variant>` methods. May significantly increase compile times due to
/// all the extra generated code. This is the default behaviour of
/// [`abigen`][abigen]
/// - `docs [ = <bool = true>]`: adds doc comments to all generated types. This
/// is the default behaviour of [`abigen`][abigen]
/// - `bytecode = <hex string literal>`: specifies the creation/init bytecode of
/// a contract. This will emit a `static` item with the specified bytes.
/// - `deployed_bytecode = <hex string literal>`: specifies the deployed
Expand Down