From d02189bc4b0fbec0ddb96ade8fa87275f39f3042 Mon Sep 17 00:00:00 2001 From: arctic_hen7 Date: Sun, 10 Oct 2021 12:06:04 +1100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20removed=20`Rc`s=20completel?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Anything wrapped in an `Rc` in a `Template` or an error page no longer needs it! BREAKING CHANGE: `Rc`s are eliminated and done behind the scenes --- README.md | 9 ++-- docs/0.3.x/en-US/updating.md | 3 +- docs/next/en-US/updating.md | 3 +- examples/basic/src/error_pages.rs | 18 +++---- examples/basic/src/templates/about.rs | 9 ++-- examples/basic/src/templates/index.rs | 13 +++-- examples/i18n/src/error_pages.rs | 31 +++++------- examples/i18n/src/templates/about.rs | 5 +- examples/i18n/src/templates/index.rs | 5 +- examples/i18n/src/templates/post.rs | 9 ++-- examples/showcase/src/error_pages.rs | 33 +++++-------- examples/showcase/src/templates/about.rs | 5 +- .../showcase/src/templates/amalgamation.rs | 11 ++--- examples/showcase/src/templates/index.rs | 7 ++- examples/showcase/src/templates/ip.rs | 7 ++- examples/showcase/src/templates/new_post.rs | 5 +- examples/showcase/src/templates/post.rs | 9 ++-- examples/showcase/src/templates/time.rs | 9 ++-- examples/showcase/src/templates/time_root.rs | 9 ++-- examples/tiny/src/lib.rs | 9 ++-- packages/perseus/src/error_pages.rs | 14 ++++-- packages/perseus/src/template.rs | 47 ++++++++++++------- website/website/src/error_pages.rs | 18 +++---- website/website/src/templates/comparisons.rs | 11 ++--- .../website/src/templates/docs/template.rs | 13 +++-- website/website/src/templates/index.rs | 9 ++-- 26 files changed, 152 insertions(+), 169 deletions(-) diff --git a/README.md b/README.md index 1c23e39763..3910ce5e46 100644 --- a/README.md +++ b/README.md @@ -22,21 +22,20 @@ Here's a taste of Perseus (see [the _tiny_ example](https://github.com/arctic-he ```rust use perseus::{define_app, ErrorPages, Template}; -use std::rc::Rc; use sycamore::template; define_app! { templates: [ - Template::::new("index").template(Rc::new(|_| { + Template::::new("index").template(|_| { template! { p { "Hello World!" } } - })) + }) ], - error_pages: ErrorPages::new(Rc::new(|url, status, err, _| { + error_pages: ErrorPages::new(|url, status, err, _| { template! { p { (format!("An error with HTTP code {} occurred at '{}': '{}'.", status, url, err)) } } - })) + }) } ``` diff --git a/docs/0.3.x/en-US/updating.md b/docs/0.3.x/en-US/updating.md index 0c680ad788..d88f057033 100644 --- a/docs/0.3.x/en-US/updating.md +++ b/docs/0.3.x/en-US/updating.md @@ -8,7 +8,8 @@ Perseus v0.3.0 added significant architectural changes to Perseus under the hood 2. Upgrade the Perseus CLI with `cargo install perseus-cli`. 3. Run `perseus clean` to remove the old `.perseus/` directory. 4. Remove any custom config managers you may have, they've been replaced by [mutable and immutable stores](:stores). -5. Update your code for the remaining breaking changes listed in [the CHANGELOG](https://github.com/arctic-hen7/perseus/blob/main/CHANGELOG). +5. Take anything Perseus-related wrapped in `Rc::new` (these will be all through your template definitions and error pages) and remove the `Rc::new`, Perseus now handles that internally, with no performance cost! +6. Update your code for the remaining breaking changes listed in [the CHANGELOG](https://github.com/arctic-hen7/perseus/blob/main/CHANGELOG). Perseus v0.3.0 also changed a few common idioms, like breaking out the `.template()` call into a separate function `template_fn()`. This is no longer recommended, though it will still work fine. You can check out the [examples directory](https://github.com/arctic-hen7/perseus/tree/main/examples) to see how things are a bit nicer now in terms of formatting. diff --git a/docs/next/en-US/updating.md b/docs/next/en-US/updating.md index 0c680ad788..d88f057033 100644 --- a/docs/next/en-US/updating.md +++ b/docs/next/en-US/updating.md @@ -8,7 +8,8 @@ Perseus v0.3.0 added significant architectural changes to Perseus under the hood 2. Upgrade the Perseus CLI with `cargo install perseus-cli`. 3. Run `perseus clean` to remove the old `.perseus/` directory. 4. Remove any custom config managers you may have, they've been replaced by [mutable and immutable stores](:stores). -5. Update your code for the remaining breaking changes listed in [the CHANGELOG](https://github.com/arctic-hen7/perseus/blob/main/CHANGELOG). +5. Take anything Perseus-related wrapped in `Rc::new` (these will be all through your template definitions and error pages) and remove the `Rc::new`, Perseus now handles that internally, with no performance cost! +6. Update your code for the remaining breaking changes listed in [the CHANGELOG](https://github.com/arctic-hen7/perseus/blob/main/CHANGELOG). Perseus v0.3.0 also changed a few common idioms, like breaking out the `.template()` call into a separate function `template_fn()`. This is no longer recommended, though it will still work fine. You can check out the [examples directory](https://github.com/arctic-hen7/perseus/tree/main/examples) to see how things are a bit nicer now in terms of formatting. diff --git a/examples/basic/src/error_pages.rs b/examples/basic/src/error_pages.rs index 35bfbd46b9..edba2da57a 100644 --- a/examples/basic/src/error_pages.rs +++ b/examples/basic/src/error_pages.rs @@ -1,21 +1,17 @@ use perseus::{ErrorPages, GenericNode}; -use std::rc::Rc; use sycamore::template; pub fn get_error_pages() -> ErrorPages { - let mut error_pages = ErrorPages::new(Rc::new(|url, status, err, _| { + let mut error_pages = ErrorPages::new(|url, status, err, _| { template! { p { (format!("An error with HTTP code {} occurred at '{}': '{}'.", status, url, err)) } } - })); - error_pages.add_page( - 404, - Rc::new(|_, _, _, _| { - template! { - p { "Page not found." } - } - }), - ); + }); + error_pages.add_page(404, |_, _, _, _| { + template! { + p { "Page not found." } + } + }); error_pages } diff --git a/examples/basic/src/templates/about.rs b/examples/basic/src/templates/about.rs index 5008b1996d..703b9af775 100644 --- a/examples/basic/src/templates/about.rs +++ b/examples/basic/src/templates/about.rs @@ -1,5 +1,4 @@ use perseus::Template; -use std::rc::Rc; use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate}; #[component(AboutPage)] @@ -11,14 +10,14 @@ pub fn about_page() -> SycamoreTemplate { pub fn get_template() -> Template { Template::new("about") - .template(Rc::new(|_| { + .template(|_| { template! { AboutPage() } - })) - .head(Rc::new(|_| { + }) + .head(|_| { template! { title { "About Page | Perseus Example – Basic" } } - })) + }) } diff --git a/examples/basic/src/templates/index.rs b/examples/basic/src/templates/index.rs index a666c53778..fe53c541ad 100644 --- a/examples/basic/src/templates/index.rs +++ b/examples/basic/src/templates/index.rs @@ -3,7 +3,6 @@ use perseus::{ GenericNode, RenderFnResultWithCause, Template, }; use serde::{Deserialize, Serialize}; -use std::rc::Rc; use sycamore::prelude::{component, template, Template as SycamoreTemplate}; #[derive(Serialize, Deserialize, Debug)] @@ -21,20 +20,20 @@ pub fn index_page(props: IndexPageProps) -> SycamoreTemplate { pub fn get_template() -> Template { Template::new("index") - .build_state_fn(Rc::new(get_build_props)) - .template(Rc::new(|props: Option| { + .build_state_fn(get_build_props) + .template(|props: Option| { template! { IndexPage( serde_json::from_str::(&props.unwrap()).unwrap() ) } - })) - .head(Rc::new(|_| { + }) + .head(|_| { template! { title { "Index Page | Perseus Example – Basic" } } - })) - .set_headers_fn(Rc::new(set_headers)) + }) + .set_headers_fn(set_headers) } pub async fn get_build_props(_path: String, _locale: String) -> RenderFnResultWithCause { diff --git a/examples/i18n/src/error_pages.rs b/examples/i18n/src/error_pages.rs index 53e386e76e..a87fe2e601 100644 --- a/examples/i18n/src/error_pages.rs +++ b/examples/i18n/src/error_pages.rs @@ -1,29 +1,22 @@ use perseus::{ErrorPages, GenericNode}; -use std::rc::Rc; use sycamore::template; pub fn get_error_pages() -> ErrorPages { - let mut error_pages = ErrorPages::new(Rc::new(|_, _, err, _| { + let mut error_pages = ErrorPages::new(|_, _, err, _| { template! { p { (format!("Another error occurred: '{}'.", err)) } } - })); - error_pages.add_page( - 404, - Rc::new(|_, _, _, _| { - template! { - p { "Page not found." } - } - }), - ); - error_pages.add_page( - 400, - Rc::new(|_, _, _, _| { - template! { - p { "Client error occurred..." } - } - }), - ); + }); + error_pages.add_page(404, |_, _, _, _| { + template! { + p { "Page not found." } + } + }); + error_pages.add_page(400, |_, _, _, _| { + template! { + p { "Client error occurred..." } + } + }); error_pages } diff --git a/examples/i18n/src/templates/about.rs b/examples/i18n/src/templates/about.rs index ba8ae78b72..ccc1accdfb 100644 --- a/examples/i18n/src/templates/about.rs +++ b/examples/i18n/src/templates/about.rs @@ -1,5 +1,4 @@ use perseus::{is_server, t, Template}; -use std::rc::Rc; use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate}; #[component(AboutPage)] @@ -19,9 +18,9 @@ pub fn about_page() -> SycamoreTemplate { } pub fn get_template() -> Template { - Template::new("about").template(Rc::new(|_| { + Template::new("about").template(|_| { template! { AboutPage() } - })) + }) } diff --git a/examples/i18n/src/templates/index.rs b/examples/i18n/src/templates/index.rs index 793dddfc78..cbc0618a2a 100644 --- a/examples/i18n/src/templates/index.rs +++ b/examples/i18n/src/templates/index.rs @@ -1,5 +1,4 @@ use perseus::{link, t, Template}; -use std::rc::Rc; use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate}; #[component(IndexPage)] @@ -14,9 +13,9 @@ pub fn index_page() -> SycamoreTemplate { } pub fn get_template() -> Template { - Template::new("index").template(Rc::new(|_| { + Template::new("index").template(|_| { template! { IndexPage() } - })) + }) } diff --git a/examples/i18n/src/templates/post.rs b/examples/i18n/src/templates/post.rs index bd469e563e..4325db6bd6 100644 --- a/examples/i18n/src/templates/post.rs +++ b/examples/i18n/src/templates/post.rs @@ -1,6 +1,5 @@ use perseus::{link, RenderFnResult, RenderFnResultWithCause, Template}; use serde::{Deserialize, Serialize}; -use std::rc::Rc; use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate}; #[derive(Serialize, Deserialize)] @@ -28,15 +27,15 @@ pub fn post_page(props: PostPageProps) -> SycamoreTemplate { pub fn get_template() -> Template { Template::new("post") - .build_paths_fn(Rc::new(get_static_paths)) - .build_state_fn(Rc::new(get_static_props)) - .template(Rc::new(|props| { + .build_paths_fn(get_static_paths) + .build_state_fn(get_static_props) + .template(|props| { template! { PostPage( serde_json::from_str::(&props.unwrap()).unwrap() ) } - })) + }) } pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWithCause { diff --git a/examples/showcase/src/error_pages.rs b/examples/showcase/src/error_pages.rs index 2db2486715..1b7aab88fe 100644 --- a/examples/showcase/src/error_pages.rs +++ b/examples/showcase/src/error_pages.rs @@ -1,30 +1,23 @@ use perseus::{ErrorPages, GenericNode}; -use std::rc::Rc; use sycamore::template; pub fn get_error_pages() -> ErrorPages { - let mut error_pages = ErrorPages::new(Rc::new(|_, _, _, _| { + let mut error_pages = ErrorPages::new(|_, _, _, _| { template! { p { "Another error occurred." } } - })); - error_pages.add_page( - 404, - Rc::new(|_, _, err, _| { - template! { - p { "Page not found." } - p { (err) } - } - }), - ); - error_pages.add_page( - 400, - Rc::new(|_, _, _, _| { - template! { - p { "Client error occurred..." } - } - }), - ); + }); + error_pages.add_page(404, |_, _, err, _| { + template! { + p { "Page not found." } + p { (err) } + } + }); + error_pages.add_page(400, |_, _, _, _| { + template! { + p { "Client error occurred..." } + } + }); error_pages } diff --git a/examples/showcase/src/templates/about.rs b/examples/showcase/src/templates/about.rs index 9ef3c905dc..d4cc46c449 100644 --- a/examples/showcase/src/templates/about.rs +++ b/examples/showcase/src/templates/about.rs @@ -1,5 +1,4 @@ use perseus::Template; -use std::rc::Rc; use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate}; #[component(AboutPage)] @@ -10,9 +9,9 @@ pub fn about_page() -> SycamoreTemplate { } pub fn get_template() -> Template { - Template::new("about").template(Rc::new(|_| { + Template::new("about").template(|_| { template! { AboutPage() } - })) + }) } diff --git a/examples/showcase/src/templates/amalgamation.rs b/examples/showcase/src/templates/amalgamation.rs index 165c6a7827..46de319910 100644 --- a/examples/showcase/src/templates/amalgamation.rs +++ b/examples/showcase/src/templates/amalgamation.rs @@ -1,6 +1,5 @@ use perseus::{RenderFnResultWithCause, Request, States, Template}; use serde::{Deserialize, Serialize}; -use std::rc::Rc; use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate}; #[derive(Serialize, Deserialize, Debug)] @@ -17,16 +16,16 @@ pub fn amalgamation_page(props: AmalagamationPageProps) -> SycamoreTemplate { pub fn get_template() -> Template { Template::new("amalgamation") - .build_state_fn(Rc::new(get_build_state)) - .request_state_fn(Rc::new(get_request_state)) - .amalgamate_states_fn(Rc::new(amalgamate_states)) - .template(Rc::new(|props| { + .build_state_fn(get_build_state) + .request_state_fn(get_request_state) + .amalgamate_states_fn(amalgamate_states) + .template(|props| { template! { AmalgamationPage( serde_json::from_str::(&props.unwrap()).unwrap() ) } - })) + }) } pub fn amalgamate_states(states: States) -> RenderFnResultWithCause> { diff --git a/examples/showcase/src/templates/index.rs b/examples/showcase/src/templates/index.rs index 04f84f78d2..455847d367 100644 --- a/examples/showcase/src/templates/index.rs +++ b/examples/showcase/src/templates/index.rs @@ -1,6 +1,5 @@ use perseus::{RenderFnResultWithCause, Template}; use serde::{Deserialize, Serialize}; -use std::rc::Rc; use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate}; #[derive(Serialize, Deserialize, Debug)] @@ -17,14 +16,14 @@ pub fn index_page(props: IndexPageProps) -> SycamoreTemplate { pub fn get_template() -> Template { Template::new("index") - .build_state_fn(Rc::new(get_static_props)) - .template(Rc::new(|props| { + .build_state_fn(get_static_props) + .template(|props| { template! { IndexPage( serde_json::from_str::(&props.unwrap()).unwrap() ) } - })) + }) } pub async fn get_static_props(_path: String, _locale: String) -> RenderFnResultWithCause { diff --git a/examples/showcase/src/templates/ip.rs b/examples/showcase/src/templates/ip.rs index 2d47556176..f9893fc5fd 100644 --- a/examples/showcase/src/templates/ip.rs +++ b/examples/showcase/src/templates/ip.rs @@ -1,6 +1,5 @@ use perseus::{RenderFnResultWithCause, Request, Template}; use serde::{Deserialize, Serialize}; -use std::rc::Rc; use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate}; #[derive(Serialize, Deserialize)] @@ -21,14 +20,14 @@ pub fn dashboard_page(props: IpPageProps) -> SycamoreTemplate { pub fn get_template() -> Template { Template::new("ip") - .request_state_fn(Rc::new(get_request_state)) - .template(Rc::new(|props| { + .request_state_fn(get_request_state) + .template(|props| { template! { IpPage( serde_json::from_str::(&props.unwrap()).unwrap() ) } - })) + }) } pub async fn get_request_state( diff --git a/examples/showcase/src/templates/new_post.rs b/examples/showcase/src/templates/new_post.rs index 1daf110f5b..2797f29b7d 100644 --- a/examples/showcase/src/templates/new_post.rs +++ b/examples/showcase/src/templates/new_post.rs @@ -1,5 +1,4 @@ use perseus::Template; -use std::rc::Rc; use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate}; #[component(NewPostPage)] @@ -10,9 +9,9 @@ pub fn new_post_page() -> SycamoreTemplate { } pub fn get_template() -> Template { - Template::new("post/new").template(Rc::new(|_| { + Template::new("post/new").template(|_| { template! { NewPostPage() } - })) + }) } diff --git a/examples/showcase/src/templates/post.rs b/examples/showcase/src/templates/post.rs index 7e28c6b3fa..d0426c74e7 100644 --- a/examples/showcase/src/templates/post.rs +++ b/examples/showcase/src/templates/post.rs @@ -2,7 +2,6 @@ use perseus::{ ErrorCause, GenericErrorWithCause, RenderFnResult, RenderFnResultWithCause, Template, }; use serde::{Deserialize, Serialize}; -use std::rc::Rc; use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate}; #[derive(Serialize, Deserialize)] @@ -27,16 +26,16 @@ pub fn post_page(props: PostPageProps) -> SycamoreTemplate { pub fn get_template() -> Template { Template::new("post") - .build_paths_fn(Rc::new(get_static_paths)) - .build_state_fn(Rc::new(get_static_props)) + .build_paths_fn(get_static_paths) + .build_state_fn(get_static_props) .incremental_generation() - .template(Rc::new(|props| { + .template(|props| { template! { PostPage( serde_json::from_str::(&props.unwrap()).unwrap() ) } - })) + }) } pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWithCause { diff --git a/examples/showcase/src/templates/time.rs b/examples/showcase/src/templates/time.rs index 67b957f43a..6d70b15701 100644 --- a/examples/showcase/src/templates/time.rs +++ b/examples/showcase/src/templates/time.rs @@ -2,7 +2,6 @@ use perseus::{ ErrorCause, GenericErrorWithCause, RenderFnResult, RenderFnResultWithCause, Template, }; use serde::{Deserialize, Serialize}; -use std::rc::Rc; use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate}; #[derive(Serialize, Deserialize, Debug)] @@ -19,18 +18,18 @@ pub fn time_page(props: TimePageProps) -> SycamoreTemplate { pub fn get_template() -> Template { Template::new("timeisr") - .template(Rc::new(|props| { + .template(|props| { template! { TimePage( serde_json::from_str::(&props.unwrap()).unwrap() ) } - })) + }) // This page will revalidate every five seconds (to illustrate revalidation) .revalidate_after("5s".to_string()) .incremental_generation() - .build_state_fn(Rc::new(get_build_state)) - .build_paths_fn(Rc::new(get_build_paths)) + .build_state_fn(get_build_state) + .build_paths_fn(get_build_paths) } pub async fn get_build_state(path: String, _locale: String) -> RenderFnResultWithCause { diff --git a/examples/showcase/src/templates/time_root.rs b/examples/showcase/src/templates/time_root.rs index ec177e6db1..8ee0440af9 100644 --- a/examples/showcase/src/templates/time_root.rs +++ b/examples/showcase/src/templates/time_root.rs @@ -1,6 +1,5 @@ use perseus::{RenderFnResultWithCause, Template}; use serde::{Deserialize, Serialize}; -use std::rc::Rc; use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate}; #[derive(Serialize, Deserialize, Debug)] @@ -17,18 +16,18 @@ pub fn time_page(props: TimePageProps) -> SycamoreTemplate { pub fn get_template() -> Template { Template::new("time") - .template(Rc::new(|props| { + .template(|props| { template! { TimePage( serde_json::from_str::(&props.unwrap()).unwrap() ) } - })) + }) // This page will revalidate every five seconds (to illustrate revalidation) // Try changing this to a week, even though the below custom logic says to always revalidate, we'll only do it weekly .revalidate_after("5s".to_string()) - .should_revalidate_fn(Rc::new(|| async { Ok(true) })) - .build_state_fn(Rc::new(get_build_state)) + .should_revalidate_fn(|| async { Ok(true) }) + .build_state_fn(get_build_state) } pub async fn get_build_state(_path: String, _locale: String) -> RenderFnResultWithCause { diff --git a/examples/tiny/src/lib.rs b/examples/tiny/src/lib.rs index 3a5627d4af..06c77165b5 100644 --- a/examples/tiny/src/lib.rs +++ b/examples/tiny/src/lib.rs @@ -1,17 +1,16 @@ use perseus::{define_app, ErrorPages, Template}; -use std::rc::Rc; use sycamore::template; define_app! { templates: [ - Template::::new("index").template(Rc::new(|_| { + Template::::new("index").template(|_| { template! { p { "Hello World!" } } - })) + }) ], - error_pages: ErrorPages::new(Rc::new(|url, status, err, _| { + error_pages: ErrorPages::new(|url, status, err, _| { template! { p { (format!("An error with HTTP code {} occurred at '{}': '{}'.", status, url, err)) } } - })) + }) } diff --git a/packages/perseus/src/error_pages.rs b/packages/perseus/src/error_pages.rs index 88ba9b5599..dedf6f45a2 100644 --- a/packages/perseus/src/error_pages.rs +++ b/packages/perseus/src/error_pages.rs @@ -20,16 +20,22 @@ pub struct ErrorPages { } impl ErrorPages { /// Creates a new definition of error pages with just a fallback. - pub fn new(fallback: ErrorPageTemplate) -> Self { + pub fn new( + fallback: impl Fn(String, u16, String, Option>) -> SycamoreTemplate + 'static, + ) -> Self { Self { status_pages: HashMap::default(), - fallback, + fallback: Rc::new(fallback), } } /// Adds a new page for the given status code. If a page was already defined for the given code, it will be updated by the mechanics of /// the internal `HashMap`. - pub fn add_page(&mut self, status: u16, page: ErrorPageTemplate) { - self.status_pages.insert(status, page); + pub fn add_page( + &mut self, + status: u16, + page: impl Fn(String, u16, String, Option>) -> SycamoreTemplate + 'static, + ) { + self.status_pages.insert(status, Rc::new(page)); } /// Gets the internal template function to render. fn get_template_fn(&self, status: &u16) -> &ErrorPageTemplate { diff --git a/packages/perseus/src/template.rs b/packages/perseus/src/template.rs index d188a6b70b..a40c974ada 100644 --- a/packages/perseus/src/template.rs +++ b/packages/perseus/src/template.rs @@ -442,23 +442,32 @@ impl Template { // Builder setters /// Sets the template rendering function to use. - pub fn template(mut self, val: TemplateFn) -> Template { - self.template = val; + pub fn template( + mut self, + val: impl Fn(Option) -> SycamoreTemplate + 'static, + ) -> Template { + self.template = Rc::new(val); self } /// Sets the document head rendering function to use. - pub fn head(mut self, val: HeadFn) -> Template { - self.head = val; + pub fn head( + mut self, + val: impl Fn(Option) -> SycamoreTemplate + 'static, + ) -> Template { + self.head = Rc::new(val); self } /// Sets the function to set headers. This will override Perseus' inbuilt header defaults. - pub fn set_headers_fn(mut self, val: SetHeadersFn) -> Template { - self.set_headers = val; + pub fn set_headers_fn( + mut self, + val: impl Fn(Option) -> HeaderMap + 'static, + ) -> Template { + self.set_headers = Rc::new(val); self } /// Enables the *build paths* strategy with the given function. - pub fn build_paths_fn(mut self, val: GetBuildPathsFn) -> Template { - self.get_build_paths = Some(val); + pub fn build_paths_fn(mut self, val: impl GetBuildPathsFnType + 'static) -> Template { + self.get_build_paths = Some(Rc::new(val)); self } /// Enables the *incremental generation* strategy. @@ -467,18 +476,21 @@ impl Template { self } /// Enables the *build state* strategy with the given function. - pub fn build_state_fn(mut self, val: GetBuildStateFn) -> Template { - self.get_build_state = Some(val); + pub fn build_state_fn(mut self, val: impl GetBuildStateFnType + 'static) -> Template { + self.get_build_state = Some(Rc::new(val)); self } /// Enables the *request state* strategy with the given function. - pub fn request_state_fn(mut self, val: GetRequestStateFn) -> Template { - self.get_request_state = Some(val); + pub fn request_state_fn(mut self, val: impl GetRequestStateFnType + 'static) -> Template { + self.get_request_state = Some(Rc::new(val)); self } /// Enables the *revalidation* strategy (logic variant) with the given function. - pub fn should_revalidate_fn(mut self, val: ShouldRevalidateFn) -> Template { - self.should_revalidate = Some(val); + pub fn should_revalidate_fn( + mut self, + val: impl ShouldRevalidateFnType + 'static, + ) -> Template { + self.should_revalidate = Some(Rc::new(val)); self } /// Enables the *revalidation* strategy (time variant). This takes a time string of a form like `1w` for one week. More details are available @@ -488,8 +500,11 @@ impl Template { self } /// Enables state amalgamation with the given function. - pub fn amalgamate_states_fn(mut self, val: AmalgamateStatesFn) -> Template { - self.amalgamate_states = Some(val); + pub fn amalgamate_states_fn( + mut self, + val: impl Fn(States) -> RenderFnResultWithCause> + 'static, + ) -> Template { + self.amalgamate_states = Some(Rc::new(val)); self } } diff --git a/website/website/src/error_pages.rs b/website/website/src/error_pages.rs index bc352487ce..5d22b1b2fd 100644 --- a/website/website/src/error_pages.rs +++ b/website/website/src/error_pages.rs @@ -1,22 +1,18 @@ use perseus::{ErrorPages, GenericNode}; -use std::rc::Rc; use sycamore::template; // This site will be exported statically, so we only have control over 404 pages for broken links in the site itself pub fn get_error_pages() -> ErrorPages { - let mut error_pages = ErrorPages::new(Rc::new(|url, status, err, _| { + let mut error_pages = ErrorPages::new(|url, status, err, _| { template! { p { (format!("An error with HTTP code {} occurred at '{}': '{}'.", status, url, err)) } } - })); - error_pages.add_page( - 404, - Rc::new(|_, _, _, _| { - template! { - p { "Page not found." } - } - }), - ); + }); + error_pages.add_page(404, |_, _, _, _| { + template! { + p { "Page not found." } + } + }); error_pages } diff --git a/website/website/src/templates/comparisons.rs b/website/website/src/templates/comparisons.rs index b849826ab3..bc053de549 100644 --- a/website/website/src/templates/comparisons.rs +++ b/website/website/src/templates/comparisons.rs @@ -8,7 +8,6 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::fs; use std::path::PathBuf; -use std::rc::Rc; use sycamore::prelude::Template as SycamoreTemplate; use sycamore::prelude::*; use walkdir::WalkDir; @@ -365,17 +364,17 @@ pub fn comparisons_page(props: ComparisonsPageProps) -> SycamoreTemplate { pub fn get_template() -> Template { Template::new("comparisons") - .template(Rc::new(|props| { + .template(|props| { template! { ComparisonsPage(serde_json::from_str(&props.unwrap()).unwrap()) } - })) - .head(Rc::new(|_| { + }) + .head(|_| { template! { title { (format!("{} | {}", t!("comparisons-title"), t!("perseus"))) } } - })) - .build_state_fn(Rc::new(get_build_state)) + }) + .build_state_fn(get_build_state) } pub async fn get_build_state(_path: String, _locale: String) -> RenderFnResultWithCause { diff --git a/website/website/src/templates/docs/template.rs b/website/website/src/templates/docs/template.rs index 222ccb580d..8495cf5d06 100644 --- a/website/website/src/templates/docs/template.rs +++ b/website/website/src/templates/docs/template.rs @@ -4,7 +4,6 @@ use crate::templates::docs::generation::{ }; use perseus::{t, GenericNode, Template}; use serde::{Deserialize, Serialize}; -use std::rc::Rc; use sycamore::prelude::Template as SycamoreTemplate; use sycamore::prelude::*; @@ -53,14 +52,14 @@ pub fn docs_page(props: DocsPageProps) -> SycamoreTemplate { pub fn get_template() -> Template { Template::new("docs") - .build_paths_fn(Rc::new(get_build_paths)) - .build_state_fn(Rc::new(get_build_state)) - .template(Rc::new(|props| { + .build_paths_fn(get_build_paths) + .build_state_fn(get_build_state) + .template(|props| { template! { DocsPage(serde_json::from_str(&props.unwrap()).unwrap()) } - })) - .head(Rc::new(|props| { + }) + .head(|props| { let props: DocsPageProps = serde_json::from_str(&props.unwrap()).unwrap(); template! { title { (format!("{} | {}", props.title, t!("docs-title-base"))) } @@ -68,5 +67,5 @@ pub fn get_template() -> Template { link(rel = "stylesheet", href = ".perseus/static/styles/docs_links_markdown.css") link(rel = "stylesheet", href = ".perseus/static/prism.css") } - })) + }) } diff --git a/website/website/src/templates/index.rs b/website/website/src/templates/index.rs index f6c29b19ca..36318caef4 100644 --- a/website/website/src/templates/index.rs +++ b/website/website/src/templates/index.rs @@ -2,7 +2,6 @@ use crate::components::container::{Container, ContainerProps}; use crate::components::features_list::get_features_list; use crate::components::github_svg::GITHUB_SVG; use perseus::{link, t, GenericNode, Template}; -use std::rc::Rc; use sycamore::prelude::Template as SycamoreTemplate; use sycamore::prelude::*; @@ -131,14 +130,14 @@ pub fn index_page() -> SycamoreTemplate { pub fn get_template() -> Template { Template::new("index") - .template(Rc::new(|_| { + .template(|_| { template! { IndexPage() } - })) - .head(Rc::new(|_| { + }) + .head(|_| { template! { title { (t!("perseus")) } } - })) + }) }