diff --git a/crates/core/src/handlers/views/reauth.rs b/crates/core/src/handlers/views/reauth.rs index 2d658e9a..65ea0eae 100644 --- a/crates/core/src/handlers/views/reauth.rs +++ b/crates/core/src/handlers/views/reauth.rs @@ -27,7 +27,7 @@ use crate::{ with_templates, CsrfToken, }, storage::SessionInfo, - templates::{TemplateContext, Templates}, + templates::{EmptyContext, TemplateContext, Templates}, }; #[derive(Deserialize, Debug)] @@ -63,7 +63,7 @@ async fn get( csrf_token: CsrfToken, session: SessionInfo, ) -> Result { - let ctx = ().with_session(session).with_csrf(&csrf_token); + let ctx = EmptyContext.with_session(session).with_csrf(&csrf_token); let content = templates.render_reauth(&ctx)?; let reply = html(content); diff --git a/crates/core/src/handlers/views/register.rs b/crates/core/src/handlers/views/register.rs index 38ed5a4c..0833387a 100644 --- a/crates/core/src/handlers/views/register.rs +++ b/crates/core/src/handlers/views/register.rs @@ -31,7 +31,7 @@ use crate::{ with_templates, CsrfToken, }, storage::{register_user, user::start_session, SessionInfo}, - templates::{TemplateContext, Templates}, + templates::{EmptyContext, TemplateContext, Templates}, }; #[derive(Serialize, Deserialize)] @@ -115,7 +115,7 @@ async fn get( if maybe_session.is_some() { Ok(Box::new(query.redirect()?)) } else { - let ctx = ().with_csrf(&csrf_token); + let ctx = EmptyContext.with_csrf(&csrf_token); let content = templates.render_register(&ctx)?; let reply = html(content); let reply = cookie_saver.save_encrypted(&csrf_token, reply)?; diff --git a/crates/core/src/templates/context.rs b/crates/core/src/templates/context.rs index ce38bb9e..164a6555 100644 --- a/crates/core/src/templates/context.rs +++ b/crates/core/src/templates/context.rs @@ -19,22 +19,31 @@ use url::Url; use crate::{errors::ErroredForm, filters::CsrfToken, storage::SessionInfo}; /// Helper trait to construct context wrappers -pub trait TemplateContext: Sized { - fn with_session(self, current_session: SessionInfo) -> WithSession { +pub trait TemplateContext { + fn with_session(self, current_session: SessionInfo) -> WithSession + where + Self: Sized, + { WithSession { current_session, inner: self, } } - fn maybe_with_session(self, current_session: Option) -> WithOptionalSession { + fn maybe_with_session(self, current_session: Option) -> WithOptionalSession + where + Self: Sized, + { WithOptionalSession { current_session, inner: self, } } - fn with_csrf(self, token: &CsrfToken) -> WithCsrf { + fn with_csrf(self, token: &CsrfToken) -> WithCsrf + where + Self: Sized, + { WithCsrf { csrf_token: token.form_value(), inner: self, @@ -42,13 +51,13 @@ pub trait TemplateContext: Sized { } } -impl TemplateContext for () {} +impl TemplateContext for EmptyContext {} impl TemplateContext for IndexContext {} impl TemplateContext for LoginContext {} -impl TemplateContext for FormPostContext {} -impl TemplateContext for WithSession {} -impl TemplateContext for WithOptionalSession {} -impl TemplateContext for WithCsrf {} +impl TemplateContext for FormPostContext {} +impl TemplateContext for WithSession {} +impl TemplateContext for WithOptionalSession {} +impl TemplateContext for WithCsrf {} /// Context with a CSRF token in it #[derive(Serialize)] @@ -77,6 +86,10 @@ pub struct WithOptionalSession { inner: T, } +/// An empty context used for composition +#[derive(Serialize)] +pub struct EmptyContext; + // Context used by the `index.html` template #[derive(Serialize)] pub struct IndexContext { diff --git a/crates/core/src/templates/mod.rs b/crates/core/src/templates/mod.rs index cb35821b..22fc27b3 100644 --- a/crates/core/src/templates/mod.rs +++ b/crates/core/src/templates/mod.rs @@ -27,8 +27,8 @@ mod context; mod macros; pub use self::context::{ - ErrorContext, FormPostContext, IndexContext, LoginContext, LoginFormField, TemplateContext, - WithCsrf, WithOptionalSession, WithSession, + EmptyContext, ErrorContext, FormPostContext, IndexContext, LoginContext, LoginFormField, + TemplateContext, WithCsrf, WithOptionalSession, WithSession, }; #[derive(Clone)] @@ -163,13 +163,13 @@ register_templates! { pub fn render_login(WithCsrf) { "login.html" } /// Render the registration page - pub fn render_register(WithCsrf<()>) { "register.html" } + pub fn render_register(WithCsrf) { "register.html" } /// Render the home page pub fn render_index(WithCsrf>) { "index.html" } /// Render the re-authentication form - pub fn render_reauth(WithCsrf>) { "reauth.html" } + pub fn render_reauth(WithCsrf>) { "reauth.html" } /// Render the form used by the form_post response mode pub fn render_form_post(FormPostContext) { "form_post.html" }