Skip to content

Commit

Permalink
refactor: move codegen type inside impl scope
Browse files Browse the repository at this point in the history
  • Loading branch information
austinabell committed Nov 3, 2022
1 parent 6eecebe commit 1e6c09e
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions near-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,6 @@ pub fn function_error(item: TokenStream) -> TokenStream {
pub fn derive_event_attributes(item: TokenStream) -> TokenStream {
if let Ok(input) = syn::parse::<ItemEnum>(item) {
let name = &input.ident;
// build wrapper name
let wrapper_name = format!("{}EventBuilder", name);
let wrapper_ident = syn::Ident::new(&wrapper_name, Span::call_site());
// get `standard` const injected from `near_events`
let standard_name = format!("{}_event_standard", name);
let standard_ident = syn::Ident::new(&standard_name, Span::call_site());
Expand Down Expand Up @@ -414,24 +411,23 @@ pub fn derive_event_attributes(item: TokenStream) -> TokenStream {
let (custom_impl_generics, ..) = generics.split_for_impl();

TokenStream::from(quote! {

#[derive(near_sdk::serde::Serialize)]
#[serde(crate="near_sdk::serde")]
#[serde(rename_all="snake_case")]
struct #wrapper_ident #custom_impl_generics #where_clause {
standard: String,
version: String,
#[serde(flatten)]
event_data: &#event_lifetime #name #type_generics
}

impl #impl_generics near_sdk::EventJson for #name #type_generics #where_clause {
type EventMessage = String;
fn format(&self) -> Self::EventMessage {
let (standard, version): (String, String) = match self {
#(#event_meta),*
};
let event = #wrapper_ident {standard, version, event_data: self };

#[derive(near_sdk::serde::Serialize)]
#[serde(crate="near_sdk::serde")]
#[serde(rename_all="snake_case")]
struct EventBuilder #custom_impl_generics #where_clause {
standard: String,
version: String,
#[serde(flatten)]
event_data: &#event_lifetime #name #type_generics
}
let event = EventBuilder { standard, version, event_data: self };
near_sdk::serde_json::to_string(&event)
.unwrap_or_else(|_| near_sdk::env::abort())
}
Expand Down

0 comments on commit 1e6c09e

Please sign in to comment.