Skip to content

Commit

Permalink
feat: add an option to include rustdoc in ABI (#876)
Browse files Browse the repository at this point in the history
  • Loading branch information
itegulov authored Aug 4, 2022
1 parent bdf54e9 commit 496b797
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/abi/res/abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"functions": [
{
"name": "add",
"doc": " Adds two pairs point-wise.",
"is_view": true,
"params": [
{
Expand Down
1 change: 1 addition & 0 deletions examples/abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Adder {}

#[near_bindgen]
impl Adder {
/// Adds two pairs point-wise.
pub fn add(&self, a: Pair, b: Pair) -> Pair {
sum_pair(&a, &b)
}
Expand Down
6 changes: 6 additions & 0 deletions near-sdk-macros/src/core_impl/abi/abi_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ impl ImplItemMethodInfo {
/// If args are serialized with Borsh it will not include `#[derive(borsh::BorshSchema)]`.
pub fn abi_struct(&self) -> TokenStream2 {
let function_name_str = self.attr_signature_info.ident.to_string();
let function_doc =
match super::doc::parse_rustdoc(&self.attr_signature_info.non_bindgen_attrs) {
Some(doc) => quote! { Some(#doc.to_string()) },
None => quote! { None },
};
let is_view = matches!(&self.attr_signature_info.method_type, &MethodType::View);
let is_init = matches!(
&self.attr_signature_info.method_type,
Expand Down Expand Up @@ -153,6 +158,7 @@ impl ImplItemMethodInfo {
quote! {
near_sdk::__private::AbiFunction {
name: #function_name_str.to_string(),
doc: #function_doc,
is_view: #is_view,
is_init: #is_init,
is_payable: #is_payable,
Expand Down
25 changes: 25 additions & 0 deletions near-sdk-macros/src/core_impl/abi/doc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use syn::{Attribute, Lit::Str, Meta::NameValue, MetaNameValue};

pub fn parse_rustdoc(attrs: &[Attribute]) -> Option<String> {
let doc = attrs
.iter()
.filter_map(|attr| {
if attr.path.is_ident("doc") {
if let NameValue(MetaNameValue { lit: Str(s), .. }) = attr.parse_meta().ok()? {
Some(s.value())
} else {
None
}
} else {
None
}
})
.collect::<Vec<_>>()
.join("\n");

if doc.is_empty() {
None
} else {
Some(doc)
}
}
1 change: 1 addition & 0 deletions near-sdk-macros/src/core_impl/abi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod abi_generator;
pub mod abi_visitor;
mod doc;
3 changes: 3 additions & 0 deletions near-sdk/src/private/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ impl Abi {
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub struct AbiFunction {
pub name: String,
/// Human-readable documentation parsed from the source file.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub doc: Option<String>,
/// Whether function does not modify the state.
#[serde(default, skip_serializing_if = "is_false")]
pub is_view: bool,
Expand Down

0 comments on commit 496b797

Please sign in to comment.