Skip to content

Commit

Permalink
Update diagnostic messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
futursolo committed May 23, 2022
1 parent b8267ef commit f01c200
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
16 changes: 12 additions & 4 deletions packages/yew-macro/src/use_prepared_state.rs
Expand Up @@ -13,11 +13,19 @@ pub struct PreparedState {
impl Parse for PreparedState {
fn parse(input: ParseStream) -> syn::Result<Self> {
// Reads a closure.
let closure: ExprClosure = input.parse()?;
let expr: Expr = input.parse()?;

input
.parse::<Token![,]>()
.map_err(|e| syn::Error::new(e.span(), "expected a second argument as dependency"))?;
let closure = match expr {
Expr::Closure(m) => m,
other => return Err(syn::Error::new_spanned(other, "expected closure")),
};

input.parse::<Token![,]>().map_err(|e| {
syn::Error::new(
e.span(),
"this hook takes 2 arguments but 1 argument was supplied",
)
})?;

let return_type = match &closure.output {
ReturnType::Default => {
Expand Down
16 changes: 12 additions & 4 deletions packages/yew-macro/src/use_transitive_state.rs
Expand Up @@ -13,11 +13,19 @@ pub struct TransitiveState {
impl Parse for TransitiveState {
fn parse(input: ParseStream) -> syn::Result<Self> {
// Reads a closure.
let closure: ExprClosure = input.parse()?;
let expr: Expr = input.parse()?;

input
.parse::<Token![,]>()
.map_err(|e| syn::Error::new(e.span(), "expected a second argument as dependency"))?;
let closure = match expr {
Expr::Closure(m) => m,
other => return Err(syn::Error::new_spanned(other, "expected closure")),
};

input.parse::<Token![,]>().map_err(|e| {
syn::Error::new(
e.span(),
"this hook takes 2 arguments but 1 argument was supplied",
)
})?;

let return_type = match &closure.output {
ReturnType::Default => {
Expand Down
@@ -1,4 +1,4 @@
error: expected `|`
error: expected closure
--> tests/hook_macro/use_prepared_state-fail.rs:6:38
|
6 | use_prepared_state_with_closure!(123)?;
Expand All @@ -10,23 +10,23 @@ error: You must specify a return type for this closure. This is used when the cl
8 | use_prepared_state_with_closure!(|_| { todo!() }, 123)?;
| ^^^^^^^^^^^^^^^

error: expected a second argument as dependency
error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_prepared_state-fail.rs:10:5
|
10 | use_prepared_state_with_closure!(|_| -> u32 { todo!() })?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `use_prepared_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected a second argument as dependency
error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_prepared_state-fail.rs:12:5
|
12 | use_prepared_state_with_closure!(async |_| -> u32 { todo!() })?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `use_prepared_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected `|`
error: expected closure
--> tests/hook_macro/use_prepared_state-fail.rs:19:41
|
19 | use_prepared_state_without_closure!(123)?;
Expand All @@ -38,15 +38,15 @@ error: You must specify a return type for this closure. This is used when the cl
21 | use_prepared_state_without_closure!(|_| { todo!() }, 123)?;
| ^^^^^^^^^^^^^^^

error: expected a second argument as dependency
error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_prepared_state-fail.rs:23:5
|
23 | use_prepared_state_without_closure!(|_| -> u32 { todo!() })?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `use_prepared_state_without_closure` (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected a second argument as dependency
error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_prepared_state-fail.rs:25:5
|
25 | use_prepared_state_without_closure!(async |_| -> u32 { todo!() })?;
Expand Down
@@ -1,4 +1,4 @@
error: expected `|`
error: expected closure
--> tests/hook_macro/use_transitive_state-fail.rs:6:40
|
6 | use_transitive_state_with_closure!(123)?;
Expand All @@ -10,15 +10,15 @@ error: You must specify a return type for this closure. This is used when the cl
8 | use_transitive_state_with_closure!(|_| { todo!() }, 123)?;
| ^^^^^^^^^^^^^^^

error: expected a second argument as dependency
error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_transitive_state-fail.rs:10:5
|
10 | use_transitive_state_with_closure!(|_| -> u32 { todo!() })?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `use_transitive_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected `|`
error: expected closure
--> tests/hook_macro/use_transitive_state-fail.rs:17:43
|
17 | use_transitive_state_without_closure!(123)?;
Expand All @@ -30,7 +30,7 @@ error: You must specify a return type for this closure. This is used when the cl
19 | use_transitive_state_without_closure!(|_| { todo!() }, 123)?;
| ^^^^^^^^^^^^^^^

error: expected a second argument as dependency
error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_transitive_state-fail.rs:21:5
|
21 | use_transitive_state_without_closure!(|_| -> u32 { todo!() })?;
Expand Down

0 comments on commit f01c200

Please sign in to comment.