From 90a446a576f8d5b5061c30b4b794a86c59475ac0 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 6 Aug 2023 18:29:31 +0200 Subject: [PATCH] Group base template parameters --- src/config.rs | 6 ------ src/web.rs | 27 ++++++++++++++++++++++++++- src/web/commit.rs | 10 ++++------ src/web/commit_hash.rs | 10 ++++------ src/web/index.rs | 10 ++++------ templates/base.html | 12 ++++++------ templates/index.html | 4 ++-- 7 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/config.rs b/src/config.rs index 7e95d23..5060035 100644 --- a/src/config.rs +++ b/src/config.rs @@ -40,12 +40,6 @@ impl Default for Repo { } } -impl Repo { - pub fn name(&self) -> String { - self.name.clone() - } -} - #[derive(Debug, Deserialize)] pub struct Web { #[serde(default = "default::web_base")] diff --git a/src/web.rs b/src/web.rs index 32516ad..82ea11d 100644 --- a/src/web.rs +++ b/src/web.rs @@ -5,7 +5,32 @@ mod r#static; use axum::{routing::get, Router, Server}; -use crate::{somehow, state::AppState}; +use crate::{config::Config, somehow, state::AppState}; + +pub enum Tab { + Index, + Commit, +} + +pub struct Base { + root: String, + repo_name: String, + current: String, +} + +impl Base { + pub fn new(config: &Config, tab: Tab) -> Self { + let current = match tab { + Tab::Index => "index", + Tab::Commit => "commit", + }; + Self { + root: config.web.base(), + repo_name: config.repo.name.clone(), + current: current.to_string(), + } + } +} pub async fn run(state: AppState) -> somehow::Result<()> { // TODO Add text body to body-less status codes diff --git a/src/web/commit.rs b/src/web/commit.rs index ef80231..a34aaac 100644 --- a/src/web/commit.rs +++ b/src/web/commit.rs @@ -3,18 +3,16 @@ use axum::{extract::State, response::IntoResponse}; use crate::{config::Config, somehow}; +use super::{Base, Tab}; + #[derive(Template)] #[template(path = "commit.html")] struct CommitTemplate { - base: String, - repo_name: String, - current: &'static str, + base: Base, } pub async fn get(State(config): State<&'static Config>) -> somehow::Result { Ok(CommitTemplate { - base: config.web.base(), - repo_name: config.repo.name(), - current: "commit", + base: Base::new(config, Tab::Commit), }) } diff --git a/src/web/commit_hash.rs b/src/web/commit_hash.rs index ab1448f..6a22cea 100644 --- a/src/web/commit_hash.rs +++ b/src/web/commit_hash.rs @@ -9,6 +9,8 @@ use sqlx::SqlitePool; use crate::{config::Config, somehow, util}; +use super::{Base, Tab}; + struct Commit { hash: String, short: String, @@ -28,9 +30,7 @@ impl Commit { #[derive(Template)] #[template(path = "commit_hash.html")] struct CommitIdTemplate { - base: String, - repo_name: String, - current: &'static str, + base: Base, hash: String, author: String, author_date: String, @@ -98,9 +98,7 @@ pub async fn get( .await?; Ok(CommitIdTemplate { - base: config.web.base(), - repo_name: config.repo.name(), - current: "commit", + base: Base::new(config, Tab::Commit), hash: commit.hash, author: commit.author, author_date: util::format_time(commit.author_date)?, diff --git a/src/web/index.rs b/src/web/index.rs index a540989..b1177dc 100644 --- a/src/web/index.rs +++ b/src/web/index.rs @@ -5,6 +5,8 @@ use sqlx::SqlitePool; use crate::{config::Config, somehow, util}; +use super::{Base, Tab}; + struct Ref { name: String, hash: String, @@ -15,9 +17,7 @@ struct Ref { #[derive(Template)] #[template(path = "index.html")] struct IndexTemplate { - base: String, - repo_name: String, - current: &'static str, + base: Base, tracked_refs: Vec, untracked_refs: Vec, } @@ -54,9 +54,7 @@ pub async fn get( } Ok(IndexTemplate { - base: config.web.base(), - repo_name: config.repo.name(), - current: "index", + base: Base::new(config, Tab::Index), tracked_refs, untracked_refs, }) diff --git a/templates/base.html b/templates/base.html index 880eb57..1da4130 100644 --- a/templates/base.html +++ b/templates/base.html @@ -3,19 +3,19 @@ - {% block title %}{% endblock %} - {{ repo_name }} + {% block title %}{% endblock %} - {{ base.repo_name }} - + {% block head %}{% endblock %} diff --git a/templates/index.html b/templates/index.html index 2535cfc..0e077b0 100644 --- a/templates/index.html +++ b/templates/index.html @@ -9,7 +9,7 @@
{% for ref in tracked_refs %}
{{ ref.name }}
-
{{ ref.short }}
+
{{ ref.short }}
{% endfor %}
@@ -18,7 +18,7 @@
{% for ref in untracked_refs %}
{{ ref.name }}
-
{{ ref.short }}
+
{{ ref.short }}
{% endfor %}