Skip to content

Commit

Permalink
chore: Add Customs type in sylvia-derive
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Sep 5, 2023
1 parent 3ece04a commit 3979f67
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
7 changes: 3 additions & 4 deletions sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,12 +900,11 @@ impl<'a> GlueMessage<'a> {
let dispatch_arms = interfaces.interfaces().iter().map(|interface| {
let ContractMessageAttr {
variant,
has_custom_msg,
has_custom_query,
customs,
..
} = interface;

let ctx = match (msg_ty, has_custom_query) {
let ctx = match (msg_ty, customs.has_query) {
(MsgType::Exec, true )=> quote! {
( ctx.0.into_empty(), ctx.1, ctx.2)
},
Expand All @@ -915,7 +914,7 @@ impl<'a> GlueMessage<'a> {
_=> quote! { ctx },
};

match (msg_ty, has_custom_msg) {
match (msg_ty, customs.has_msg) {
(MsgType::Exec, true) => quote! {
#contract_name :: #variant(msg) => #sylvia ::into_response::IntoResponse::into_response(msg.dispatch(contract, Into::into( #ctx ))?)
},
Expand Down
38 changes: 22 additions & 16 deletions sylvia-derive/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,19 @@ impl Parse for ContractErrorAttr {
}
}

#[derive(Debug)]
pub struct Customs {
pub has_msg: bool,
pub has_query: bool,
}

#[derive(Debug)]
pub struct ContractMessageAttr {
pub module: Path,
pub exec_generic_params: Vec<Path>,
pub query_generic_params: Vec<Path>,
pub variant: Ident,
pub has_custom_msg: bool,
pub has_custom_query: bool,
pub customs: Customs,
}

#[cfg(not(tarpaulin_include))]
Expand All @@ -263,14 +268,20 @@ fn parse_generics(content: &ParseBuffer) -> Result<Vec<Path>> {
Ok(params)
}

fn interface_has_custom(content: ParseStream) -> Result<(bool, bool)> {
let mut has_custom_msg = false;
let mut has_custom_query = false;
fn interface_has_custom(content: ParseStream) -> Result<Customs> {
let mut customs = Customs {
has_msg: false,
has_query: false,
};

if !content.peek(Token![:]) {
return Ok(customs);
}

let _: Token![:] = content.parse()?;
let attr: Ident = content.parse()?;
if attr != "custom" {
return Ok((has_custom_msg, has_custom_query));
return Ok(customs);
}

let custom_content;
Expand All @@ -279,8 +290,8 @@ fn interface_has_custom(content: ParseStream) -> Result<(bool, bool)> {
while !custom_content.is_empty() {
let custom = custom_content.parse::<Path>()?;
match custom.get_ident() {
Some(ident) if ident == "msg" => has_custom_msg = true,
Some(ident) if ident == "query" => has_custom_query = true,
Some(ident) if ident == "msg" => customs.has_msg = true,
Some(ident) if ident == "query" => customs.has_query = true,
_ => {
return Err(Error::new(
custom.span(),
Expand All @@ -289,7 +300,7 @@ fn interface_has_custom(content: ParseStream) -> Result<(bool, bool)> {
}
}
}
Ok((has_custom_msg, has_custom_query))
Ok(customs)
}

#[cfg(not(tarpaulin_include))]
Expand Down Expand Up @@ -329,11 +340,7 @@ impl Parse for ContractMessageAttr {
let _: Token![as] = content.parse()?;
let variant = content.parse()?;

let (has_custom_msg, has_custom_query) = if content.peek(Token![:]) {
interface_has_custom(&content)?
} else {
(false, false)
};
let customs = interface_has_custom(&content)?;

if !content.is_empty() {
return Err(Error::new(
Expand All @@ -347,8 +354,7 @@ impl Parse for ContractMessageAttr {
exec_generic_params,
query_generic_params,
variant,
has_custom_msg,
has_custom_query,
customs,
})
}
}
Expand Down

0 comments on commit 3979f67

Please sign in to comment.