Skip to content

Commit

Permalink
feat: Make as TraitName optional in `#[message(some::module::trait_…
Browse files Browse the repository at this point in the history
…name as TraitName)]` attribute
  • Loading branch information
kulikthebird committed Jan 18, 2024
1 parent 511d88b commit 277162e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
25 changes: 20 additions & 5 deletions sylvia-derive/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use convert_case::{Case, Casing};
use proc_macro2::{Punct, TokenStream};
use proc_macro_error::emit_error;
use quote::quote;
Expand Down Expand Up @@ -357,18 +358,32 @@ impl Parse for ContractMessageAttr {
let generics = extract_generics_from_path(&module);
let module = StripGenerics.fold_path(module);

let _: Token![as] = content.parse()?;
let variant = content.parse()?;

let variant = if content.parse::<Token![as]>().is_ok() {
content.parse()?
} else {
syn::Ident::new(
&module
.segments
.last()
.ok_or_else(|| {
let err =
"#`[message(..)]` attribute without `as TraitName` needs to provide a path";
emit_error!(module.span(), err);
syn::Error::new(module.span(), err)
})?
.ident
.to_string()
.to_case(Case::UpperCamel),
module.span()
)
};
let customs = interface_has_custom(&content)?;

if !content.is_empty() {
return Err(Error::new(
content.span(),
"Unexpected token on the end of `message` attribtue",
));
}

Ok(Self {
module,
variant,
Expand Down
17 changes: 10 additions & 7 deletions sylvia/tests/custom_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ mod impl_some_interface {
use crate::{MyMsg, SomeResponse};

#[contract(module=crate)]
#[messages(crate::some_interface as SomeInterface)]
#[messages(crate::some_interface)]
#[sv::custom(msg=MyMsg)]
impl SomeInterface for crate::MyContract {
type Error = StdError;
Expand Down Expand Up @@ -93,7 +93,7 @@ mod impl_interface {
use sylvia::types::ExecCtx;

#[contract(module=crate)]
#[messages(crate::interface as Interface)]
#[messages(crate::interface)]
#[sv::custom(msg=MyMsg)]
impl Interface for crate::MyContract {
type Error = StdError;
Expand Down Expand Up @@ -127,7 +127,7 @@ mod impl_other_interface {
use sylvia::types::ExecCtx;

#[contract(module=crate)]
#[messages(crate::other_interface as OtherInterface)]
#[messages(crate::other_interface)]
#[sv::custom(msg=crate::MyMsg)]
impl OtherInterface for crate::MyContract {
type Error = StdError;
Expand Down Expand Up @@ -162,7 +162,7 @@ mod impl_associated_interface {
use sylvia::types::ExecCtx;

#[contract(module=crate)]
#[messages(crate::associated_interface as AssociatedInterface)]
#[messages(crate::associated_interface)]
#[sv::custom(msg=MyMsg)]
impl AssociatedInterface for crate::MyContract {
type Error = StdError;
Expand All @@ -177,9 +177,12 @@ mod impl_associated_interface {

#[contract]
#[messages(some_interface as SomeInterface)]
#[messages(other_interface as OtherInterface: custom(msg))]
#[messages(associated_interface as AssociatedInterface)]
#[messages(interface as Interface)]
// When the last part of the path in #[message] attribute
// coresponds to the name of the module then
// `as InterfaceName` can be omitted:
#[messages(other_interface: custom(msg))]
#[messages(associated_interface)]
#[messages(interface)]
#[sv::custom(msg=MyMsg)]
impl MyContract {
#[allow(clippy::new_without_default)]
Expand Down
4 changes: 2 additions & 2 deletions sylvia/tests/custom_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod impl_interface {
use crate::{MyQuery, OtherQuery, SomeResponse};

#[contract(module=crate)]
#[messages(crate::interface as Interface)]
#[messages(crate::interface)]
#[sv::custom(query=MyQuery)]
impl crate::interface::Interface for crate::MyContract {
type Error = StdError;
Expand Down Expand Up @@ -97,7 +97,7 @@ mod impl_some_interface {
use crate::{MyQuery, SomeResponse};

#[contract(module=crate)]
#[messages(crate::some_interface as SomeInterface)]
#[messages(crate::some_interface)]
#[sv::custom(query=MyQuery)]
impl super::some_interface::SomeInterface for crate::MyContract {
type Error = StdError;
Expand Down

0 comments on commit 277162e

Please sign in to comment.