Skip to content

Commit

Permalink
feat(minor-axelar-wasm-std-derive): derive macro into_event_derive (#733
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fish-sammy authored Dec 24, 2024
1 parent fbbb8d5 commit 7240b50
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions packages/axelar-wasm-std-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ edition = { workspace = true }
proc-macro = true

[dependencies]
cosmwasm-std = { workspace = true }
error-stack = { workspace = true }
quote = "1.0.33"
quote = { workspace = true }
report = { workspace = true }
syn = "2.0.29"
syn = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
axelar-wasm-std = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }

[lints]
workspace = true
78 changes: 78 additions & 0 deletions packages/axelar-wasm-std-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,81 @@ pub fn into_contract_error_derive(input: TokenStream) -> TokenStream {

gen.into()
}

/// Derive macro to implement `From` for a struct to convert it into a `cosmwasm_std::Event`.
///
/// # Examples
///
/// ```
/// use std::collections::HashMap;
/// use serde::Serialize;
///
/// use axelar_wasm_std_derive::into_event;
///
/// #[derive(Serialize)]
/// struct SomeObject {
/// pub some_option: Option<String>,
/// pub some_other_option: Option<String>,
/// pub some_vec: Vec<String>,
/// pub some_map: HashMap<String, String>,
/// }
///
/// #[derive(Serialize)]
/// #[into_event("some_event")]
/// struct SomeEvent {
/// pub some_uint: u64,
/// pub some_string: String,
/// pub some_bool: bool,
/// pub some_object: SomeObject,
/// }
///
/// let event = SomeEvent {
/// some_uint: 42,
/// some_string: "string".to_string(),
/// some_bool: true,
/// some_object: SomeObject {
/// some_option: Some("some".to_string()),
/// some_other_option: None,
/// some_vec: vec!["a".to_string(), "b".to_string()],
/// some_map: [("a".to_string(), "b".to_string()), ("c".to_string(), "d".to_string()), ("e".to_string(), "f".to_string())].into_iter().collect(),
/// }
/// };
/// let actual_event = cosmwasm_std::Event::from(event);
/// let expected_event = cosmwasm_std::Event::new("some_event")
/// .add_attribute("some_bool", "true")
/// .add_attribute("some_object", r#"{"some_map":{"a":"b","c":"d","e":"f"},"some_option":"some","some_other_option":null,"some_vec":["a","b"]}"#)
/// .add_attribute("some_string", "\"string\"")
/// .add_attribute("some_uint", "42");
///
/// assert_eq!(actual_event, expected_event);
/// ```
#[proc_macro_attribute]
pub fn into_event(arg: TokenStream, input: TokenStream) -> TokenStream {
let event_name = syn::parse_macro_input!(arg as syn::LitStr);
let input = syn::parse_macro_input!(input as syn::ItemStruct);

let event_struct = input.ident.clone();

TokenStream::from(quote! {
#input

impl From<&#event_struct> for cosmwasm_std::Event {
fn from(event: &#event_struct) -> Self {
let json_value = serde_json::to_value(event).expect("failed to serialize event");
let attributes = json_value
.as_object()
.expect("event must be a json object")
.into_iter()
.map(|(key, value)| cosmwasm_std::Attribute::new(key, value.to_string()));

cosmwasm_std::Event::new(#event_name.to_string()).add_attributes(attributes)
}
}

impl From<#event_struct> for cosmwasm_std::Event {
fn from(event: #event_struct) -> Self {
(&event).into()
}
}
})
}
4 changes: 2 additions & 2 deletions packages/events-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ proc-macro = true
[dependencies]
error-stack = { workspace = true }
events = { workspace = true }
quote = "1.0.33"
quote = { workspace = true }
serde = "1.0.183"
serde_json = "1.0.105"
syn = "2.0.29"
syn = { workspace = true }

[lints]
workspace = true
4 changes: 2 additions & 2 deletions packages/into-inner-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ proc-macro = true

[dependencies]
error-stack = { workspace = true }
quote = "1.0.33"
quote = { workspace = true }
report = { workspace = true }
syn = "2.0.29"
syn = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
Expand Down

0 comments on commit 7240b50

Please sign in to comment.