Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update signature of use_prepared_state/use_transitive_state #3376

Merged
merged 9 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/simple_ssr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn fetch_uuid() -> Uuid {

#[function_component]
fn Content() -> HtmlResult {
let uuid = use_prepared_state!(async move |_| -> Uuid { fetch_uuid().await }, ())?.unwrap();
let uuid = use_prepared_state!((), async move |_| -> Uuid { fetch_uuid().await })?.unwrap();

Ok(html! {
<div>{"Random UUID: "}{uuid}</div>
Expand Down
24 changes: 12 additions & 12 deletions packages/yew-macro/src/use_prepared_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ pub struct PreparedState {

impl Parse for PreparedState {
fn parse(input: ParseStream) -> syn::Result<Self> {
// Reads a closure.
let expr: Expr = input.parse()?;

let closure = match expr {
Expr::Closure(m) => m,
other => return Err(syn::Error::new_spanned(other, "expected closure")),
};
// Reads the deps.
let deps = input.parse()?;

input.parse::<Token![,]>().map_err(|e| {
syn::Error::new(
Expand All @@ -27,6 +22,14 @@ impl Parse for PreparedState {
)
})?;

// Reads a closure.
let expr: Expr = input.parse()?;

let closure = match expr {
Expr::Closure(m) => m,
other => return Err(syn::Error::new_spanned(other, "expected closure")),
};

let return_type = match &closure.output {
ReturnType::Default => {
return Err(syn::Error::new_spanned(
Expand All @@ -38,9 +41,6 @@ impl Parse for PreparedState {
ReturnType::Type(_rarrow, ty) => *ty.to_owned(),
};

// Reads the deps.
let deps = input.parse()?;

if !input.is_empty() {
let maybe_trailing_comma = input.lookahead1();

Expand Down Expand Up @@ -107,10 +107,10 @@ impl PreparedState {

match &self.closure.asyncness {
Some(_) => quote! {
::yew::functional::use_prepared_state_with_suspension::<#rt, _, _, _>(#closure, #deps)
::yew::functional::use_prepared_state_with_suspension::<#rt, _, _, _>(#deps, #closure)
},
None => quote! {
::yew::functional::use_prepared_state::<#rt, _, _>(#closure, #deps)
::yew::functional::use_prepared_state::<#rt, _, _>(#deps, #closure)
},
}
}
Expand Down
22 changes: 11 additions & 11 deletions packages/yew-macro/src/use_transitive_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ pub struct TransitiveState {

impl Parse for TransitiveState {
fn parse(input: ParseStream) -> syn::Result<Self> {
// Reads a closure.
let expr: Expr = input.parse()?;

let closure = match expr {
Expr::Closure(m) => m,
other => return Err(syn::Error::new_spanned(other, "expected closure")),
};
// Reads the deps.
let deps = input.parse()?;

input.parse::<Token![,]>().map_err(|e| {
syn::Error::new(
Expand All @@ -27,6 +22,14 @@ impl Parse for TransitiveState {
)
})?;

// Reads a closure.
let expr: Expr = input.parse()?;

let closure = match expr {
Expr::Closure(m) => m,
other => return Err(syn::Error::new_spanned(other, "expected closure")),
};

let return_type = match &closure.output {
ReturnType::Default => {
return Err(syn::Error::new_spanned(
Expand All @@ -38,9 +41,6 @@ impl Parse for TransitiveState {
ReturnType::Type(_rarrow, ty) => *ty.to_owned(),
};

// Reads the deps.
let deps = input.parse()?;

if !input.is_empty() {
let maybe_trailing_comma = input.lookahead1();

Expand All @@ -64,7 +64,7 @@ impl TransitiveState {
let closure = &self.closure;

quote! {
::yew::functional::use_transitive_state::<#rt, _, _>(#closure, #deps)
::yew::functional::use_transitive_state::<#rt, _, _>(#deps, #closure)
}
}

Expand Down
10 changes: 9 additions & 1 deletion packages/yew-macro/tests/hook_macro/use_prepared_state-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@ use yew_macro::{use_prepared_state_with_closure, use_prepared_state_without_clos
fn Comp() -> HtmlResult {
use_prepared_state_with_closure!(123)?;

use_prepared_state_with_closure!(|_| { todo!() }, 123)?;
use_prepared_state_with_closure!(123, |_| { todo!() })?;

use_prepared_state_with_closure!(|_| -> u32 { todo!() })?;

use_prepared_state_with_closure!(|_| -> u32 { todo!() }, 123)?;

use_prepared_state_with_closure!(async |_| -> u32 { todo!() })?;

use_prepared_state_with_closure!(|_| { todo!() }, 123)?;

Ok(Html::default())
}

#[function_component]
fn Comp2() -> HtmlResult {
use_prepared_state_without_closure!(123)?;

use_prepared_state_without_closure!(123, |_| { todo!() })?;

use_prepared_state_without_closure!(|_| { todo!() }, 123)?;

use_prepared_state_without_closure!(|_| -> u32 { todo!() })?;

use_prepared_state_without_closure!(|_| -> u32 { todo!() }, 123)?;

use_prepared_state_without_closure!(async |_| -> u32 { todo!() })?;

Ok(Html::default())
Expand Down
64 changes: 46 additions & 18 deletions packages/yew-macro/tests/hook_macro/use_prepared_state-fail.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
error: expected closure
--> tests/hook_macro/use_prepared_state-fail.rs:6:38
error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_prepared_state-fail.rs:6:5
|
6 | use_prepared_state_with_closure!(123)?;
| ^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `use_prepared_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info)

error: You must specify a return type for this closure. This is used when the closure is omitted from the client side rendering bundle.
--> tests/hook_macro/use_prepared_state-fail.rs:8:38
--> tests/hook_macro/use_prepared_state-fail.rs:8:43
|
8 | use_prepared_state_with_closure!(|_| { todo!() }, 123)?;
| ^^^^^^^^^^^^^^^
8 | use_prepared_state_with_closure!(123, |_| { todo!() })?;
| ^^^^^^^^^^^^^^^

error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_prepared_state-fail.rs:10:5
Expand All @@ -18,38 +20,64 @@ error: this hook takes 2 arguments but 1 argument was supplied
|
= 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 closure
--> tests/hook_macro/use_prepared_state-fail.rs:12:62
|
12 | use_prepared_state_with_closure!(|_| -> u32 { todo!() }, 123)?;
| ^^^

error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_prepared_state-fail.rs:12:5
--> tests/hook_macro/use_prepared_state-fail.rs:14:5
|
12 | use_prepared_state_with_closure!(async |_| -> u32 { todo!() })?;
14 | 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 closure
--> tests/hook_macro/use_prepared_state-fail.rs:19:41
--> tests/hook_macro/use_prepared_state-fail.rs:16:55
|
19 | use_prepared_state_without_closure!(123)?;
| ^^^
16 | use_prepared_state_with_closure!(|_| { todo!() }, 123)?;
| ^^^

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!(123)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `use_prepared_state_without_closure` (in Nightly builds, run with -Z macro-backtrace for more info)

error: You must specify a return type for this closure. This is used when the closure is omitted from the client side rendering bundle.
--> tests/hook_macro/use_prepared_state-fail.rs:21:41
--> tests/hook_macro/use_prepared_state-fail.rs:25:46
|
25 | use_prepared_state_without_closure!(123, |_| { todo!() })?;
| ^^^^^^^^^^^^^^^

error: expected closure
--> tests/hook_macro/use_prepared_state-fail.rs:27:58
|
21 | use_prepared_state_without_closure!(|_| { todo!() }, 123)?;
| ^^^^^^^^^^^^^^^
27 | use_prepared_state_without_closure!(|_| { todo!() }, 123)?;
| ^^^

error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_prepared_state-fail.rs:23:5
--> tests/hook_macro/use_prepared_state-fail.rs:29:5
|
23 | use_prepared_state_without_closure!(|_| -> u32 { todo!() })?;
29 | 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 closure
--> tests/hook_macro/use_prepared_state-fail.rs:31:65
|
31 | use_prepared_state_without_closure!(|_| -> u32 { todo!() }, 123)?;
| ^^^

error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_prepared_state-fail.rs:25:5
--> tests/hook_macro/use_prepared_state-fail.rs:33:5
|
25 | use_prepared_state_without_closure!(async |_| -> u32 { todo!() })?;
33 | use_prepared_state_without_closure!(async |_| -> 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)
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ fn Comp() -> HtmlResult {

use_transitive_state_with_closure!(|_| { todo!() }, 123)?;

use_transitive_state_with_closure!(123, |_| { todo!() })?;

use_transitive_state_with_closure!(|_| -> u32 { todo!() })?;

use_transitive_state_with_closure!(|_| -> u32 { todo!() }, 123)?;

Ok(Html::default())
}

Expand All @@ -18,8 +22,12 @@ fn Comp2() -> HtmlResult {

use_transitive_state_without_closure!(|_| { todo!() }, 123)?;

use_transitive_state_without_closure!(123, |_| { todo!() })?;

use_transitive_state_without_closure!(|_| -> u32 { todo!() })?;

use_transitive_state_without_closure!(|_| -> u32 { todo!() }, 123)?;

Ok(Html::default())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,67 @@
error: expected closure
--> tests/hook_macro/use_transitive_state-fail.rs:6:40
error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_transitive_state-fail.rs:6:5
|
6 | use_transitive_state_with_closure!(123)?;
| ^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `use_transitive_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info)

error: You must specify a return type for this closure. This is used when the closure is omitted from the client side rendering bundle.
--> tests/hook_macro/use_transitive_state-fail.rs:8:40
error: expected closure
--> tests/hook_macro/use_transitive_state-fail.rs:8:57
|
8 | use_transitive_state_with_closure!(|_| { todo!() }, 123)?;
| ^^^^^^^^^^^^^^^
| ^^^

error: You must specify a return type for this closure. This is used when the closure is omitted from the client side rendering bundle.
--> tests/hook_macro/use_transitive_state-fail.rs:10:45
|
10 | use_transitive_state_with_closure!(123, |_| { todo!() })?;
| ^^^^^^^^^^^^^^^

error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_transitive_state-fail.rs:10:5
--> tests/hook_macro/use_transitive_state-fail.rs:12:5
|
10 | use_transitive_state_with_closure!(|_| -> u32 { todo!() })?;
12 | 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 closure
--> tests/hook_macro/use_transitive_state-fail.rs:17:43
--> tests/hook_macro/use_transitive_state-fail.rs:14:64
|
17 | use_transitive_state_without_closure!(123)?;
| ^^^
14 | use_transitive_state_with_closure!(|_| -> u32 { todo!() }, 123)?;
| ^^^

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!(123)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `use_transitive_state_without_closure` (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected closure
--> tests/hook_macro/use_transitive_state-fail.rs:23:60
|
23 | use_transitive_state_without_closure!(|_| { todo!() }, 123)?;
| ^^^

error: You must specify a return type for this closure. This is used when the closure is omitted from the client side rendering bundle.
--> tests/hook_macro/use_transitive_state-fail.rs:19:43
--> tests/hook_macro/use_transitive_state-fail.rs:25:48
|
19 | use_transitive_state_without_closure!(|_| { todo!() }, 123)?;
| ^^^^^^^^^^^^^^^
25 | use_transitive_state_without_closure!(123, |_| { todo!() })?;
| ^^^^^^^^^^^^^^^

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

error: expected closure
--> tests/hook_macro/use_transitive_state-fail.rs:29:67
|
29 | use_transitive_state_without_closure!(|_| -> u32 { todo!() }, 123)?;
| ^^^
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::suspense::SuspensionResult;

#[doc(hidden)]
pub fn use_prepared_state<T, D, F>(
f: F,
deps: D,
f: F,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand All @@ -41,7 +41,7 @@ where

fn run(self, ctx: &mut HookContext) -> Self::Output {
match ctx.creation_mode {
RenderMode::Ssr => feat_ssr::use_prepared_state(self.f, self.deps).run(ctx),
RenderMode::Ssr => feat_ssr::use_prepared_state(self.deps, self.f).run(ctx),
_ => feat_hydration::use_prepared_state(self.deps).run(ctx),
}
}
Expand All @@ -52,8 +52,8 @@ where

#[doc(hidden)]
pub fn use_prepared_state_with_suspension<T, D, F, U>(
f: F,
deps: D,
f: F,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand Down Expand Up @@ -84,7 +84,7 @@ where
fn run(self, ctx: &mut HookContext) -> Self::Output {
match ctx.creation_mode {
RenderMode::Ssr => {
feat_ssr::use_prepared_state_with_suspension(self.f, self.deps).run(ctx)
feat_ssr::use_prepared_state_with_suspension(self.deps, self.f).run(ctx)
}
_ => feat_hydration::use_prepared_state(self.deps).run(ctx),
}
Expand Down
Loading
Loading