From c3f97564d6a1e3b7e773d9fb7478d5ccd7bde85e Mon Sep 17 00:00:00 2001 From: Simon C Date: Sat, 10 Aug 2024 16:59:36 +0200 Subject: [PATCH] refactor: Used askama_axum::Template docs: https://djc.github.io/askama/integrations.html#axum-integration --- crates/app/src/lib.rs | 9 ++++----- crates/app/src/pages/cps.rs | 9 ++++----- crates/app/src/pages/home.rs | 9 ++++----- crates/app/src/templates/hello.rs | 10 ++++------ crates/app/src/templates/nav.rs | 12 +++++------- crates/app/src/templates/profile.rs | 12 +++++------- 6 files changed, 26 insertions(+), 35 deletions(-) diff --git a/crates/app/src/lib.rs b/crates/app/src/lib.rs index 1757b88..8bdad03 100644 --- a/crates/app/src/lib.rs +++ b/crates/app/src/lib.rs @@ -3,8 +3,7 @@ mod templates; use std::path::Path; -use askama::Template; -use askama_axum::IntoResponse; +use askama_axum::Template; use axum::http::{StatusCode, Uri}; use tower_http::services::ServeDir; @@ -14,10 +13,10 @@ async fn fallback(uri: Uri) -> (StatusCode, String) { #[derive(Template)] #[template(path = "index.html")] -pub struct GetIndexResponse; +pub struct GetIndexTemplate; -async fn root() -> impl IntoResponse { - GetIndexResponse {}.into_response() +async fn root() -> GetIndexTemplate { + GetIndexTemplate {} } pub fn get_router(assets_path: &Path) -> axum::Router { diff --git a/crates/app/src/pages/cps.rs b/crates/app/src/pages/cps.rs index 85c3f8a..bee64c5 100644 --- a/crates/app/src/pages/cps.rs +++ b/crates/app/src/pages/cps.rs @@ -1,10 +1,9 @@ -use askama::Template; -use askama_axum::IntoResponse; +use askama_axum::Template; #[derive(Template)] #[template(path = "pages/cps.html")] -struct CpsResponse; +pub struct CpsTemplate; -pub async fn cps() -> impl IntoResponse { - CpsResponse.into_response() +pub async fn cps() -> CpsTemplate { + CpsTemplate } diff --git a/crates/app/src/pages/home.rs b/crates/app/src/pages/home.rs index c2d9c91..791c63f 100644 --- a/crates/app/src/pages/home.rs +++ b/crates/app/src/pages/home.rs @@ -1,10 +1,9 @@ -use askama::Template; -use askama_axum::IntoResponse; +use askama_axum::Template; #[derive(Template)] #[template(path = "pages/home.html")] -struct HomeResponse; +pub struct HomeTemplate; -pub async fn home() -> impl IntoResponse { - HomeResponse.into_response() +pub async fn home() -> HomeTemplate { + HomeTemplate } diff --git a/crates/app/src/templates/hello.rs b/crates/app/src/templates/hello.rs index 7c35118..58ae568 100644 --- a/crates/app/src/templates/hello.rs +++ b/crates/app/src/templates/hello.rs @@ -1,18 +1,16 @@ -use askama::Template; -use askama_axum::IntoResponse; +use askama_axum::Template; use axum::{routing, Router}; #[derive(Template)] #[template(path = "hello.html")] -struct HelloResponse { +struct HelloTemplate { pub name: String, } -async fn hello() -> impl IntoResponse { - HelloResponse { +async fn hello() -> HelloTemplate { + HelloTemplate { name: "Theo".to_string(), } - .into_response() } pub fn get_routes() -> Router { diff --git a/crates/app/src/templates/nav.rs b/crates/app/src/templates/nav.rs index e94dd0d..9bb44cc 100644 --- a/crates/app/src/templates/nav.rs +++ b/crates/app/src/templates/nav.rs @@ -1,5 +1,4 @@ -use askama::Template; -use askama_axum::IntoResponse; +use askama_axum::Template; use axum::{extract::Query, routing, Router}; use serde::Deserialize; @@ -16,12 +15,12 @@ struct MenuParameters { #[derive(Template)] #[template(path = "layout/nav/nav-menu-items.html")] -struct MenuResponse { +struct MenuTemplate { mobile: bool, items: Vec, } -impl MenuResponse { +impl MenuTemplate { fn get_classes(&self, is_current_item: &bool) -> String { let common_classes = match self.mobile { true => "block border-l-4 py-2 pl-3 pr-4 text-base font-medium".to_string(), @@ -38,8 +37,8 @@ impl MenuResponse { } } -async fn menu(Query(params): Query) -> impl IntoResponse { - MenuResponse { +async fn menu(Query(params): Query) -> MenuTemplate { + MenuTemplate { mobile: params.mobile, items: vec![ MenuItem { @@ -54,7 +53,6 @@ async fn menu(Query(params): Query) -> impl IntoResponse { }, ], } - .into_response() } pub fn get_routes() -> Router { diff --git a/crates/app/src/templates/profile.rs b/crates/app/src/templates/profile.rs index 3fc268f..60a915a 100644 --- a/crates/app/src/templates/profile.rs +++ b/crates/app/src/templates/profile.rs @@ -1,5 +1,4 @@ -use askama::Template; -use askama_axum::IntoResponse; +use askama_axum::Template; use axum::{extract::Query, routing, Router}; use serde::Deserialize; @@ -16,12 +15,12 @@ struct MenuParameters { #[derive(Template)] #[template(path = "layout/nav/profile-menu-items.html")] -struct MenuResponse { +struct MenuTemplate { mobile: bool, items: Vec, } -impl MenuResponse { +impl MenuTemplate { fn get_classes(&self, is_current_item: &bool) -> String { let common_classes = match self.mobile { true => "block px-4 py-2 text-base font-medium text-gray-500 hover:bg-gray-100 hover:text-gray-800".to_string(), @@ -36,8 +35,8 @@ impl MenuResponse { } } -async fn menu(Query(params): Query) -> impl IntoResponse { - MenuResponse { +async fn menu(Query(params): Query) -> MenuTemplate { + MenuTemplate { mobile: params.mobile, items: vec![ MenuItem { @@ -57,7 +56,6 @@ async fn menu(Query(params): Query) -> impl IntoResponse { }, ], } - .into_response() } pub fn get_routes() -> Router { -- 2.45.2