From b10d3494e301220e6c35ecac6fadcf8275bf3e41 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 18 Nov 2022 21:24:18 +0200 Subject: [PATCH] endpoint label annotation --- .../abi_tester_expected_main.abi.json | 19 +++++++++++++++++++ .../abi-tester/src/abi_tester.rs | 9 +++++++++ .../src/abi_json/endpoint_abi_json.rs | 3 +++ elrond-wasm-derive/src/generate/abi_gen.rs | 4 ++++ elrond-wasm-derive/src/model/endpoint.rs | 2 +- elrond-wasm-derive/src/model/method.rs | 1 + .../src/parse/attributes/attr_names.rs | 2 ++ .../src/parse/attributes/label_attr.rs | 4 ++-- .../src/parse/attributes/mod.rs | 2 ++ .../src/parse/endpoint_parse.rs | 16 +++++++++++++--- elrond-wasm-derive/src/parse/method_parse.rs | 4 +++- elrond-wasm/src/abi/endpoint_abi.rs | 3 ++- 12 files changed, 61 insertions(+), 8 deletions(-) diff --git a/contracts/feature-tests/abi-tester/abi_tester_expected_main.abi.json b/contracts/feature-tests/abi-tester/abi_tester_expected_main.abi.json index 7c2a3cf949..aa27f21653 100644 --- a/contracts/feature-tests/abi-tester/abi_tester_expected_main.abi.json +++ b/contracts/feature-tests/abi-tester/abi_tester_expected_main.abi.json @@ -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": [ diff --git a/contracts/feature-tests/abi-tester/src/abi_tester.rs b/contracts/feature-tests/abi-tester/src/abi_tester.rs index dc478bb12a..19b56bcb68 100644 --- a/contracts/feature-tests/abi-tester/src/abi_tester.rs +++ b/contracts/feature-tests/abi-tester/src/abi_tester.rs @@ -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) {} } diff --git a/elrond-wasm-debug/src/abi_json/endpoint_abi_json.rs b/elrond-wasm-debug/src/abi_json/endpoint_abi_json.rs index b14fd15827..dc9735e611 100644 --- a/elrond-wasm-debug/src/abi_json/endpoint_abi_json.rs +++ b/elrond-wasm-debug/src/abi_json/endpoint_abi_json.rs @@ -97,6 +97,8 @@ pub struct EndpointAbiJson { pub payable_in_tokens: Vec, pub inputs: Vec, pub outputs: Vec, + #[serde(skip_serializing_if = "Vec::is_empty")] + pub labels: Vec, } impl From<&EndpointAbi> for EndpointAbiJson { @@ -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() } } } diff --git a/elrond-wasm-derive/src/generate/abi_gen.rs b/elrond-wasm-derive/src/generate/abi_gen.rs index b26b368446..0cb7c2dc61 100644 --- a/elrond-wasm-derive/src/generate/abi_gen.rs +++ b/elrond-wasm-derive/src/generate/abi_gen.rs @@ -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(); @@ -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 diff --git a/elrond-wasm-derive/src/model/endpoint.rs b/elrond-wasm-derive/src/model/endpoint.rs index e226a5b66d..cf0a004141 100644 --- a/elrond-wasm-derive/src/model/endpoint.rs +++ b/elrond-wasm-derive/src/model/endpoint.rs @@ -1,4 +1,4 @@ -use super::{EndpointLocationMetadata, EndpointMutabilityMetadata, MethodPayableMetadata}; +use super::{EndpointMutabilityMetadata, MethodPayableMetadata, EndpointLocationMetadata}; #[derive(Clone, Debug)] pub struct InitMetadata { diff --git a/elrond-wasm-derive/src/model/method.rs b/elrond-wasm-derive/src/model/method.rs index 766619c0d2..584a60cf64 100644 --- a/elrond-wasm-derive/src/model/method.rs +++ b/elrond-wasm-derive/src/model/method.rs @@ -41,6 +41,7 @@ pub struct Method { pub unprocessed_attributes: Vec, pub method_args: Vec, pub output_names: Vec, + pub label_names: Vec, pub return_type: syn::ReturnType, pub implementation: MethodImpl, } diff --git a/elrond-wasm-derive/src/parse/attributes/attr_names.rs b/elrond-wasm-derive/src/parse/attributes/attr_names.rs index be0479dba2..6c7b838178 100644 --- a/elrond-wasm-derive/src/parse/attributes/attr_names.rs +++ b/elrond-wasm-derive/src/parse/attributes/attr_names.rs @@ -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"; + diff --git a/elrond-wasm-derive/src/parse/attributes/label_attr.rs b/elrond-wasm-derive/src/parse/attributes/label_attr.rs index 0a54240a84..77b9edb9b1 100644 --- a/elrond-wasm-derive/src/parse/attributes/label_attr.rs +++ b/elrond-wasm-derive/src/parse/attributes/label_attr.rs @@ -1,4 +1,4 @@ -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, @@ -6,7 +6,7 @@ pub struct LabelAttribute { impl LabelAttribute{ pub fn parse(attr: &syn::Attribute) -> Option { - 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, }) } diff --git a/elrond-wasm-derive/src/parse/attributes/mod.rs b/elrond-wasm-derive/src/parse/attributes/mod.rs index 1ca68d50ac..37b8326392 100644 --- a/elrond-wasm-derive/src/parse/attributes/mod.rs +++ b/elrond-wasm-derive/src/parse/attributes/mod.rs @@ -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::*; diff --git a/elrond-wasm-derive/src/parse/endpoint_parse.rs b/elrond-wasm-derive/src/parse/endpoint_parse.rs index 4bcf140f80..9c7f8f27d1 100644 --- a/elrond-wasm-derive/src/parse/endpoint_parse.rs +++ b/elrond-wasm-derive/src/parse/endpoint_parse.rs @@ -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, }; @@ -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() +} diff --git a/elrond-wasm-derive/src/parse/method_parse.rs b/elrond-wasm-derive/src/parse/method_parse.rs index bc28fbf23b..da816ee1c4 100644 --- a/elrond-wasm-derive/src/parse/method_parse.rs +++ b/elrond-wasm-derive/src/parse/method_parse.rs @@ -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, @@ -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, }; @@ -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) } diff --git a/elrond-wasm/src/abi/endpoint_abi.rs b/elrond-wasm/src/abi/endpoint_abi.rs index 18abd42fe7..fe29e2242e 100644 --- a/elrond-wasm/src/abi/endpoint_abi.rs +++ b/elrond-wasm/src/abi/endpoint_abi.rs @@ -1,5 +1,5 @@ use super::*; -use alloc::vec::Vec; +use alloc::{vec::Vec}; #[derive(Clone, Debug)] pub struct InputAbi { @@ -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],