Skip to content

Commit

Permalink
endpoint label annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Nov 18, 2022
1 parent e2b95c0 commit b10d349
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,25 @@
],
"inputs": [],
"outputs": []
},
{
"name": "label_a",
"mutability": "mutable",
"inputs": [],
"outputs": [],
"labels": [
"label1"
]
},
{
"name": "label_b",
"mutability": "mutable",
"inputs": [],
"outputs": [],
"labels": [
"label1",
"label2"
]
}
],
"events": [
Expand Down
9 changes: 9 additions & 0 deletions contracts/feature-tests/abi-tester/src/abi_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,13 @@ pub trait AbiTester {

#[event("address-h256-event")]
fn address_h256_event(&self, #[indexed] address: &Address, #[indexed] h256: &H256);

#[endpoint]
#[label("label1")]
fn label_a(&self) {}

#[endpoint]
#[label("label1")]
#[label("label2")]
fn label_b(&self) {}
}
3 changes: 3 additions & 0 deletions elrond-wasm-debug/src/abi_json/endpoint_abi_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ pub struct EndpointAbiJson {
pub payable_in_tokens: Vec<String>,
pub inputs: Vec<InputAbiJson>,
pub outputs: Vec<OutputAbiJson>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub labels: Vec<String>,
}

impl From<&EndpointAbi> for EndpointAbiJson {
Expand All @@ -118,6 +120,7 @@ impl From<&EndpointAbi> for EndpointAbiJson {
.collect(),
inputs: abi.inputs.iter().map(InputAbiJson::from).collect(),
outputs: abi.outputs.iter().map(OutputAbiJson::from).collect(),
labels: abi.labels.iter().map(|&label| label.to_owned()).collect()
}
}
}
4 changes: 4 additions & 0 deletions elrond-wasm-derive/src/generate/abi_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ fn generate_endpoint_snippet(
}
},
};


let label_names = &m.label_names;
let mutability_tokens = mutability.to_tokens();
let location_tokens = location.to_tokens();

