Open
Description
So I was recently banging my head against the wall trying to parse wasm attributes and I came up with an idea for making this process painless and convenient. It looks something like this right now but I'd be open to suggestions.
#[derive(Attrs)]
#[cw_serde]
pub enum ExecuteMsg {
#[attrs = "HelloAttrs"]
Hello{},
#[attrs = "WorldAttrs"]
World{}
}
#[derive(MsgAttr)]
pub struct HelloAttrs {
/// The action field should be mandatory and unique so parsing
/// any message can be fully automated. Perhaps the action field shouldn't even be part of the struct
/// and just assumed to be present in the attributes.
action: String,
sender: Addr // <-- fields must be `Serialize + Deserialize`
}
MsgAttr
derives From<Vec<Attribute>>
and Into<Vec<Attribute>>
to make parsing extremely easy, and
Then in the smart contract we can have
let res = Response::new().with_attributes(HelloAttrs{
action: "hello".to_string(),
sender: "foo bar baz".to_string()
});
The Attrs
macro the creates a new enum that looks like this:
pub enum ExecuteMsgAttrs {
Hello(HelloAttrs),
World(WorldAttrs),
}
and we can access to a parsing function like this:
pub fn parse_attrs(attrs: Vec<Attribute>) -> Option<ExecuteMsgAttrs>
This could be made a little more robust by including wasm events as well but I haven't really thought that far ahead. Let me know what you think!