diff --git a/src/main.rs b/src/main.rs index 2149bd7..39355d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ mod database; mod states; mod routes; mod responses; +mod views; mod schema; mod models; mod forms; diff --git a/src/responses.rs b/src/responses.rs index f6a74b2..9699345 100644 --- a/src/responses.rs +++ b/src/responses.rs @@ -1,3 +1,5 @@ +use crate::views; + use rocket::response::Redirect; use rocket_contrib::templates::Template; @@ -15,12 +17,6 @@ pub enum CommonResponse { UnknownError(Template), } -#[derive(Serialize)] -struct ErrorTemplateContext { - layout: &'static str, - error_code: u16, -} - impl From for CommonResponse { fn from(_: rocket_csrf::VerificationFailure) -> Self { Self::InvalidAuthenticityToken(Redirect::to("/")) @@ -29,11 +25,17 @@ impl From for CommonResponse { impl From for CommonResponse { fn from(_: diesel::result::Error) -> Self { - let template_context = ErrorTemplateContext { - layout: "site", + let page_context = views::Error { error_code: 500, }; - Self::UnknownError(Template::render("error", &template_context)) + let context = views::Site { + page: "error".to_string(), + page_context, + authenticity_token: "".to_string(), // TODO + current_user: None, // TODO + }; + + Self::UnknownError(Template::render("site", &context)) } } diff --git a/src/routes/home.rs b/src/routes/home.rs index 79bd847..300e089 100644 --- a/src/routes/home.rs +++ b/src/routes/home.rs @@ -1,5 +1,6 @@ use crate::database; use crate::states; +use crate::views; use crate::models; use crate::responses::CommonResponse; @@ -15,18 +16,16 @@ pub fn index( ) -> Result { let all_users = models::User::all(db_conn)?; - Ok(Template::render("home/index", &IndexTemplateContext { - authenticity_token: csrf_token.0, - layout: "site", - current_user: current_user.0, + let page_context = views::home::Index { users: all_users, - })) -} + }; -#[derive(Serialize)] -struct IndexTemplateContext { - authenticity_token: String, - layout: &'static str, - current_user: Option, - users: Vec, + let context = views::Site { + page: "home/index".to_string(), + page_context, + authenticity_token: csrf_token.0, + current_user: current_user.0, + }; + + Ok(Template::render("site", &context)) } diff --git a/src/routes/sessions.rs b/src/routes/sessions.rs index 4e329ec..7df5294 100644 --- a/src/routes/sessions.rs +++ b/src/routes/sessions.rs @@ -1,5 +1,6 @@ use crate::database; use crate::states; +use crate::views; use crate::models; use crate::forms; @@ -22,10 +23,18 @@ pub fn new( )); } - Ok(Template::render("sessions/new", &BasicTemplateContext { - authenticity_token: csrf_token.0, - layout: "site", - })) + let page_context = views::sessions::New { + authenticity_token: csrf_token.0.to_string(), + }; + + let context = views::Site { + page: "sessions/new".to_string(), + page_context, + authenticity_token: csrf_token.0.to_string(), + current_user: None, + }; + + Ok(Template::render("site", &context)) } #[post("/sign_in", data = "
")] @@ -76,17 +85,19 @@ pub fn delete( Ok(Redirect::to(uri!(super::home::index))) } -#[derive(Serialize)] -struct BasicTemplateContext { - authenticity_token: String, - layout: &'static str, -} - fn invalid_sign_in_credentials(authenticity_token: &String) -> CommonResponse { + let page_context = views::sessions::New { + authenticity_token: authenticity_token.to_string(), + }; + + let context = views::Site { + page: "sessions/new".to_string(), + page_context, + authenticity_token: authenticity_token.to_string(), + current_user: None, + }; + CommonResponse::InvalidCredentials( - Template::render("sessions/new", &BasicTemplateContext { - authenticity_token: authenticity_token.to_string(), - layout: "site", - }) + Template::render("site", &context) ) } diff --git a/src/routes/users.rs b/src/routes/users.rs index bd337d7..8fbad49 100644 --- a/src/routes/users.rs +++ b/src/routes/users.rs @@ -1,5 +1,6 @@ use crate::database; use crate::states; +use crate::views; use crate::models; use crate::forms; @@ -22,10 +23,18 @@ pub fn new( )); } - Ok(Template::render("users/new", &BasicTemplateContext { - authenticity_token: csrf_token.0, - layout: "site", - })) + let page_context = views::users::New { + authenticity_token: csrf_token.0.to_string(), + }; + + let context = views::Site { + page: "users/new".to_string(), + page_context, + authenticity_token: csrf_token.0.to_string(), + current_user: None, + }; + + Ok(Template::render("site", &context)) } #[post("/sign_up", data = "")] @@ -53,12 +62,6 @@ pub fn create( Ok(Redirect::to(uri!(super::home::index))) } -#[derive(Serialize)] -struct BasicTemplateContext { - authenticity_token: String, - layout: &'static str, -} - struct XXXXX { form: forms::UserSignUp, authenticity_token: String, @@ -81,9 +84,17 @@ impl XXXXX { impl From for CommonResponse { fn from(yyyyy: YYYYY) -> Self { - Self::InvalidForm(Template::render("users/new", &BasicTemplateContext { - authenticity_token: yyyyy.authenticity_token, - layout: "site", - })) + let page_context = views::users::New { + authenticity_token: yyyyy.authenticity_token.to_string(), + }; + + let context = views::Site { + page: "users/new".to_string(), + page_context, + authenticity_token: yyyyy.authenticity_token.to_string(), + current_user: None, + }; + + Self::InvalidForm(Template::render("site", &context)) } } diff --git a/src/views.rs b/src/views.rs new file mode 100644 index 0000000..4073193 --- /dev/null +++ b/src/views.rs @@ -0,0 +1,39 @@ +use crate::models; + +use serde::ser::Serialize; + +#[derive(Serialize)] +pub struct Site { + pub page: String, + pub page_context: T, + pub authenticity_token: String, + pub current_user: Option, +} + +#[derive(Serialize)] +pub struct Error { + pub error_code: u16, +} + +pub mod home { + use crate::models; + + #[derive(Serialize)] + pub struct Index { + pub users: Vec, + } +} + +pub mod sessions { + #[derive(Serialize)] + pub struct New { + pub authenticity_token: String, + } +} + +pub mod users { + #[derive(Serialize)] + pub struct New { + pub authenticity_token: String, + } +} diff --git a/templates/error.html.hbs b/templates/error.html.hbs index ab2f5e0..d2a0ebd 100644 --- a/templates/error.html.hbs +++ b/templates/error.html.hbs @@ -1,5 +1 @@ -{{#*inline "page"}}

