Skip to content

Commit

Permalink
Use darling to parse value of macros attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
iho committed Oct 2, 2023
1 parent d11a7f3 commit e4ae6fd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 55 deletions.
13 changes: 7 additions & 6 deletions examples/lockable-fungible-token/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions examples/status-message/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions near-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ strum = "0.24"
strum_macros = "0.24"
quote = "1.0"
Inflector = { version = "0.11.4", default-features = false, features = [] }
darling = { version = "0.20.3" }

[features]
abi = []
Expand Down
1 change: 0 additions & 1 deletion near-sdk-macros/src/core_impl/abi/abi_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ pub fn parse_rustdoc(attrs: &[Attribute]) -> Option<String> {
}
}
}

None
})
.collect::<Vec<_>>()
Expand Down
72 changes: 31 additions & 41 deletions near-sdk-macros/src/core_impl/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,41 @@ use proc_macro2::Span;

use quote::quote;

use syn::parse_macro_input;
use darling::ast::NestedMeta;
use darling::Error;
use darling::FromMeta;
use syn::{parse_quote, ItemEnum, LitStr};

#[derive(Default, FromMeta, Clone, Debug)]
pub struct MacroConfig {
pub event_json: Option<EventsConfig>,
}
#[derive(Default, FromMeta, Clone, Debug)]
pub struct EventsConfig {
standard: Option<String>,
}

use syn::Meta;
use syn::{parse_quote, ItemEnum, LitStr, MetaNameValue};
/// this function is used to inject serialization macros and the `near_sdk::EventMetadata` macro.
/// In addition, this function extracts the event's `standard` value and injects it as a constant to be used by
/// the `near_sdk::EventMetadata` derive macro
pub(crate) fn near_events(attr: TokenStream, item: TokenStream) -> TokenStream {
// get standard from attr args
let args = parse_macro_input!(attr as syn::Meta);
let standard = get_standard_arg(args);
if standard.is_none() {

let attr_args = match NestedMeta::parse_meta_list(attr.into()) {
Ok(v) => v,
Err(e) => {
return TokenStream::from(Error::from(e).write_errors());
}
};

let args = match MacroConfig::from_list(&attr_args) {
Ok(v) => v,
Err(e) => {
return TokenStream::from(e.write_errors());
}
};

if args.event_json.is_none() || args.event_json.clone().unwrap().standard.is_none() {
return TokenStream::from(
syn::Error::new(
Span::call_site(),
Expand All @@ -24,6 +47,8 @@ pub(crate) fn near_events(attr: TokenStream, item: TokenStream) -> TokenStream {
);
}

let standard = args.event_json.unwrap().standard.unwrap();

if let Ok(mut input) = syn::parse::<ItemEnum>(item) {
let name = &input.ident;
let standard_name = format!("{}_event_standard", name);
Expand Down Expand Up @@ -61,38 +86,3 @@ pub(crate) fn get_event_version(var: &syn::Variant) -> Option<LitStr> {
}
None
}

/// this function returns the `standard` value from `#[near_bindgen(event_json(standard = "nepXXX"))]`
fn get_standard_arg(meta: syn::Meta) -> Option<LitStr> {
let mut standard: Option<LitStr> = None;
if meta.path().is_ident("event_json") {
match meta {
Meta::NameValue(MetaNameValue { path, value, .. }) => {
if path.is_ident("standard") {
if let syn::Expr::Lit(lit_str) = value {
if let syn::Lit::Str(lit_str) = lit_str.lit {
standard = Some(lit_str);
}
}
}
}
Meta::Path(path) => {
if path.is_ident("standard") {
standard = Some(LitStr::new("nep141", Span::call_site()));
}
}
Meta::List(list) => {
let _ = list.parse_nested_meta(|meta| {
if meta.path.is_ident("standard") {
let value = meta.value()?; // this parses the `=`
let s: LitStr = value.parse()?; // this parses `"EarlGrey"`
standard = Some(s);
}

Ok(())
});
}
}
}
standard
}
2 changes: 1 addition & 1 deletion near-sdk/tests/event_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use near_sdk::test_utils::get_logs;
use near_sdk::{near_bindgen, AccountId};

#[near_bindgen(event_json(standard = "test_standard", random = "random"))]
#[near_bindgen(event_json(standard = "test_standard"))]
// #[near_bindgen(event_json(standard = "test_standard", random = "random"), other_random)]
pub enum TestEvents<'a, 'b, T>
where
Expand Down

0 comments on commit e4ae6fd

Please sign in to comment.