Merge pull request 'refactor: Used askama_axum::Template' (#51) from askama_axum_template into main

### Détails

Changement de `askama::Template` à `askama_axum::Template`.

### Pourquoi ?

Pour supprimer les `into_response()` et ainsi réduire les imports

### Documentation

https://djc.github.io/askama/integrations.html#axum-integration

Reviewed-on: #51
Reviewed-by: florian_briand <florian.briand@digital-engine.info>
This commit is contained in:
kosssi 2024-08-10 17:51:51 +02:00
commit 5269dd7789
6 changed files with 26 additions and 35 deletions

View File

@ -3,8 +3,7 @@ mod templates;
use std::path::Path; use std::path::Path;
use askama::Template; use askama_axum::Template;
use askama_axum::IntoResponse;
use axum::http::{StatusCode, Uri}; use axum::http::{StatusCode, Uri};
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
@ -14,10 +13,10 @@ async fn fallback(uri: Uri) -> (StatusCode, String) {
#[derive(Template)] #[derive(Template)]
#[template(path = "index.html")] #[template(path = "index.html")]
pub struct GetIndexResponse; pub struct GetIndexTemplate;
async fn root() -> impl IntoResponse { async fn root() -> GetIndexTemplate {
GetIndexResponse {}.into_response() GetIndexTemplate {}
} }
pub fn get_router(assets_path: &Path) -> axum::Router { pub fn get_router(assets_path: &Path) -> axum::Router {

View File

@ -1,10 +1,9 @@
use askama::Template; use askama_axum::Template;
use askama_axum::IntoResponse;
#[derive(Template)] #[derive(Template)]
#[template(path = "pages/cps.html")] #[template(path = "pages/cps.html")]
struct CpsResponse; pub struct CpsTemplate;
pub async fn cps() -> impl IntoResponse { pub async fn cps() -> CpsTemplate {
CpsResponse.into_response() CpsTemplate
} }

View File

@ -1,10 +1,9 @@
use askama::Template; use askama_axum::Template;
use askama_axum::IntoResponse;
#[derive(Template)] #[derive(Template)]
#[template(path = "pages/home.html")] #[template(path = "pages/home.html")]
struct HomeResponse; pub struct HomeTemplate;
pub async fn home() -> impl IntoResponse { pub async fn home() -> HomeTemplate {
HomeResponse.into_response() HomeTemplate
} }

View File

@ -1,18 +1,16 @@
use askama::Template; use askama_axum::Template;
use askama_axum::IntoResponse;
use axum::{routing, Router}; use axum::{routing, Router};
#[derive(Template)] #[derive(Template)]
#[template(path = "hello.html")] #[template(path = "hello.html")]
struct HelloResponse { struct HelloTemplate {
pub name: String, pub name: String,
} }
async fn hello() -> impl IntoResponse { async fn hello() -> HelloTemplate {
HelloResponse { HelloTemplate {
name: "Theo".to_string(), name: "Theo".to_string(),
} }
.into_response()
} }
pub fn get_routes() -> Router { pub fn get_routes() -> Router {

View File

@ -1,5 +1,4 @@
use askama::Template; use askama_axum::Template;
use askama_axum::IntoResponse;
use axum::{extract::Query, routing, Router}; use axum::{extract::Query, routing, Router};
use serde::Deserialize; use serde::Deserialize;
@ -16,12 +15,12 @@ struct MenuParameters {
#[derive(Template)] #[derive(Template)]
#[template(path = "layout/nav/nav-menu-items.html")] #[template(path = "layout/nav/nav-menu-items.html")]
struct MenuResponse { struct MenuTemplate {
mobile: bool, mobile: bool,
items: Vec<MenuItem>, items: Vec<MenuItem>,
} }
impl MenuResponse { impl MenuTemplate {
fn get_classes(&self, is_current_item: &bool) -> String { fn get_classes(&self, is_current_item: &bool) -> String {
let common_classes = match self.mobile { let common_classes = match self.mobile {
true => "block border-l-4 py-2 pl-3 pr-4 text-base font-medium".to_string(), 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<MenuParameters>) -> impl IntoResponse { async fn menu(Query(params): Query<MenuParameters>) -> MenuTemplate {
MenuResponse { MenuTemplate {
mobile: params.mobile, mobile: params.mobile,
items: vec![ items: vec![
MenuItem { MenuItem {
@ -54,7 +53,6 @@ async fn menu(Query(params): Query<MenuParameters>) -> impl IntoResponse {
}, },
], ],
} }
.into_response()
} }
pub fn get_routes() -> Router { pub fn get_routes() -> Router {

View File

@ -1,5 +1,4 @@
use askama::Template; use askama_axum::Template;
use askama_axum::IntoResponse;
use axum::{extract::Query, routing, Router}; use axum::{extract::Query, routing, Router};
use serde::Deserialize; use serde::Deserialize;
@ -16,12 +15,12 @@ struct MenuParameters {
#[derive(Template)] #[derive(Template)]
#[template(path = "layout/nav/profile-menu-items.html")] #[template(path = "layout/nav/profile-menu-items.html")]
struct MenuResponse { struct MenuTemplate {
mobile: bool, mobile: bool,
items: Vec<MenuItem>, items: Vec<MenuItem>,
} }
impl MenuResponse { impl MenuTemplate {
fn get_classes(&self, is_current_item: &bool) -> String { fn get_classes(&self, is_current_item: &bool) -> String {
let common_classes = match self.mobile { 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(), 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<MenuParameters>) -> impl IntoResponse { async fn menu(Query(params): Query<MenuParameters>) -> MenuTemplate {
MenuResponse { MenuTemplate {
mobile: params.mobile, mobile: params.mobile,
items: vec![ items: vec![
MenuItem { MenuItem {
@ -57,7 +56,6 @@ async fn menu(Query(params): Query<MenuParameters>) -> impl IntoResponse {
}, },
], ],
} }
.into_response()
} }
pub fn get_routes() -> Router { pub fn get_routes() -> Router {