Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Parse additional parameters for sv::msg(reply) (#426) #433

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 63 additions & 8 deletions sylvia-derive/src/parser/attributes/msg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro_error::emit_error;
use syn::parse::{Error, Parse, ParseStream, Parser};
use syn::{Ident, MetaList, Result, Token};
use syn::{bracketed, Ident, MetaList, Result, Token};

/// Type of message to be generated
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
Expand All @@ -17,24 +17,47 @@
#[derive(Default)]
struct ArgumentParser {
pub resp_type: Option<Ident>,
pub reply_handlers: Vec<Ident>,
pub reply_on: Option<ReplyOn>,
}

impl Parse for ArgumentParser {
fn parse(input: ParseStream) -> Result<Self> {
let mut result = Self::default();

while input.peek2(Ident) {
let _: Token![,] = input.parse()?;
let arg_type: Ident = input.parse()?;
let _: Token![=] = input.parse()?;
match arg_type.to_string().as_str() {
"resp" => {
let _: Token![=] = input.parse()?;
let resp_type: Ident = input.parse()?;
result.resp_type = Some(resp_type);
}
"handlers" => {
let _: Token![=] = input.parse()?;
let handlers_content;
bracketed!(handlers_content in input);

while !handlers_content.is_empty() {
let handler = handlers_content.parse::<Ident>()?;
result.reply_handlers.push(handler);
if !handlers_content.peek(Token![,]) {
break;
}
let _: Token![,] = handlers_content.parse()?;
}
}
"reply_on" => {
let _: Token![=] = input.parse()?;
let reply_on: Ident = input.parse()?;
let reply_on = ReplyOn::new(reply_on)?;
result.reply_on = Some(reply_on);
}
_ => {
return Err(Error::new(
input.span(),
"Invalid argument type, expected `resp` or no argument.",
arg_type.span(),
"Invalid argument type, expected `resp`, `handlers`, `reply_on` or no argument.",

Check warning on line 60 in sylvia-derive/src/parser/attributes/msg.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/msg.rs#L59-L60

Added lines #L59 - L60 were not covered by tests
))
}
}
Expand All @@ -43,14 +66,42 @@
}
}

/// Representation of `reply_on` parameter in `#[sv::msg(reply(...))]` attribute.
#[derive(Default, Clone)]
pub enum ReplyOn {
Success,
Failure,
#[default]
Always,
}

impl ReplyOn {
pub fn new(reply_on: Ident) -> Result<Self> {
match reply_on.to_string().as_str() {
"success" => Ok(Self::Success),
"failure" => Ok(Self::Failure),
"always" => Ok(Self::Always),
_ => Err(Error::new(
reply_on.span(),
"Invalid argument type, expected one of `success`, `failure` or `always`.",

Check warning on line 86 in sylvia-derive/src/parser/attributes/msg.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/msg.rs#L84-L86

Added lines #L84 - L86 were not covered by tests
)),
}
}
}

/// Parsed representation of `#[sv::msg(...)]` attribute.
#[derive(Clone)]
pub enum MsgAttr {
Exec,
Query { resp_type: Option<Ident> },
Query {
resp_type: Option<Ident>,
},
Instantiate,
Migrate,
Reply,
Reply {
_handlers: Vec<Ident>,
_reply_on: ReplyOn,
},
Sudo,
}

Expand Down Expand Up @@ -85,14 +136,18 @@
impl Parse for MsgAttr {
fn parse(input: ParseStream) -> Result<Self> {
let ty: Ident = input.parse()?;
let ArgumentParser { resp_type } = ArgumentParser::parse(input)?;
let ArgumentParser {
resp_type,
reply_handlers,
reply_on,
} = ArgumentParser::parse(input)?;

Check warning on line 143 in sylvia-derive/src/parser/attributes/msg.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/msg.rs#L143

Added line #L143 was not covered by tests

let result = match ty.to_string().as_str() {
"exec" => Self::Exec,
"query" => Self::Query { resp_type },
"instantiate" => Self::Instantiate,
"migrate" => Self::Migrate,
"reply" => Self::Reply,
"reply" => Self::Reply {_handlers: reply_handlers, _reply_on: reply_on.unwrap_or_default()},
"sudo" => Self::Sudo,
_ => return Err(Error::new(
input.span(),
Expand Down
46 changes: 46 additions & 0 deletions sylvia/tests/reply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use sylvia::contract;
use sylvia::cw_std::{Reply, Response, StdResult};
use sylvia::types::{InstantiateCtx, ReplyCtx};

pub struct Contract;

#[contract]
impl Contract {
pub fn new() -> Self {
Self

Check warning on line 10 in sylvia/tests/reply.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/reply.rs#L9-L10

Added lines #L9 - L10 were not covered by tests
}

#[sv::msg(instantiate)]
pub fn instantiate(&self, _ctx: InstantiateCtx) -> StdResult<Response> {
Ok(Response::new())

Check warning on line 15 in sylvia/tests/reply.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/reply.rs#L14-L15

Added lines #L14 - L15 were not covered by tests
}

#[sv::msg(reply)]
fn clean(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response> {
Ok(Response::new())

Check warning on line 20 in sylvia/tests/reply.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/reply.rs#L19-L20

Added lines #L19 - L20 were not covered by tests
}

#[allow(dead_code)]
#[sv::msg(reply, handlers=[handler_one, handler_two])]
fn custom_handlers(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response> {
Ok(Response::new())

Check warning on line 26 in sylvia/tests/reply.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/reply.rs#L25-L26

Added lines #L25 - L26 were not covered by tests
}

#[allow(dead_code)]
#[sv::msg(reply, reply_on = success)]
fn reply_on(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response> {
Ok(Response::new())

Check warning on line 32 in sylvia/tests/reply.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/reply.rs#L31-L32

Added lines #L31 - L32 were not covered by tests
}

#[allow(dead_code)]
#[sv::msg(reply, reply_on = always)]
fn reply_on_always(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response> {
Ok(Response::new())

Check warning on line 38 in sylvia/tests/reply.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/reply.rs#L37-L38

Added lines #L37 - L38 were not covered by tests
}

#[allow(dead_code)]
#[sv::msg(reply, handlers=[handler_one, handler_two], reply_on = failure)]
fn both_parameters(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response> {
Ok(Response::new())

Check warning on line 44 in sylvia/tests/reply.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/reply.rs#L43-L44

Added lines #L43 - L44 were not covered by tests
}
}
25 changes: 25 additions & 0 deletions sylvia/tests/ui/attributes/msg/invalid_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![allow(unused_imports)]
use sylvia::contract;
use sylvia::cw_std::{Reply, Response, StdResult};
use sylvia::types::{InstantiateCtx, ReplyCtx};

pub struct Contract;

#[contract]
impl Contract {
pub fn new() -> Self {
Self
}

#[sv::msg(instantiate)]
pub fn instantiate(&self, _ctx: InstantiateCtx) -> StdResult<Response> {
Ok(Response::new())
}

#[sv::msg(reply, unknown_parameter)]
fn reply(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response> {
Ok(Response::new())
}
}

fn main() {}
5 changes: 5 additions & 0 deletions sylvia/tests/ui/attributes/msg/invalid_params.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Invalid argument type, expected `resp`, `handlers`, `reply_on` or no argument.
--> tests/ui/attributes/msg/invalid_params.rs:19:22
|
19 | #[sv::msg(reply, unknown_parameter)]
| ^^^^^^^^^^^^^^^^^
Loading