-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1345 from CosmWasm/cosmwasm-msg-attr-macro
Implement `msg` macro to simplify configuring serialization
- Loading branch information
Showing
5 changed files
with
247 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use syn::{parse_quote, DeriveInput}; | ||
|
||
pub fn cw_serde_impl(input: DeriveInput) -> DeriveInput { | ||
match input.data { | ||
syn::Data::Struct(_) => parse_quote! { | ||
#[derive( | ||
serde::Serialize, | ||
serde::Deserialize, | ||
Clone, | ||
Debug, | ||
PartialEq, | ||
schemars::JsonSchema | ||
)] | ||
#[serde(deny_unknown_fields)] | ||
#input | ||
}, | ||
syn::Data::Enum(_) => parse_quote! { | ||
#[derive( | ||
serde::Serialize, | ||
serde::Deserialize, | ||
Clone, | ||
Debug, | ||
PartialEq, | ||
schemars::JsonSchema | ||
)] | ||
#[serde(deny_unknown_fields, rename_all = "snake_case")] | ||
#input | ||
}, | ||
syn::Data::Union(_) => panic!("unions are not supported"), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn structs() { | ||
let expanded = cw_serde_impl(parse_quote! { | ||
pub struct InstantiateMsg { | ||
pub verifier: String, | ||
pub beneficiary: String, | ||
} | ||
}); | ||
|
||
let expected = parse_quote! { | ||
#[derive( | ||
serde::Serialize, | ||
serde::Deserialize, | ||
Clone, | ||
Debug, | ||
PartialEq, | ||
schemars::JsonSchema | ||
)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct InstantiateMsg { | ||
pub verifier: String, | ||
pub beneficiary: String, | ||
} | ||
}; | ||
|
||
assert_eq!(expanded, expected); | ||
} | ||
|
||
#[test] | ||
fn enums() { | ||
let expanded = cw_serde_impl(parse_quote! { | ||
pub enum SudoMsg { | ||
StealFunds { | ||
recipient: String, | ||
amount: Vec<Coin>, | ||
}, | ||
} | ||
}); | ||
|
||
let expected = parse_quote! { | ||
#[derive( | ||
serde::Serialize, | ||
serde::Deserialize, | ||
Clone, | ||
Debug, | ||
PartialEq, | ||
schemars::JsonSchema | ||
)] | ||
#[serde(deny_unknown_fields, rename_all = "snake_case")] | ||
pub enum SudoMsg { | ||
StealFunds { | ||
recipient: String, | ||
amount: Vec<Coin>, | ||
}, | ||
} | ||
}; | ||
|
||
assert_eq!(expanded, expected); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "unions are not supported")] | ||
fn unions() { | ||
cw_serde_impl(parse_quote! { | ||
pub union SudoMsg { | ||
x: u32, | ||
y: u32, | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters