Skip to content

Commit

Permalink
feat: Assert no attributes used on self and ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Nov 19, 2024
1 parent a7af42b commit adaa241
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions sylvia-derive/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,29 @@ pub fn process_fields<'s, Generic>(
where
Generic: GetPath + PartialEq,
{
// Assert no accidental usage of Sylvia attributes on `self` and `ctx` parameters.
sig.inputs.iter().take(2).for_each(|arg| match arg {
FnArg::Receiver(item) => {
item.attrs.iter().for_each(|attr| {
emit_error!(attr.span(),
"Invalid usage of Sylvia attribute.";
note = "First and second arguments of the method should be `self` and `ctx` respectively.";
note = "Unexpected attribute on `self` parameter."

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

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/mod.rs#L74-L77

Added lines #L74 - L77 were not covered by tests
);
});
}

FnArg::Typed(item) => {
item.attrs.iter().for_each(|attr| {
emit_error!(attr.span(),
"Invalid usage of Sylvia attribute.";
note = "First and second arguments of the method should be `self` and `ctx` respectively.";
note = "Unexpected attribute on `ctx` parameter."

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

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/mod.rs#L84-L87

Added lines #L84 - L87 were not covered by tests
);
});
}
});

sig.inputs
.iter()
.skip(2)
Expand Down
10 changes: 10 additions & 0 deletions sylvia/tests/ui/attributes/payload/invalid_usage.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
error: Invalid usage of Sylvia attribute.

= note: First and second arguments of the method should be `self` and `ctx` respectively.
= note: Unexpected attribute on `ctx` parameter.

--> tests/ui/attributes/payload/invalid_usage.rs:23:25
|
23 | fn reply(&self, #[sv::payload(raw)] _ctx: ReplyCtx) -> StdResult<Response> {
| ^

error: Missing payload parameter.

= note: Expected at least one payload parameter at the end of parameter list.
Expand Down

0 comments on commit adaa241

Please sign in to comment.