Group base template parameters

This commit is contained in:
Joscha 2023-08-06 18:29:31 +02:00
parent 729b3ba672
commit 90a446a576
7 changed files with 46 additions and 33 deletions

View file

@ -40,12 +40,6 @@ impl Default for Repo {
} }
} }
impl Repo {
pub fn name(&self) -> String {
self.name.clone()
}
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Web { pub struct Web {
#[serde(default = "default::web_base")] #[serde(default = "default::web_base")]

View file

@ -5,7 +5,32 @@ mod r#static;
use axum::{routing::get, Router, Server}; 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<()> { pub async fn run(state: AppState) -> somehow::Result<()> {
// TODO Add text body to body-less status codes // TODO Add text body to body-less status codes

View file

@ -3,18 +3,16 @@ use axum::{extract::State, response::IntoResponse};
use crate::{config::Config, somehow}; use crate::{config::Config, somehow};
use super::{Base, Tab};
#[derive(Template)] #[derive(Template)]
#[template(path = "commit.html")] #[template(path = "commit.html")]
struct CommitTemplate { struct CommitTemplate {
base: String, base: Base,
repo_name: String,
current: &'static str,
} }
pub async fn get(State(config): State<&'static Config>) -> somehow::Result<impl IntoResponse> { pub async fn get(State(config): State<&'static Config>) -> somehow::Result<impl IntoResponse> {
Ok(CommitTemplate { Ok(CommitTemplate {
base: config.web.base(), base: Base::new(config, Tab::Commit),
repo_name: config.repo.name(),
current: "commit",
}) })
} }

View file

@ -9,6 +9,8 @@ use sqlx::SqlitePool;
use crate::{config::Config, somehow, util}; use crate::{config::Config, somehow, util};
use super::{Base, Tab};
struct Commit { struct Commit {
hash: String, hash: String,
short: String, short: String,
@ -28,9 +30,7 @@ impl Commit {
#[derive(Template)] #[derive(Template)]
#[template(path = "commit_hash.html")] #[template(path = "commit_hash.html")]
struct CommitIdTemplate { struct CommitIdTemplate {
base: String, base: Base,
repo_name: String,
current: &'static str,
hash: String, hash: String,
author: String, author: String,
author_date: String, author_date: String,
@ -98,9 +98,7 @@ pub async fn get(
.await?; .await?;
Ok(CommitIdTemplate { Ok(CommitIdTemplate {
base: config.web.base(), base: Base::new(config, Tab::Commit),
repo_name: config.repo.name(),
current: "commit",
hash: commit.hash, hash: commit.hash,
author: commit.author, author: commit.author,
author_date: util::format_time(commit.author_date)?, author_date: util::format_time(commit.author_date)?,

View file

@ -5,6 +5,8 @@ use sqlx::SqlitePool;
use crate::{config::Config, somehow, util}; use crate::{config::Config, somehow, util};
use super::{Base, Tab};
struct Ref { struct Ref {
name: String, name: String,
hash: String, hash: String,
@ -15,9 +17,7 @@ struct Ref {
#[derive(Template)] #[derive(Template)]
#[template(path = "index.html")] #[template(path = "index.html")]
struct IndexTemplate { struct IndexTemplate {
base: String, base: Base,
repo_name: String,
current: &'static str,
tracked_refs: Vec<Ref>, tracked_refs: Vec<Ref>,
untracked_refs: Vec<Ref>, untracked_refs: Vec<Ref>,
} }
@ -54,9 +54,7 @@ pub async fn get(
} }
Ok(IndexTemplate { Ok(IndexTemplate {
base: config.web.base(), base: Base::new(config, Tab::Index),
repo_name: config.repo.name(),
current: "index",
tracked_refs, tracked_refs,
untracked_refs, untracked_refs,
}) })

View file

@ -3,19 +3,19 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>{% block title %}{% endblock %} - {{ repo_name }}</title> <title>{% block title %}{% endblock %} - {{ base.repo_name }}</title>
<link rel="icon" href="/logo.svg"> <link rel="icon" href="/logo.svg">
<link rel="stylesheet" href="{{ base }}/base.css" /> <link rel="stylesheet" href="{{ base.root }}/base.css" />
{% block head %}{% endblock %} {% block head %}{% endblock %}
</head> </head>
<body> <body>
<nav> <nav>
<a href="{{ base }}/" {% if current=="index" %} class="current" {% endif %}> <a href="{{ base.root }}/" {% if base.current=="index" %} class="current" {% endif %}>
<img src="{{ base }}/logo.svg"> <img src="{{ base.root }}/logo.svg">
{{ repo_name }} {{ base.repo_name }}
</a> </a>
<a href="{{ base }}/commit/" {% if current=="commit" %} class="current" {% endif %}> <a href="{{ base.root }}/commit/" {% if base.current=="commit" %} class="current" {% endif %}>
commit commit
</a> </a>
</nav> </nav>

View file

@ -9,7 +9,7 @@
<dl> <dl>
{% for ref in tracked_refs %} {% for ref in tracked_refs %}
<dt>{{ ref.name }}</dt> <dt>{{ ref.name }}</dt>
<dd><a href="{{ base }}/commit/{{ ref.hash }}">{{ ref.short }}</a></dd> <dd><a href="{{ base.root }}/commit/{{ ref.hash }}">{{ ref.short }}</a></dd>
{% endfor %} {% endfor %}
</dl> </dl>
</details> </details>
@ -18,7 +18,7 @@
<dl> <dl>
{% for ref in untracked_refs %} {% for ref in untracked_refs %}
<dt>{{ ref.name }}</dt> <dt>{{ ref.name }}</dt>
<dd><a href="{{ base }}/commit/{{ ref.hash }}">{{ ref.short }}</a></dd> <dd><a href="{{ base.root }}/commit/{{ ref.hash }}">{{ ref.short }}</a></dd>
{% endfor %} {% endfor %}
</dl> </dl>
</details> </details>