Expand All @@ -62,6 +65,7 @@ fn generate_endpoint_snippet(
payable_in_tokens: &[ #(#payable_in_tokens),* ],
inputs: elrond_wasm::types::heap::Vec::new(),
outputs: elrond_wasm::types::heap::Vec::new(),
labels: &[ #(#label_names),* ],
};
#(#input_snippets)*
#output_snippet
Expand Down
2 changes: 1 addition & 1 deletion elrond-wasm-derive/src/model/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{EndpointLocationMetadata, EndpointMutabilityMetadata, MethodPayableMetadata};
use super::{EndpointMutabilityMetadata, MethodPayableMetadata, EndpointLocationMetadata};

#[derive(Clone, Debug)]
pub struct InitMetadata {
Expand Down
1 change: 1 addition & 0 deletions elrond-wasm-derive/src/model/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct Method {
pub unprocessed_attributes: Vec<syn::Attribute>,
pub method_args: Vec<MethodArgument>,
pub output_names: Vec<String>,
pub label_names: Vec<String>,
pub return_type: syn::ReturnType,
pub implementation: MethodImpl,
}
Expand Down
2 changes: 2 additions & 0 deletions elrond-wasm-derive/src/parse/attributes/attr_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ pub(super) static ATTR_STORAGE_MAPPER: &str = "storage_mapper";
pub(super) static ATTR_STORAGE_IS_EMPTY: &str = "storage_is_empty";
pub(super) static ATTR_STORAGE_CLEAR: &str = "storage_clear";
pub(super) static ATTR_PROXY: &str = "proxy";
pub(super) static ATTR_LABEL: &str = "label";

4 changes: 2 additions & 2 deletions elrond-wasm-derive/src/parse/attributes/label_attr.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::{util::is_attr_one_string_arg, trait_prop_names::PROP_LABEL};
use super::{util::is_attr_one_string_arg, attr_names::ATTR_LABEL};

pub struct LabelAttribute {
pub label: String,
}

impl LabelAttribute{
pub fn parse(attr: &syn::Attribute) -> Option<Self> {
is_attr_one_string_arg(attr, PROP_LABEL).map(|arg_str| LabelAttribute {
is_attr_one_string_arg(attr, ATTR_LABEL).map(|arg_str| LabelAttribute {
label: arg_str,
})
}
Expand Down
2 changes: 2 additions & 0 deletions elrond-wasm-derive/src/parse/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ mod payable_attr;
mod storage_attr;
mod trait_argument_prop;
mod trait_prop_names;
mod label_attr;
mod util;

pub use argument_attr::*;
pub use doc_attr::{extract_doc, OutputNameAttribute};
pub use endpoint_attr::*;
pub use event_attr::*;
pub use label_attr::*;
pub use payable_attr::*;
pub use storage_attr::*;
pub use trait_argument_prop::*;
16 changes: 13 additions & 3 deletions elrond-wasm-derive/src/parse/endpoint_parse.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::model::{
CallbackMetadata, EndpointLocationMetadata, EndpointMetadata, EndpointMutabilityMetadata,
InitMetadata, Method, PublicRole,
CallbackMetadata, EndpointMetadata, EndpointMutabilityMetadata,
InitMetadata, Method, PublicRole, EndpointLocationMetadata,
};

use super::{
attributes::{
is_callback_raw, is_init, is_only_admin, is_only_owner, is_only_user_account,
CallbackAttribute, EndpointAttribute, ExternalViewAttribute, OutputNameAttribute,
ViewAttribute,
ViewAttribute, LabelAttribute,
},
MethodAttributesPass1,
};
Expand Down Expand Up @@ -174,3 +174,13 @@ pub fn process_output_names_attribute(attr: &syn::Attribute, method: &mut Method
})
.is_some()
}

pub fn process_label_names_attribute(
attr: &syn::Attribute,
method: &mut Method) -> bool {


LabelAttribute::parse(attr).map(|label_attr|{
method.label_names.push(label_attr.label);
}).is_some()
}
4 changes: 3 additions & 1 deletion elrond-wasm-derive/src/parse/method_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{
process_endpoint_attribute, process_external_view_attribute, process_init_attribute,
process_only_admin_attribute, process_only_owner_attribute,
process_only_user_account_attribute, process_output_names_attribute, process_payable_attribute,
process_view_attribute,
process_view_attribute, process_label_names_attribute,
};
pub struct MethodAttributesPass1 {
pub method_name: String,
Expand Down Expand Up @@ -54,6 +54,7 @@ pub fn process_method(m: &syn::TraitItemMethod, trait_attributes: &TraitProperti
unprocessed_attributes: Vec::new(),
method_args,
output_names: Vec::new(),
label_names: Vec::new(),
return_type: m.sig.output.clone(),
implementation,
};
Expand Down Expand Up @@ -123,4 +124,5 @@ fn process_attribute_second_pass(
|| process_storage_is_empty_attribute(attr, method)
|| process_storage_clear_attribute(attr, method)
|| process_output_names_attribute(attr, method)
|| process_label_names_attribute(attr, method)
}
3 changes: 2 additions & 1 deletion elrond-wasm/src/abi/endpoint_abi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use alloc::vec::Vec;
use alloc::{vec::Vec};

#[derive(Clone, Debug)]
pub struct InputAbi {
Expand Down Expand Up @@ -38,6 +38,7 @@ pub struct EndpointAbi {
pub rust_method_name: &'static str,
pub only_owner: bool,
pub only_admin: bool,
pub labels: &'static [&'static str],
pub mutability: EndpointMutabilityAbi,
pub location: EndpointLocationAbi,
pub payable_in_tokens: &'static [&'static str],
Expand Down

0 comments on commit b10d349

Please sign in to comment.