Skip to content

Commit

Permalink
feat: ✨ removed Rcs completely
Browse files Browse the repository at this point in the history
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
  • Loading branch information
arctic-hen7 committed Oct 10, 2021
1 parent 488a9a0 commit d02189b
Show file tree
Hide file tree
Showing 26 changed files with 152 additions and 169 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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::<G>::new("index").template(Rc::new(|_| {
Template::<G>::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)) }
}
}))
})
}
```

Expand Down
3 changes: 2 additions & 1 deletion docs/0.3.x/en-US/updating.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion docs/next/en-US/updating.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
18 changes: 7 additions & 11 deletions examples/basic/src/error_pages.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
use perseus::{ErrorPages, GenericNode};
use std::rc::Rc;
use sycamore::template;

pub fn get_error_pages<G: GenericNode>() -> ErrorPages<G> {
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
}
9 changes: 4 additions & 5 deletions examples/basic/src/templates/about.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use perseus::Template;
use std::rc::Rc;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[component(AboutPage<G>)]
Expand All @@ -11,14 +10,14 @@ pub fn about_page() -> SycamoreTemplate<G> {

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("about")
.template(Rc::new(|_| {
.template(|_| {
template! {
AboutPage()
}
}))
.head(Rc::new(|_| {
})
.head(|_| {
template! {
title { "About Page | Perseus Example – Basic" }
}
}))
})
}
13 changes: 6 additions & 7 deletions examples/basic/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -21,20 +20,20 @@ pub fn index_page(props: IndexPageProps) -> SycamoreTemplate<G> {

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("index")
.build_state_fn(Rc::new(get_build_props))
.template(Rc::new(|props: Option<String>| {
.build_state_fn(get_build_props)
.template(|props: Option<String>| {
template! {
IndexPage(
serde_json::from_str::<IndexPageProps>(&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<String> {
Expand Down
31 changes: 12 additions & 19 deletions examples/i18n/src/error_pages.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
use perseus::{ErrorPages, GenericNode};
use std::rc::Rc;
use sycamore::template;

pub fn get_error_pages<G: GenericNode>() -> ErrorPages<G> {
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
}
5 changes: 2 additions & 3 deletions examples/i18n/src/templates/about.rs
Original file line number Diff line number Diff line change
@@ -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<G>)]
Expand All @@ -19,9 +18,9 @@ pub fn about_page() -> SycamoreTemplate<G> {
}

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("about").template(Rc::new(|_| {
Template::new("about").template(|_| {
template! {
AboutPage()
}
}))
})
}
5 changes: 2 additions & 3 deletions examples/i18n/src/templates/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use perseus::{link, t, Template};
use std::rc::Rc;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[component(IndexPage<G>)]
Expand All @@ -14,9 +13,9 @@ pub fn index_page() -> SycamoreTemplate<G> {
}

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("index").template(Rc::new(|_| {
Template::new("index").template(|_| {
template! {
IndexPage()
}
}))
})
}
9 changes: 4 additions & 5 deletions examples/i18n/src/templates/post.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -28,15 +27,15 @@ pub fn post_page(props: PostPageProps) -> SycamoreTemplate<G> {

pub fn get_template<G: GenericNode>() -> Template<G> {
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::<PostPageProps>(&props.unwrap()).unwrap()
)
}
}))
})
}

pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWithCause<String> {
Expand Down
33 changes: 13 additions & 20 deletions examples/showcase/src/error_pages.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
use perseus::{ErrorPages, GenericNode};
use std::rc::Rc;
use sycamore::template;

pub fn get_error_pages<G: GenericNode>() -> ErrorPages<G> {
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
}
5 changes: 2 additions & 3 deletions examples/showcase/src/templates/about.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use perseus::Template;
use std::rc::Rc;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[component(AboutPage<G>)]
Expand All @@ -10,9 +9,9 @@ pub fn about_page() -> SycamoreTemplate<G> {
}

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("about").template(Rc::new(|_| {
Template::new("about").template(|_| {
template! {
AboutPage()
}
}))
})
}
11 changes: 5 additions & 6 deletions examples/showcase/src/templates/amalgamation.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -17,16 +16,16 @@ pub fn amalgamation_page(props: AmalagamationPageProps) -> SycamoreTemplate<G> {

pub fn get_template<G: GenericNode>() -> Template<G> {
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::<AmalagamationPageProps>(&props.unwrap()).unwrap()
)
}
}))
})
}

pub fn amalgamate_states(states: States) -> RenderFnResultWithCause<Option<String>> {
Expand Down
7 changes: 3 additions & 4 deletions examples/showcase/src/templates/index.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -17,14 +16,14 @@ pub fn index_page(props: IndexPageProps) -> SycamoreTemplate<G> {

pub fn get_template<G: GenericNode>() -> Template<G> {
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::<IndexPageProps>(&props.unwrap()).unwrap()
)
}
}))
})
}

pub async fn get_static_props(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
Expand Down
7 changes: 3 additions & 4 deletions examples/showcase/src/templates/ip.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -21,14 +20,14 @@ pub fn dashboard_page(props: IpPageProps) -> SycamoreTemplate<G> {

pub fn get_template<G: GenericNode>() -> Template<G> {
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::<IpPageProps>(&props.unwrap()).unwrap()
)
}
}))
})
}

pub async fn get_request_state(
Expand Down
5 changes: 2 additions & 3 deletions examples/showcase/src/templates/new_post.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use perseus::Template;
use std::rc::Rc;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[component(NewPostPage<G>)]
Expand All @@ -10,9 +9,9 @@ pub fn new_post_page() -> SycamoreTemplate<G> {
}

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("post/new").template(Rc::new(|_| {
Template::new("post/new").template(|_| {
template! {
NewPostPage()
}
}))
})
}
Loading

0 comments on commit d02189b

Please sign in to comment.