{{ error_code }}

-{{/inline}} - -{{> (layout)}} diff --git a/templates/home/index.html.hbs b/templates/home/index.html.hbs index 2ddf017..67bfe46 100644 --- a/templates/home/index.html.hbs +++ b/templates/home/index.html.hbs @@ -1,4 +1,3 @@ -{{#*inline "page"}}

Users

@@ -8,6 +7,3 @@ {{/each}}
-{{/inline}} - -{{> (layout)}} diff --git a/templates/sessions/new.html.hbs b/templates/sessions/new.html.hbs index c5ee532..0c32c81 100644 --- a/templates/sessions/new.html.hbs +++ b/templates/sessions/new.html.hbs @@ -1,4 +1,3 @@ -{{#*inline "page"}}

Sign In

@@ -20,6 +19,3 @@
-{{/inline}} - -{{> (layout)}} diff --git a/templates/site.html.hbs b/templates/site.html.hbs index 5786ffa..9d7416b 100644 --- a/templates/site.html.hbs +++ b/templates/site.html.hbs @@ -17,7 +17,7 @@ {{> navbar}} - {{> page}} + {{> (page) page_context}} diff --git a/templates/users/new.html.hbs b/templates/users/new.html.hbs index b0533ab..3c38dd6 100644 --- a/templates/users/new.html.hbs +++ b/templates/users/new.html.hbs @@ -1,4 +1,3 @@ -{{#*inline "page"}}

Sign Up

@@ -25,6 +24,3 @@
-{{/inline}} - -{{> (layout)}}