Skip to content

Commit

Permalink
chore: Update after review
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird committed Jun 27, 2024
1 parent ff9fa7a commit 1ffb5d2
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 110 deletions.
38 changes: 10 additions & 28 deletions sylvia-derive/src/parser/attributes/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use proc_macro2::{Span, TokenStream};
use proc_macro_error::emit_error;
use syn::parse::{Error, Parse, ParseStream, Parser};
use syn::spanned::Spanned;
use syn::{Attribute, Ident, Result, Token};
use syn::{Ident, MetaList, Result, Token};

use super::MsgType;

Expand All @@ -13,28 +13,11 @@ pub struct VariantAttrForwarding {
}

impl VariantAttrForwarding {
pub fn new(attr: &Attribute) -> Result<Self> {
attr.meta
.require_list()
.and_then(|meta| VariantAttrForwarding::parse.parse2(meta.tokens.clone()))
.map(|mut variant| {
variant.span = attr.span();
variant
})
.map_err(|err| {
emit_error!(attr.span(), err);
err
})
}
}

impl Parse for VariantAttrForwarding {
fn parse(input: ParseStream) -> Result<Self> {
let attrs = input.parse()?;
Ok(Self {
attrs,
span: input.span(),
})
pub fn new(attr: &MetaList) -> Self {
VariantAttrForwarding {
attrs: attr.tokens.clone(),
span: attr.span(),
}
}
}

Expand All @@ -45,12 +28,11 @@ pub struct MsgAttrForwarding {
}

impl MsgAttrForwarding {
pub fn new(attr: &Attribute) -> Result<Self> {
attr.meta
.require_list()
.and_then(|meta| MsgAttrForwarding::parse.parse2(meta.tokens.clone()))
pub fn new(attr: &MetaList) -> Result<Self> {
MsgAttrForwarding::parse
.parse2(attr.tokens.clone())
.map_err(|err| {
emit_error!(attr.span(), err);
emit_error!(err.span(), err);
err

Check warning on line 36 in sylvia-derive/src/parser/attributes/attr.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/attr.rs#L35-L36

Added lines #L35 - L36 were not covered by tests
})
}
Expand Down
36 changes: 17 additions & 19 deletions sylvia-derive/src/parser/attributes/custom.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use proc_macro_error::emit_error;
use syn::parse::{Parse, ParseStream, Parser};
use syn::spanned::Spanned;
use syn::{parse_quote, Attribute, Ident, Result, Token, Type};
use syn::{parse_quote, Error, Ident, MetaList, Result, Token, Type};

use crate::crate_module;

Expand All @@ -12,14 +11,11 @@ pub struct Custom {
}

impl Custom {
pub fn new(attr: &Attribute) -> Result<Self> {
attr.meta
.require_list()
.and_then(|meta| Custom::parse.parse2(meta.tokens.clone()))
.map_err(|err| {
emit_error!(attr.span(), err);
err
})
pub fn new(attr: &MetaList) -> Result<Self> {
Custom::parse.parse2(attr.tokens.clone()).map_err(|err| {
emit_error!(err.span(), err);
err

Check warning on line 17 in sylvia-derive/src/parser/attributes/custom.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/custom.rs#L16-L17

Added lines #L16 - L17 were not covered by tests
})
}

pub fn msg_or_default(&self) -> Type {
Expand All @@ -43,15 +39,17 @@ impl Parse for Custom {
while !input.is_empty() {
let ty: Ident = input.parse()?;
let _: Token![=] = input.parse()?;
if ty == "msg" {
custom.msg = Some(input.parse()?)
} else if ty == "query" {
custom.query = Some(input.parse()?)
} else {
emit_error!(ty.span(), "Invalid custom type.";
note = ty.span() => "Expected `#[sv::custom(msg=SomeMsg, query=SomeQuery)]`"
);
};
match ty.to_string().as_str() {
"msg" => custom.msg = Some(input.parse()?),
"query" => custom.query = Some(input.parse()?),
_ => {
return Err(Error::new(
ty.span(),
"Invalid custom type.\n
= note: Expected `#[sv::custom(msg=SomeMsg, query=SomeQuery)]`.\n",

Check warning on line 49 in sylvia-derive/src/parser/attributes/custom.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/custom.rs#L46-L49

Added lines #L46 - L49 were not covered by tests
))
}
}
if !input.peek(Token![,]) {
break;
}
Expand Down
12 changes: 5 additions & 7 deletions sylvia-derive/src/parser/attributes/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use proc_macro_error::emit_error;
use syn::parse::{Parse, ParseStream, Parser};
use syn::spanned::Spanned;
use syn::{parse_quote, Attribute, Result, Type};
use syn::{parse_quote, MetaList, Result, Type};

use crate::crate_module;

Expand All @@ -20,12 +19,11 @@ impl Default for ContractErrorAttr {
}

impl ContractErrorAttr {
pub fn new(attr: &Attribute) -> Result<Self> {
attr.meta
.require_list()
.and_then(|meta| ContractErrorAttr::parse.parse2(meta.tokens.clone()))
pub fn new(attr: &MetaList) -> Result<Self> {
ContractErrorAttr::parse
.parse2(attr.tokens.clone())
.map_err(|err| {
emit_error!(attr.span(), err);
emit_error!(err.span(), err);
err

Check warning on line 27 in sylvia-derive/src/parser/attributes/error.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/error.rs#L26-L27

Added lines #L26 - L27 were not covered by tests
})
}
Expand Down
30 changes: 16 additions & 14 deletions sylvia-derive/src/parser/attributes/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use syn::fold::Fold;
use syn::parse::{Parse, ParseStream, Parser};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{parenthesized, Attribute, GenericArgument, Ident, Path, Result, Token};
use syn::{parenthesized, Error, GenericArgument, Ident, MetaList, Path, Result, Token};

use proc_macro_error::emit_error;

Expand All @@ -20,12 +20,11 @@ pub struct ContractMessageAttr {
}

impl ContractMessageAttr {
pub fn new(attr: &Attribute) -> Result<Self> {
attr.meta
.require_list()
.and_then(|meta| ContractMessageAttr::parse.parse2(meta.tokens.clone()))
pub fn new(attr: &MetaList) -> Result<Self> {
ContractMessageAttr::parse
.parse2(attr.tokens.clone())
.map_err(|err| {
emit_error!(attr.span(), err);
emit_error!(err.span(), err);
err
})
}
Expand Down Expand Up @@ -61,10 +60,13 @@ fn interface_has_custom(content: ParseStream) -> Result<Customs> {
match custom.get_ident() {
Some(ident) if ident == "msg" => customs.has_msg = true,
Some(ident) if ident == "query" => customs.has_query = true,
_ => emit_error!(custom.span(),
"Invalid custom attribute, expected one or both of: [`msg`, `query`]";
note = "Expected attribute to be in form `#[sv::messages(interface: custom(msg, query))]`."
),
_ => {
return Err(Error::new(
custom.span(),
"Invalid custom attribute, expected one or both of: [`msg`, `query`]\n
= note: Expected attribute to be in form `#[sv::messages(interface: custom(msg, query))]`.\n",

Check warning on line 67 in sylvia-derive/src/parser/attributes/messages.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/messages.rs#L64-L67

Added lines #L64 - L67 were not covered by tests
))
}
}
if !custom_content.peek(Token![,]) {
break;
Expand Down Expand Up @@ -103,10 +105,10 @@ impl Parse for ContractMessageAttr {
};
let customs = interface_has_custom(input)?;
if !input.is_empty() {
emit_error!(input.span(),
"Unexpected tokens inside `sv::messages` attribtue.";
note = "Maximal supported form of attribute: `#[sv::messages(interface::path as InterfaceName: custom(msg, query))]`."
)
return Err(Error::new(input.span(),
"Unexpected tokens inside `sv::messages` attribtue.\n
= note: Maximal supported form of attribute: `#[sv::messages(interface::path as InterfaceName: custom(msg, query))]`.\n"

Check warning on line 110 in sylvia-derive/src/parser/attributes/messages.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/messages.rs#L108-L110

Added lines #L108 - L110 were not covered by tests
));
}
Ok(Self {
module,
Expand Down
15 changes: 8 additions & 7 deletions sylvia-derive/src/parser/attributes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro_error::emit_error;
use syn::spanned::Spanned;
use syn::{Attribute, PathSegment};
use syn::{Attribute, MetaList, PathSegment};

pub mod attr;
pub mod custom;
Expand Down Expand Up @@ -72,7 +72,9 @@ impl ParsedSylviaAttributes {
let mut result = Self::default();
for attr in attrs {
let sylvia_attr = SylviaAttribute::new(attr);
if let Some(sylvia_attr) = sylvia_attr {
let attr_content = attr.meta.require_list();

if let (Some(sylvia_attr), Ok(attr)) = (sylvia_attr, &attr_content) {
result.match_attribute(&sylvia_attr, attr);
}
}
Expand All @@ -85,7 +87,7 @@ impl ParsedSylviaAttributes {
);
} else if let Some(MsgAttr::Migrate) = result.msg_attr {
emit_error!(
attr.span, "The attribute `sv::attr` is not supported for `migate`";
attr.span, "The attribute `sv::attr` is not supported for `migrate`";
note = "Message `migrate` is a structure, use `#[sv::msg_attr] instead`";

Check warning on line 91 in sylvia-derive/src/parser/attributes/mod.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/mod.rs#L89-L91

Added lines #L89 - L91 were not covered by tests
);
}
Expand All @@ -94,7 +96,7 @@ impl ParsedSylviaAttributes {
result
}

fn match_attribute(&mut self, attribute_type: &SylviaAttribute, attr: &Attribute) {
fn match_attribute(&mut self, attribute_type: &SylviaAttribute, attr: &MetaList) {
match attribute_type {
SylviaAttribute::Custom => {
if self.custom_attr.is_none() {
Expand Down Expand Up @@ -146,9 +148,8 @@ impl ParsedSylviaAttributes {
}
}
SylviaAttribute::VariantAttrs => {
if let Ok(variant_attrs) = VariantAttrForwarding::new(attr) {
self.variant_attrs_forward.push(variant_attrs);
}
self.variant_attrs_forward
.push(VariantAttrForwarding::new(attr));
}
SylviaAttribute::MsgAttrs => {
if let Ok(message_attrs) = MsgAttrForwarding::new(attr) {
Expand Down
19 changes: 7 additions & 12 deletions sylvia-derive/src/parser/attributes/msg.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use syn::parse::{Error, Parse, ParseStream, Parser};
use syn::spanned::Spanned;
use syn::{Attribute, Ident, Result, Token};

use proc_macro_error::emit_error;
use syn::parse::{Error, Parse, ParseStream, Parser};
use syn::{Ident, MetaList, Result, Token};

/// Type of message to be generated
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
Expand Down Expand Up @@ -56,14 +54,11 @@ pub enum MsgAttr {
}

impl MsgAttr {
pub fn new(attr: &Attribute) -> Result<Self> {
attr.meta
.require_list()
.and_then(|meta| MsgAttr::parse.parse2(meta.tokens.clone()))
.map_err(|err| {
emit_error!(attr.span(), err);
err
})
pub fn new(attr: &MetaList) -> Result<Self> {
MsgAttr::parse.parse2(attr.tokens.clone()).map_err(|err| {
emit_error!(err.span(), err);
err

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 Down
12 changes: 5 additions & 7 deletions sylvia-derive/src/parser/attributes/override_entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use proc_macro2::TokenStream;
use proc_macro_error::emit_error;
use quote::quote;
use syn::parse::{Error, Parse, ParseStream, Parser};
use syn::spanned::Spanned;
use syn::{parenthesized, Attribute, Ident, Path, Result, Token, Type};
use syn::{parenthesized, Ident, MetaList, Path, Result, Token, Type};

use crate::crate_module;
use crate::parser::MsgType;
Expand All @@ -16,12 +15,11 @@ pub struct OverrideEntryPoint {
}

impl OverrideEntryPoint {
pub fn new(attr: &Attribute) -> Result<Self> {
attr.meta
.require_list()
.and_then(|meta| OverrideEntryPoint::parse.parse2(meta.tokens.clone()))
pub fn new(attr: &MetaList) -> Result<Self> {
OverrideEntryPoint::parse
.parse2(attr.tokens.clone())
.map_err(|err| {
emit_error!(attr.span(), err);
emit_error!(err.span(), err);

Check warning on line 22 in sylvia-derive/src/parser/attributes/override_entry_point.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/override_entry_point.rs#L22

Added line #L22 was not covered by tests
err
})
}
Expand Down
28 changes: 28 additions & 0 deletions sylvia/tests/ui/attributes/attr_forwarding/invalid_sv_attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![allow(unused_imports)]
use sylvia::cw_std::{Response, StdResult};
use sylvia::types::{ExecCtx, InstantiateCtx};

pub struct Contract;

#[sylvia::contract]
#[sv::msg_attr(random_msg, PartialOrd)]
#[sv::msg_attr(exec PartialOrd)]
#[sv::msg_attr(PartialOrd)]
#[sv::msg_attr(exec,)]
impl Contract {
pub const fn new() -> Self {
Self
}

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

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

fn main() {}
21 changes: 21 additions & 0 deletions sylvia/tests/ui/attributes/attr_forwarding/invalid_sv_attr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error: Invalid message type, expected one of: `exec`, `query`, `instantiate`, `migrate`, `reply` or `sudo`.
--> tests/ui/attributes/attr_forwarding/invalid_sv_attr.rs:7:1
|
7 | #[sylvia::contract]
| ^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the attribute macro `sylvia::contract` (in Nightly builds, run with -Z macro-backtrace for more info)

error: Expected attribute of the form: `#[sv::msg_attr(msg_type, attribute_to_forward)]`
--> tests/ui/attributes/attr_forwarding/invalid_sv_attr.rs:9:21
|
9 | #[sv::msg_attr(exec PartialOrd)]
| ^^^^^^^^^^

error: Expected attribute of the form: `#[sv::msg_attr(msg_type, attribute_to_forward)]`
--> tests/ui/attributes/attr_forwarding/invalid_sv_attr.rs:7:1
|
7 | #[sylvia::contract]
| ^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the attribute macro `sylvia::contract` (in Nightly builds, run with -Z macro-backtrace for more info)
9 changes: 1 addition & 8 deletions sylvia/tests/ui/attributes/custom/invalid_custom.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
error: Invalid custom type.

= note: Expected `#[sv::custom(msg=SomeMsg, query=SomeQuery)]`

= note: Expected `#[sv::custom(msg=SomeMsg, query=SomeQuery)]`.
--> tests/ui/attributes/custom/invalid_custom.rs:12:14
|
12 | #[sv::custom(mgs=MyMsg)]
| ^^^

error: unexpected token
--> tests/ui/attributes/custom/invalid_custom.rs:12:1
|
12 | #[sv::custom(mgs=MyMsg)]
| ^
1 change: 0 additions & 1 deletion sylvia/tests/ui/attributes/messages/invalid_custom.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
error: Invalid custom attribute, expected one or both of: [`msg`, `query`]

= note: Expected attribute to be in form `#[sv::messages(interface: custom(msg, query))]`.

--> tests/ui/attributes/messages/invalid_custom.rs:21:34
|
21 | #[sv::messages(interface: custom(wrong))]
Expand Down
7 changes: 0 additions & 7 deletions sylvia/tests/ui/attributes/messages/unexpected_token.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
error: Unexpected tokens inside `sv::messages` attribtue.

= note: Maximal supported form of attribute: `#[sv::messages(interface::path as InterfaceName: custom(msg, query))]`.

--> tests/ui/attributes/messages/unexpected_token.rs:24:25
|
24 | #[sv::messages(interface(Empty))]
| ^

error: unexpected token
--> tests/ui/attributes/messages/unexpected_token.rs:24:1
|
24 | #[sv::messages(interface(Empty))]
| ^

0 comments on commit 1ffb5d2

Please sign in to comment.