aboutsummaryrefslogtreecommitdiff
path: root/src/controller
diff options
context:
space:
mode:
authorJoris2025-01-26 17:58:57 +0100
committerJoris2025-01-26 17:58:57 +0100
commit24eeb54a6b7159964e8887ade7fa5173b50feb3a (patch)
tree91af6253df784445db9b084b02b38b37a83224e8 /src/controller
parentc5759f348e70cf54b4bfa4cd17e1fe1828ead30a (diff)
Replace tera by minijinjamain
tera was doing the job all right, but minijinja has fewer dependencies.
Diffstat (limited to 'src/controller')
-rw-r--r--src/controller/balance.rs21
-rw-r--r--src/controller/categories.rs40
-rw-r--r--src/controller/error.rs14
-rw-r--r--src/controller/incomes.rs56
-rw-r--r--src/controller/login.rs21
-rw-r--r--src/controller/payments.rs62
-rw-r--r--src/controller/statistics.rs15
-rw-r--r--src/controller/utils.rs39
-rw-r--r--src/controller/wallet.rs3
9 files changed, 144 insertions, 127 deletions
diff --git a/src/controller/balance.rs b/src/controller/balance.rs
index c5d9d4a..efe5af6 100644
--- a/src/controller/balance.rs
+++ b/src/controller/balance.rs
@@ -2,7 +2,6 @@ use http_body_util::Full;
use hyper::body::Bytes;
use hyper::Response;
use std::collections::HashMap;
-use tera::Context;
use crate::controller::utils;
use crate::controller::wallet::Wallet;
@@ -31,18 +30,16 @@ pub async fn get(wallet: &Wallet) -> Response<Full<Bytes>> {
let exceeding_payers =
payer::exceeding(&users, &user_incomes, &user_payments);
- let mut context = Context::new();
- context.insert("header", &templates::Header::Balance);
- context.insert("connected_user", &wallet.user);
- context.insert(
- "incomes_from",
- &incomes_from.map(|d| d.format("%d/%m/%Y").to_string()),
+ let context = minijinja::context!(
+ header => templates::Header::Balance,
+ connected_user => wallet.user,
+ incomes_from => incomes_from.map(|d| d.format("%d/%m/%Y").to_string()),
+ total_income => total_income,
+ user_incomes => template_user_incomes,
+ total_payments => total_payments,
+ user_payments => template_user_payments,
+ exceeding_payers => exceeding_payers
);
- context.insert("total_income", &total_income);
- context.insert("user_incomes", &template_user_incomes);
- context.insert("total_payments", &total_payments);
- context.insert("user_payments", &template_user_payments);
- context.insert("exceeding_payers", &exceeding_payers);
utils::template(&wallet.assets, &wallet.templates, "balance.html", context)
}
diff --git a/src/controller/categories.rs b/src/controller/categories.rs
index ff2d8e7..fbbd309 100644
--- a/src/controller/categories.rs
+++ b/src/controller/categories.rs
@@ -2,7 +2,6 @@ use http_body_util::Full;
use hyper::body::Bytes;
use hyper::Response;
use std::collections::HashMap;
-use tera::Context;
use crate::controller::utils;
use crate::controller::wallet::Wallet;
@@ -17,11 +16,12 @@ pub async fn table(
) -> Response<Full<Bytes>> {
let categories = db::categories::list(&wallet.pool).await;
- let mut context = Context::new();
- context.insert("header", &templates::Header::Categories);
- context.insert("connected_user", &wallet.user);
- context.insert("categories", &categories);
- context.insert("highlight", &query.highlight);
+ let context = minijinja::context!(
+ header => templates::Header::Categories,
+ connected_user => wallet.user,
+ categories => categories,
+ highlight => query.highlight
+ );
utils::template(
&wallet.assets,
@@ -40,11 +40,12 @@ async fn create_form_feedback(
form: HashMap<String, String>,
error: Option<String>,
) -> Response<Full<Bytes>> {
- let mut context = Context::new();
- context.insert("header", &templates::Header::Categories);
- context.insert("connected_user", &wallet.user.clone());
- context.insert("form", &form);
- context.insert("error", &error);
+ let context = minijinja::context!(
+ header => &templates::Header::Categories,
+ connected_user => &wallet.user.clone(),
+ form => &form,
+ error => &error
+ );
utils::template(
&wallet.assets,
@@ -89,14 +90,15 @@ async fn update_form_feedback(
let is_category_used =
db::payments::is_category_used(&wallet.pool, id).await;
- let mut context = Context::new();
- context.insert("header", &templates::Header::Categories);
- context.insert("connected_user", &wallet.user);
- context.insert("id", &id);
- context.insert("category", &category);
- context.insert("is_category_used", &is_category_used);
- context.insert("form", &form);
- context.insert("error", &error);
+ let context = minijinja::context!(
+ header => templates::Header::Categories,
+ connected_user => wallet.user,
+ id => id,
+ category => category,
+ is_category_used => is_category_used,
+ form => form,
+ error => error
+ );
utils::template(
&wallet.assets,
diff --git a/src/controller/error.rs b/src/controller/error.rs
index 0f6dcc1..fb1375c 100644
--- a/src/controller/error.rs
+++ b/src/controller/error.rs
@@ -3,7 +3,6 @@ use hyper::body::Bytes;
use hyper::header::CACHE_CONTROL;
use hyper::Response;
use std::collections::HashMap;
-use tera::{Context, Tera};
use crate::controller::utils;
use crate::controller::wallet::Wallet;
@@ -24,14 +23,15 @@ pub fn error(
pub fn template(
assets: &HashMap<String, String>,
- templates: &Tera,
+ templates: &minijinja::Environment<'_>,
title: &str,
message: &str,
) -> String {
- let mut context = Context::new();
- context.insert("title", title);
- context.insert("message", message);
- context.insert("assets", assets);
+ let context = minijinja::context!(
+ title => title,
+ message => message,
+ assets => assets
+ );
- templates.render("error.html", &context).unwrap()
+ templates.render_str("error.html", &context).unwrap()
}
diff --git a/src/controller/incomes.rs b/src/controller/incomes.rs
index f22098b..ac3a332 100644
--- a/src/controller/incomes.rs
+++ b/src/controller/incomes.rs
@@ -4,7 +4,6 @@ use http_body_util::Full;
use hyper::body::Bytes;
use hyper::Response;
use std::collections::HashMap;
-use tera::Context;
use crate::controller::utils;
use crate::controller::wallet::Wallet;
@@ -24,13 +23,14 @@ pub async fn table(
let incomes = db::incomes::list(&wallet.pool, page, PER_PAGE).await;
let max_page = (count as f32 / PER_PAGE as f32).ceil() as i64;
- let mut context = Context::new();
- context.insert("header", &templates::Header::Incomes);
- context.insert("connected_user", &wallet.user);
- context.insert("incomes", &incomes);
- context.insert("page", &page);
- context.insert("max_page", &max_page);
- context.insert("highlight", &query.highlight);
+ let context = minijinja::context!(
+ header => templates::Header::Incomes,
+ connected_user => wallet.user,
+ incomes => incomes,
+ page => page,
+ max_page => max_page,
+ highlight => query.highlight
+ );
utils::template(
&wallet.assets,
@@ -70,15 +70,16 @@ async fn create_form_feedback(
) -> Response<Full<Bytes>> {
let users = db::users::list(&wallet.pool).await;
- let mut context = Context::new();
- context.insert("header", &templates::Header::Incomes);
- context.insert("connected_user", &wallet.user);
- context.insert("users", &users);
- context.insert("query", &query);
- context.insert("current_month", &Utc::now().date_naive().month());
- context.insert("months", &MONTHS);
- context.insert("form", &form);
- context.insert("error", &error);
+ let context = minijinja::context!(
+ header => templates::Header::Incomes,
+ connected_user => wallet.user,
+ users => users,
+ query => query,
+ current_month => Utc::now().date_naive().month(),
+ months => MONTHS,
+ form => form,
+ error => error,
+ );
utils::template(
&wallet.assets,
@@ -144,16 +145,17 @@ async fn update_form_feedback(
let users = db::users::list(&wallet.pool).await;
let income = db::incomes::get(&wallet.pool, id).await;
- let mut context = Context::new();
- context.insert("header", &templates::Header::Incomes);
- context.insert("connected_user", &wallet.user);
- context.insert("users", &users);
- context.insert("id", &id);
- context.insert("income", &income);
- context.insert("query", &query);
- context.insert("months", &MONTHS);
- context.insert("form", &form);
- context.insert("error", &error);
+ let context = minijinja::context!(
+ header => &templates::Header::Incomes,
+ connected_user => &wallet.user,
+ users => &users,
+ id => &id,
+ income => &income,
+ query => &query,
+ months => &MONTHS,
+ form => &form,
+ error => &error
+ );
utils::template(
&wallet.assets,
diff --git a/src/controller/login.rs b/src/controller/login.rs
index a1bf466..31370cc 100644
--- a/src/controller/login.rs
+++ b/src/controller/login.rs
@@ -5,7 +5,6 @@ use hyper::header::SET_COOKIE;
use hyper::Response;
use sqlx::sqlite::SqlitePool;
use std::collections::HashMap;
-use tera::{Context, Tera};
use crate::controller::utils::with_headers;
use crate::controller::wallet::Wallet;
@@ -18,14 +17,15 @@ use crate::validation;
pub async fn page(
assets: &HashMap<String, String>,
- templates: &Tera,
+ templates: &minijinja::Environment<'_>,
error: Option<&str>,
) -> Response<Full<Bytes>> {
let connected_user: Option<User> = None;
- let mut context = Context::new();
- context.insert("connected_user", &connected_user);
- context.insert("error", &error);
+ let context = minijinja::context!(
+ connected_user => &connected_user,
+ error => &error
+ );
utils::template(assets, templates, "login.html", context)
}
@@ -33,7 +33,7 @@ pub async fn page(
pub async fn login(
config: &Config,
assets: &HashMap<String, String>,
- templates: &Tera,
+ templates: &minijinja::Environment<'_>,
form: HashMap<String, String>,
pool: SqlitePool,
) -> Response<Full<Bytes>> {
@@ -75,7 +75,10 @@ pub async fn login(
}
Ok(false) => not_authorized(assets, templates).await,
Err(err) => {
- log::error!("Error verifying bcrypt password: {:?}", err);
+ log::error!(
+ "Error verifying bcrypt password: {:?}",
+ err
+ );
server_error(assets, templates, "Erreur serveur").await
}
},
@@ -88,7 +91,7 @@ pub async fn login(
async fn server_error(
assets: &HashMap<String, String>,
- templates: &Tera,
+ templates: &minijinja::Environment<'_>,
msg: &str,
) -> Response<Full<Bytes>> {
page(assets, templates, Some(msg)).await
@@ -96,7 +99,7 @@ async fn server_error(
async fn not_authorized(
assets: &HashMap<String, String>,
- templates: &Tera,
+ templates: &minijinja::Environment<'_>,
) -> Response<Full<Bytes>> {
page(
assets,
diff --git a/src/controller/payments.rs b/src/controller/payments.rs
index 8184015..b5c0256 100644
--- a/src/controller/payments.rs
+++ b/src/controller/payments.rs
@@ -3,7 +3,6 @@ use hyper::body::Bytes;
use hyper::header::CONTENT_TYPE;
use hyper::Response;
use std::collections::HashMap;
-use tera::Context;
use crate::controller::utils;
use crate::controller::wallet::Wallet;
@@ -27,17 +26,18 @@ pub async fn table(
let users = db::users::list(&wallet.pool).await;
let categories = db::categories::list(&wallet.pool).await;
- let mut context = Context::new();
- context.insert("header", &templates::Header::Payments);
- context.insert("connected_user", &wallet.user);
- context.insert("payments", &payments);
- context.insert("page", &page);
- context.insert("max_page", &max_page);
- context.insert("query", &query);
- context.insert("count", &count.count);
- context.insert("total_cost", &count.total_cost);
- context.insert("users", &users);
- context.insert("categories", &categories);
+ let context = minijinja::context!(
+ header => templates::Header::Payments,
+ connected_user => wallet.user,
+ payments => payments,
+ page => page,
+ max_page => max_page,
+ query => query,
+ count => count.count,
+ total_cost => count.total_cost,
+ users => users,
+ categories => categories
+ );
utils::template(
&wallet.assets,
@@ -63,14 +63,15 @@ async fn create_form_feedback(
let users = db::users::list(&wallet.pool).await;
let categories = db::categories::list(&wallet.pool).await;
- let mut context = Context::new();
- context.insert("header", &templates::Header::Payments);
- context.insert("connected_user", &wallet.user);
- context.insert("users", &users);
- context.insert("categories", &categories);
- context.insert("query", &query);
- context.insert("form", &form);
- context.insert("error", &error);
+ let context = minijinja::context!(
+ header => templates::Header::Payments,
+ connected_user => wallet.user,
+ users => users,
+ categories => categories,
+ query => query,
+ form => form,
+ error => error
+ );
utils::template(
&wallet.assets,
@@ -142,16 +143,17 @@ async fn update_form_feedback(
let users = db::users::list(&wallet.pool).await;
let categories = db::categories::list(&wallet.pool).await;
- let mut context = Context::new();
- context.insert("header", &templates::Header::Payments);
- context.insert("connected_user", &wallet.user);
- context.insert("id", &id);
- context.insert("payment", &payment);
- context.insert("users", &users);
- context.insert("categories", &categories);
- context.insert("query", &query);
- context.insert("form", &form);
- context.insert("error", &error);
+ let context = minijinja::context!(
+ header => templates::Header::Payments,
+ connected_user => wallet.user,
+ id => id,
+ payment => payment,
+ users => users,
+ categories => categories,
+ query => query,
+ form => form,
+ error => error
+ );
utils::template(
&wallet.assets,
diff --git a/src/controller/statistics.rs b/src/controller/statistics.rs
index eb1e704..e57e2be 100644
--- a/src/controller/statistics.rs
+++ b/src/controller/statistics.rs
@@ -1,7 +1,6 @@
use http_body_util::Full;
use hyper::body::Bytes;
use hyper::Response;
-use tera::Context;
use crate::controller::utils;
use crate::controller::wallet::Wallet;
@@ -13,15 +12,13 @@ pub async fn get(wallet: &Wallet) -> Response<Full<Bytes>> {
let payments = db::payments::list_for_stats(&wallet.pool).await;
let incomes = db::incomes::total_each_month(&wallet.pool).await;
- let mut context = Context::new();
- context.insert("header", &templates::Header::Statistics);
- context.insert("connected_user", &wallet.user);
- context.insert(
- "json_categories",
- &serde_json::to_string(&categories).unwrap(),
+ let context = minijinja::context!(
+ header => templates::Header::Statistics,
+ connected_user => wallet.user,
+ json_categories => serde_json::to_string(&categories).unwrap(),
+ json_payments => serde_json::to_string(&payments).unwrap(),
+ json_incomes => serde_json::to_string(&incomes).unwrap()
);
- context.insert("json_payments", &serde_json::to_string(&payments).unwrap());
- context.insert("json_incomes", &serde_json::to_string(&incomes).unwrap());
utils::template(
&wallet.assets,
diff --git a/src/controller/utils.rs b/src/controller/utils.rs
index 1b58c68..340a5c7 100644
--- a/src/controller/utils.rs
+++ b/src/controller/utils.rs
@@ -5,7 +5,6 @@ use hyper::header::{
};
use hyper::{Response, StatusCode};
use std::collections::HashMap;
-use tera::{Context, Tera};
use crate::controller::error;
@@ -23,29 +22,45 @@ pub fn with_headers(
pub fn template(
assets: &HashMap<String, String>,
- templates: &Tera,
+ templates: &minijinja::Environment<'_>,
path: &str,
- context: Context,
+ context: minijinja::Value,
) -> Response<Full<Bytes>> {
- let mut context = context;
- context.insert("assets", assets);
+ let context = minijinja::context! { ..context, ..minijinja::context! {
+ assets => assets
+ }};
- match templates.render(path, &context) {
+ match render_template(templates, path, context) {
Ok(template) => with_headers(
Response::new(template.into()),
vec![(CONTENT_TYPE, "text/html"), (CACHE_CONTROL, "no-cache")],
),
- Err(err) => server_error(
- assets,
- templates,
- &format!("Erreur lors de la préparation de la page : {:?}", err),
- ),
+ Err(err) => {
+ log::error!("ERROR template rendering {}\n{:?}", path, err);
+ server_error(
+ assets,
+ templates,
+ &format!(
+ "Erreur lors de la préparation de la page : {:?}",
+ err
+ ),
+ )
+ }
}
}
+fn render_template(
+ templates: &minijinja::Environment<'_>,
+ name: &str,
+ context: minijinja::Value,
+) -> Result<String, minijinja::Error> {
+ let template = templates.get_template(name)?;
+ template.render(context)
+}
+
fn server_error(
assets: &HashMap<String, String>,
- templates: &Tera,
+ templates: &minijinja::Environment<'_>,
msg: &str,
) -> Response<Full<Bytes>> {
with_headers(
diff --git a/src/controller/wallet.rs b/src/controller/wallet.rs
index 2a4a593..7537406 100644
--- a/src/controller/wallet.rs
+++ b/src/controller/wallet.rs
@@ -1,6 +1,5 @@
use sqlx::sqlite::SqlitePool;
use std::collections::HashMap;
-use tera::Tera;
use crate::model::user::User;
@@ -8,6 +7,6 @@ use crate::model::user::User;
pub struct Wallet {
pub pool: SqlitePool,
pub assets: HashMap<String, String>,
- pub templates: Tera,
+ pub templates: minijinja::Environment<'static>,
pub user: User,
}