Skip to content

Commit

Permalink
feat: added component name inference to template_rx
Browse files Browse the repository at this point in the history
Can't do this with the old macro until the next breaking change.
  • Loading branch information
arctic-hen7 committed Mar 23, 2022
1 parent 9f3d5a2 commit d1ba2ef
Show file tree
Hide file tree
Showing 29 changed files with 40 additions and 55 deletions.
2 changes: 1 addition & 1 deletion examples/.base/src/templates/index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use perseus::{Html, Template};
use sycamore::prelude::{view, SsrNode, View};

#[perseus::template_rx(IndexPage)]
#[perseus::template_rx]
pub fn index_page() -> View<G> {
view! {
p { "Hello World!" }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/basic/src/templates/about.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use perseus::Template;
use sycamore::prelude::{view, Html, SsrNode, View};

#[perseus::template_rx(AboutPage)]
#[perseus::template_rx]
pub fn about_page() -> View<G> {
view! {
p { "About." }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/basic/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct IndexPageState {
pub greeting: String,
}

#[perseus::template_rx(IndexPage)]
#[perseus::template_rx]
pub fn index_page(state: IndexPageStateRx) -> View<G> {
view! {
p { (state.greeting.get()) }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/freezing_and_thawing/src/templates/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sycamore::prelude::*;

use crate::global_state::AppStateRx;

#[perseus::template_rx(AboutPage)]
#[perseus::template_rx]
pub fn about_page(_: (), global_state: AppStateRx) -> View<G> {
let test = global_state.test;
// This is not part of our data model, we do NOT want the frozen app synchronized as part of our page's state, it should be separate
Expand Down
2 changes: 1 addition & 1 deletion examples/core/freezing_and_thawing/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct IndexProps {
username: String,
}

#[perseus::template_rx(IndexPage)]
#[perseus::template_rx]
pub fn index_page(state: IndexPropsRx, global_state: AppStateRx) -> View<G> {
let username = state.username;
let username_2 = username.clone(); // This is necessary until Sycamore's new reactive primitives are released
Expand Down
2 changes: 1 addition & 1 deletion examples/core/global_state/src/templates/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sycamore::prelude::{view, SsrNode, View};
use crate::global_state::AppStateRx;

// This template needs global state, but doesn't have any state of its own, so the first argument is the unit type `()` (which the macro will detect)
#[perseus::template_rx(AboutPage)]
#[perseus::template_rx]
pub fn about_page(_: (), global_state: AppStateRx) -> View<G> {
let test = global_state.test;
let test_2 = test.clone();
Expand Down
2 changes: 1 addition & 1 deletion examples/core/global_state/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sycamore::prelude::{view, SsrNode, View};
use crate::global_state::AppStateRx;

// This template needs global state, but doesn't have any state of its own, so the first argument is the unit type `()` (which the macro will detect)
#[perseus::template_rx(AboutPage)]
#[perseus::template_rx]
pub fn index_page(_: (), global_state: AppStateRx) -> View<G> {
let test = global_state.test;
let test_2 = test.clone();
Expand Down
2 changes: 1 addition & 1 deletion examples/core/i18n/src/templates/about.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use perseus::{t, Template};
use sycamore::prelude::{view, Html, View};

#[perseus::template_rx(AboutPage)]
#[perseus::template_rx]
pub fn about_page() -> View<G> {
view! {
p { (t!("about")) }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/i18n/src/templates/index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use perseus::{link, t, Template};
use sycamore::prelude::{view, Html, View};

#[perseus::template_rx(IndexPage)]
#[perseus::template_rx]
pub fn index_page() -> View<G> {
let username = "User";
view! {
Expand Down
2 changes: 1 addition & 1 deletion examples/core/i18n/src/templates/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct PostPageState {
content: String,
}

#[perseus::template_rx(PostPage)]
#[perseus::template_rx]
pub fn post_page(props: PostPageStateRx) -> View<G> {
let title = props.title;
let content = props.content;
Expand Down
2 changes: 1 addition & 1 deletion examples/core/idb_freezing/src/templates/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sycamore::prelude::*;

use crate::global_state::AppStateRx;

#[perseus::template_rx(AboutPage)]
#[perseus::template_rx]
pub fn about_page(_: (), global_state: AppStateRx) -> View<G> {
let test = global_state.test;
// This is not part of our data model
Expand Down
2 changes: 1 addition & 1 deletion examples/core/idb_freezing/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct IndexProps {
username: String,
}

#[perseus::template_rx(IndexPage)]
#[perseus::template_rx]
pub fn index_page(state: IndexPropsRx, global_state: AppStateRx) -> View<G> {
let username = state.username;
let username_2 = username.clone(); // This is necessary until Sycamore's new reactive primitives are released
Expand Down
2 changes: 1 addition & 1 deletion examples/core/plugins/src/templates/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use perseus::Template;
use sycamore::prelude::{view, Html, SsrNode, View};

// This page will actually be replaced entirely by a plugin!
#[perseus::template_rx(AboutPage)]
#[perseus::template_rx]
pub fn about_page() -> View<G> {
view! {
p { "About." }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/plugins/src/templates/index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use perseus::{Html, Template};
use sycamore::prelude::{view, SsrNode, View};

#[perseus::template_rx(IndexPage)]
#[perseus::template_rx]
pub fn index_page() -> View<G> {
view! {
p { "Hello World!" }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/router_state/src/templates/about.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use perseus::{Html, Template};
use sycamore::prelude::{view, SsrNode, View};

#[perseus::template_rx(AboutPage)]
#[perseus::template_rx]
pub fn about_page() -> View<G> {
view! {
p { "Hello World!" }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/router_state/src/templates/index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use perseus::{templates::RouterLoadState, Html, Template};
use sycamore::prelude::{cloned, create_memo, view, View};

#[perseus::template_rx(RouterStatePage)]
#[perseus::template_rx]
pub fn router_state_page() -> View<G> {
let load_state = perseus::get_render_ctx!().router.get_load_state();
// This uses Sycamore's `create_memo` to create a state that will update whenever the router state changes
Expand Down
2 changes: 1 addition & 1 deletion examples/core/rx_state/src/templates/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sycamore::view::View;
// WARNING: Accessing the page state store manually as this template does is NOT recommended, and is done for demonstration purposes only! In reality, you should use global state for anything that
// you need to share between pages.

#[perseus::template_rx(AboutPage)]
#[perseus::template_rx]
pub fn about_page() -> View<G> {
// Get the page state store manually
// The index page is just an empty string
Expand Down
2 changes: 1 addition & 1 deletion examples/core/rx_state/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct IndexPageState {
}

// This macro will make our state reactive *and* store it in the page state store, which means it'll be the same even if we go to the about page and come back (as long as we're in the same session)
#[perseus::template_rx(IndexPage)]
#[perseus::template_rx]
pub fn index_page(state: IndexPageStateRx) -> View<G> {
let username = state.username;
let username_2 = username.clone();
Expand Down
2 changes: 1 addition & 1 deletion examples/core/set_headers/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct PageState {
greeting: String,
}

#[perseus::template_rx(IndexPage)]
#[perseus::template_rx]
pub fn index_page(state: PageStateRx) -> View<G> {
view! {
p { (state.greeting.get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct PageState {
pub message: String,
}

#[perseus::template_rx(AmalgamationPage)]
#[perseus::template_rx]
pub fn amalgamation_page(state: PageStateRx) -> View<G> {
view! {
p { (format!("The message is: '{}'", state.message.get())) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct PageState {
content: String,
}

#[perseus::template_rx(BuildPathsPage)]
#[perseus::template_rx]
pub fn build_paths_page(state: PageStateRx) -> View<G> {
let title = state.title;
let content = state.content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct PageState {
pub greeting: String,
}

#[perseus::template_rx(BuildStatePage)]
#[perseus::template_rx]
pub fn build_state_page(state: PageStateRx) -> View<G> {
view! {
p { (state.greeting.get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct PageState {
content: String,
}

#[perseus::template_rx(IncrementalGenerationPage)]
#[perseus::template_rx]
pub fn incremental_generation_page(state: PageStateRx) -> View<G> {
let title = state.title;
let content = state.content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct PageState {
ip: String,
}

#[perseus::template_rx(RequestStatePage)]
#[perseus::template_rx]
pub fn request_state_page(state: PageStateRx) -> View<G> {
view! {
p {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct PageState {
pub time: String,
}

#[perseus::template_rx(RevalidationPage)]
#[perseus::template_rx]
pub fn revalidation_page(state: PageStateRx) -> View<G> {
view! {
p { (format!("The time when this page was last rendered was '{}'.", state.time.get())) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct PageState {
pub time: String,
}

#[perseus::template_rx(RevalidationPage)]
#[perseus::template_rx]
pub fn revalidation_and_incremental_generation_page(state: PageStateRx) -> View<G> {
view! {
p { (format!("The time when this page was last rendered was '{}'.", state.time.get())) }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/static_content/src/templates/index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use perseus::{Html, Template};
use sycamore::prelude::{view, SsrNode, View};

#[perseus::template_rx(IndexPage)]
#[perseus::template_rx]
pub fn index_page() -> View<G> {
view! {
p { "Hello World!" }
Expand Down
2 changes: 1 addition & 1 deletion examples/demos/fetching/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct IndexPageState {
browser_ip: Option<String>,
}

#[perseus::template_rx(IndexPage)]
#[perseus::template_rx]
pub fn index_page(
IndexPageStateRx {
server_ip,
Expand Down
39 changes: 12 additions & 27 deletions packages/perseus-macro/src/template_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,26 +173,11 @@ pub fn template_impl(input: TemplateFn, attr_args: AttributeArgs) -> TokenStream
} = input;

// We want either one or two arguments
if attr_args.is_empty() || attr_args.len() > 2 {
return quote!(compile_error!(
"this macro takes either one or two arguments"
));
if attr_args.len() > 1 {
return quote!(compile_error!("this macro takes one optional argument"));
}
// This must always be provided
let component_name = match &attr_args[0] {
NestedMeta::Meta(meta) if meta.path().get_ident().is_some() => {
meta.path().get_ident().unwrap()
}
nested_meta => {
return syn::Error::new_spanned(
nested_meta,
"first argument must be a component identifier",
)
.to_compile_error()
}
};
// But this is optional (we'll use `G` as the default if it's not provided)
let type_param = match &attr_args.get(1) {
// This is optional (we'll use `G` as the default if it's not provided)
let type_param = match &attr_args.get(0) {
Some(NestedMeta::Meta(meta)) if meta.path().get_ident().is_some() => {
meta.path().get_ident().unwrap().clone()
}
Expand Down Expand Up @@ -254,7 +239,7 @@ pub fn template_impl(input: TemplateFn, attr_args: AttributeArgs) -> TokenStream
// The user's function
// We know this won't be async because Sycamore doesn't allow that
#(#attrs)*
#[::sycamore::component(#component_name<#type_param>)]
#[::sycamore::component(PerseusPage<#type_param>)]
fn #name#generics(#state_arg) -> #return_type {
let #global_state_arg_pat: #global_state_rx = {
let global_state = ::perseus::get_render_ctx!().global_state.0;
Expand All @@ -266,7 +251,7 @@ pub fn template_impl(input: TemplateFn, attr_args: AttributeArgs) -> TokenStream
#block
}
::sycamore::prelude::view! {
#component_name(())
PerseusPage(())
}
}
},
Expand All @@ -293,7 +278,7 @@ pub fn template_impl(input: TemplateFn, attr_args: AttributeArgs) -> TokenStream
// The user's function
// We know this won't be async because Sycamore doesn't allow that
#(#attrs)*
#[::sycamore::component(#component_name<#type_param>)]
#[::sycamore::component(PerseusPage<#type_param>)]
fn #name#generics(#state_arg) -> #return_type {
let #global_state_arg_pat: #global_state_rx = {
let global_state = ::perseus::get_render_ctx!().global_state.0;
Expand All @@ -305,7 +290,7 @@ pub fn template_impl(input: TemplateFn, attr_args: AttributeArgs) -> TokenStream
#block
}
::sycamore::prelude::view! {
#component_name(
PerseusPage(
{
// Check if properties of the reactive type are already in the page state store
// If they are, we'll use them (so state persists for templates across the whole app)
Expand Down Expand Up @@ -348,12 +333,12 @@ pub fn template_impl(input: TemplateFn, attr_args: AttributeArgs) -> TokenStream
// The user's function, with Sycamore component annotations and the like preserved
// We know this won't be async because Sycamore doesn't allow that
#(#attrs)*
#[::sycamore::component(#component_name<#type_param>)]
#[::sycamore::component(PerseusPage<#type_param>)]
fn #name#generics(#arg) -> #return_type {
#block
}
::sycamore::prelude::view! {
#component_name(
PerseusPage(
{
// Check if properties of the reactive type are already in the page state store
// If they are, we'll use them (so state persists for templates across the whole app)
Expand Down Expand Up @@ -388,12 +373,12 @@ pub fn template_impl(input: TemplateFn, attr_args: AttributeArgs) -> TokenStream
// The user's function, with Sycamore component annotations and the like preserved
// We know this won't be async because Sycamore doesn't allow that
#(#attrs)*
#[::sycamore::component(#component_name<#type_param>)]
#[::sycamore::component(PerseusPage<#type_param>)]
fn #name#generics() -> #return_type {
#block
}
::sycamore::prelude::view! {
#component_name()
PerseusPage()
}
}
}
Expand Down

0 comments on commit d1ba2ef

Please sign in to comment.