Skip to content

Commit

Permalink
feat: Allow specifying concrete customs in entry_points
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Dec 21, 2023
1 parent c245395 commit 4b65245
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
5 changes: 4 additions & 1 deletion examples/contracts/generics_forwarded/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use sylvia::types::{
};
use sylvia::{contract, schemars};

#[cfg(not(feature = "library"))]
use sylvia::types::SvCustomQuery;

pub struct GenericsForwardedContract<
InstantiateT,
Exec1T,
Expand Down Expand Up @@ -35,7 +38,7 @@ pub struct GenericsForwardedContract<
)>,
}

// TODO: Add entry points call.
#[cfg_attr(not(feature = "library"), sylvia::entry_points(generics<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg, SvCustomMsg, SvCustomQuery, String>, custom(msg=SvCustomMsg, query=SvCustomQuery)))]
#[contract]
#[messages(generic<Exec1T, Exec2T, Exec3T, Query1T, Query2T, Query3T, SvCustomMsg> as Generic: custom(msg, query))]
#[messages(cw1 as Cw1: custom(msg, query))]
Expand Down
9 changes: 7 additions & 2 deletions sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1651,11 +1651,11 @@ pub struct EntryPoints<'a> {
override_entry_points: OverrideEntryPoints,
generics: Vec<&'a GenericParam>,
where_clause: &'a Option<WhereClause>,
attrs: EntryPointArgs,
attrs: EntryPointArgs<'a>,
}

impl<'a> EntryPoints<'a> {
pub fn new(source: &'a ItemImpl, attrs: EntryPointArgs) -> Self {
pub fn new(source: &'a ItemImpl, attrs: EntryPointArgs<'a>) -> Self {
let sylvia = crate_module();
let name = StripGenerics.fold_type(*source.self_ty.clone());
let override_entry_points = OverrideEntryPoints::new(&source.attrs);
Expand Down Expand Up @@ -1704,6 +1704,11 @@ impl<'a> EntryPoints<'a> {
} = self;
let sylvia = crate_module();

let custom = match &attrs.custom {
Some(custom) => custom,
None => custom,
};

let custom_msg = custom.msg_or_default();
let custom_query = custom.query_or_default();

Expand Down
38 changes: 30 additions & 8 deletions sylvia-derive/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,49 @@ impl Parse for ContractArgs {
}

/// Parsed arguments for `entry_points` macro
pub struct EntryPointArgs {
#[derive(Default)]
pub struct EntryPointArgs<'a> {
/// Types used in place of contracts generics.
pub generics: Option<Punctuated<GenericArgument, Token![,]>>,
/// Custom msg/query used in place of contracts generic ones.
pub custom: Option<Custom<'a>>,
}

impl Parse for EntryPointArgs {
impl<'a> Parse for EntryPointArgs<'a> {
fn parse(input: ParseStream) -> Result<Self> {
let mut entry_points_args = Self::default();
if input.is_empty() {
return Ok(Self { generics: None });
return Ok(entry_points_args);
}

let path: Path = input.parse()?;
let generics: Path = input.parse()?;
match generics.segments.last() {
Some(segment) if segment.ident == "generics" => {
entry_points_args.generics = Some(extract_generics_from_path(&generics))
}
_ => return Err(Error::new(generics.span(), "Expected `generics`.")),
};

let generics = match path.segments.last() {
Some(segment) if segment.ident == "generics" => Some(extract_generics_from_path(&path)),
_ => return Err(Error::new(path.span(), "Expected `generics`")),
let comma: Option<Token![,]> = input.parse().ok();
if comma.is_none() {
return Ok(entry_points_args);
}

let custom: Option<Path> = input.parse().ok();
match custom {
Some(custom)
if custom.get_ident().map(|custom| custom.to_string())
== Some("custom".to_owned()) =>
{
entry_points_args.custom = Some(Custom::parse.parse2(input.parse()?)?);
}
Some(attr) => return Err(Error::new(attr.span(), "Expected `custom`.")),
_ => (),
};

let _: Nothing = input.parse()?;

Ok(Self { generics })
Ok(entry_points_args)
}
}

Expand Down

0 comments on commit 4b65245

Please sign in to comment.