Make most links typesafe

This commit is contained in:
Joscha 2023-08-13 20:24:37 +02:00
parent 30ddf1e9b2
commit e64ea7ac12
10 changed files with 82 additions and 100 deletions

View file

@ -2,6 +2,8 @@ use std::fmt;
use crate::config::Config;
use super::paths::{PathIndex, PathQueue};
pub enum Tab {
None,
Index,
@ -10,9 +12,13 @@ pub enum Tab {
#[derive(Clone)]
pub struct Base {
web_base: String,
repo_name: String,
tab: &'static str,
pub link_logo_svg: Link,
pub link_base_css: Link,
pub link_index: Link,
pub link_queue: Link,
pub web_base: String,
pub repo_name: String,
pub tab: &'static str,
}
impl Base {
@ -23,20 +29,29 @@ impl Base {
Tab::Queue => "queue",
};
Self {
link_logo_svg: Self::link_from_base(&config.web_base, "/logo.svg"), // TODO Static link
link_base_css: Self::link_from_base(&config.web_base, "/base.css"), // TODO Static link
link_index: Self::link_from_base(&config.web_base, PathIndex {}),
link_queue: Self::link_from_base(&config.web_base, PathQueue {}),
web_base: config.web_base.clone(),
repo_name: config.repo_name.clone(),
tab,
}
}
pub fn link<P: fmt::Display>(&self, to: P) -> Link {
fn link_from_base<P: fmt::Display>(base: &str, to: P) -> Link {
let to = format!("{to}");
assert!(!self.web_base.ends_with('/'));
assert!(!base.ends_with('/'));
assert!(to.starts_with('/'));
Link(format!("{}{to}", self.web_base))
Link(format!("{base}{to}"))
}
pub fn link<P: fmt::Display>(&self, to: P) -> Link {
Self::link_from_base(&self.web_base, to)
}
}
#[derive(Clone)]
pub struct Link(String);
impl fmt::Display for Link {

View file

@ -10,26 +10,26 @@ use sqlx::SqlitePool;
use crate::{config::Config, server::util, somehow};
use super::{
link::CommitLink,
base::{Base, Link, Tab},
link::LinkCommit,
paths::{PathAdminQueueAdd, PathCommitByHash},
Base, Tab,
};
#[derive(Template)]
#[template(path = "commit.html")]
struct CommitTemplate {
link_admin_queue_add: Link,
base: Base,
hash: String,
author: String,
author_date: String,
commit: String,
commit_date: String,
parents: Vec<CommitLink>,
children: Vec<CommitLink>,
parents: Vec<LinkCommit>,
children: Vec<LinkCommit>,
summary: String,
message: String,
reachable: i64,
link_admin_queue_add: PathAdminQueueAdd,
}
pub async fn get_commit_by_hash(
@ -70,7 +70,7 @@ pub async fn get_commit_by_hash(
path.hash,
)
.fetch(&db)
.map_ok(|r| CommitLink::new(&base, r.hash, &r.message, r.reachable))
.map_ok(|r| LinkCommit::new(&base, r.hash, &r.message, r.reachable))
.try_collect::<Vec<_>>()
.await?;
@ -84,11 +84,12 @@ pub async fn get_commit_by_hash(
path.hash,
)
.fetch(&db)
.map_ok(|r| CommitLink::new(&base, r.hash, &r.message, r.reachable))
.map_ok(|r| LinkCommit::new(&base, r.hash, &r.message, r.reachable))
.try_collect::<Vec<_>>()
.await?;
Ok(CommitTemplate {
link_admin_queue_add: base.link(PathAdminQueueAdd {}),
base,
hash: commit.hash,
author: commit.author,
@ -100,7 +101,6 @@ pub async fn get_commit_by_hash(
summary: util::format_commit_summary(&commit.message),
message: commit.message.trim_end().to_string(),
reachable: commit.reachable,
link_admin_queue_add: PathAdminQueueAdd {},
}
.into_response())
}

View file

@ -5,11 +5,15 @@ use sqlx::SqlitePool;
use crate::{config::Config, somehow};
use super::{link::CommitLink, paths::PathIndex, Base, Tab};
use super::{
base::{Base, Tab},
link::LinkCommit,
paths::PathIndex,
};
struct Ref {
name: String,
commit: CommitLink,
commit: LinkCommit,
tracked: bool,
}
@ -39,7 +43,7 @@ pub async fn get_index(
.fetch(&db)
.map_ok(|r| Ref {
name: r.name.clone(),
commit: CommitLink::new(&base, r.hash, &r.message, r.reachable),
commit: LinkCommit::new(&base, r.hash, &r.message, r.reachable),
tracked: r.tracked != 0,
})
.try_collect::<Vec<_>>()

View file

@ -2,80 +2,66 @@ use askama::Template;
use crate::server::util;
use super::Base;
use super::{
base::{Base, Link},
paths::{PathCommitByHash, PathRunById, PathWorkerByName},
};
#[derive(Template)]
#[template(
ext = "html",
source = "\
{% import \"util.html\" as util %}
<a href=\"{{ root }}/commit/{{ hash }}\"
class=\"{% call util::commit_class(reachable) %}\"
<a href=\"{{ link }}\" \
class=\"{% call util::commit_class(reachable) %}\" \
title=\"{% call util::commit_title(reachable) %}\">
{{ short }}
</a>
"
)]
pub struct CommitLink {
root: String,
hash: String,
pub struct LinkCommit {
link: Link,
short: String,
reachable: i64,
}
impl CommitLink {
impl LinkCommit {
pub fn new(base: &Base, hash: String, message: &str, reachable: i64) -> Self {
Self {
root: base.root.clone(),
short: util::format_commit_short(&hash, message),
hash,
link: base.link(PathCommitByHash { hash }),
reachable,
}
}
}
#[derive(Template)]
#[template(
ext = "html",
source = "\
<a href=\"{{ root }}/run/{{ id }}\">
Run of {{ short }}
</a>
"
)]
pub struct RunLink {
root: String,
id: String,
#[template(ext = "html", source = "<a href=\"{{ link }}\">Run of {{ short }}</a>")]
pub struct LinkRunShort {
link: Link,
short: String,
}
impl RunLink {
impl LinkRunShort {
pub fn new(base: &Base, id: String, hash: &str, message: &str) -> Self {
Self {
root: base.root.clone(),
id,
link: base.link(PathRunById { id }),
short: util::format_commit_short(hash, message),
}
}
}
#[derive(Template)]
#[template(
ext = "html",
source = "\
<a href=\"{{ root }}/worker/{{ name }}\">
{{ name }}
</a>
"
)]
pub struct WorkerLink {
root: String,
#[template(ext = "html", source = "<a href=\"{{ link }}\">{{ name }}</a>")]
pub struct LinkWorker {
link: Link,
name: String,
}
impl WorkerLink {
impl LinkWorker {
pub fn new(base: &Base, name: String) -> Self {
Self {
root: base.root.clone(),
link: base.link(PathWorkerByName { name: name.clone() }),
name,
}
}

View file

@ -19,27 +19,27 @@ use crate::{
};
use super::{
link::{CommitLink, RunLink, WorkerLink},
base::{Base, Link, Tab},
link::{LinkCommit, LinkRunShort, LinkWorker},
paths::{PathQueue, PathQueueInner},
Base, Tab,
};
enum Status {
Idle,
Busy,
Working(RunLink),
Working(LinkRunShort),
}
struct Worker {
link: WorkerLink,
link: LinkWorker,
status: Status,
}
struct Task {
commit: CommitLink,
commit: LinkCommit,
since: String,
priority: i64,
workers: Vec<WorkerLink>,
workers: Vec<LinkWorker>,
odd: bool,
}
@ -72,7 +72,7 @@ async fn get_workers(
)
.fetch_one(db)
.await?;
Status::Working(RunLink::new(
Status::Working(LinkRunShort::new(
base,
unfinished.run.id.clone(),
&unfinished.run.hash,
@ -82,7 +82,7 @@ async fn get_workers(
};
result.push(Worker {
link: WorkerLink::new(base, name.clone()),
link: LinkWorker::new(base, name.clone()),
status,
})
}
@ -95,13 +95,13 @@ async fn get_queue_data(
base: &Base,
) -> somehow::Result<Vec<Task>> {
// Group workers by commit hash
let mut workers_by_commit: HashMap<String, Vec<WorkerLink>> = HashMap::new();
let mut workers_by_commit: HashMap<String, Vec<LinkWorker>> = HashMap::new();
for (name, info) in workers {
if let WorkerStatus::Working(unfinished) = &info.status {
workers_by_commit
.entry(unfinished.run.hash.clone())
.or_default()
.push(WorkerLink::new(base, name.clone()));
.push(LinkWorker::new(base, name.clone()));
}
}
@ -121,7 +121,7 @@ async fn get_queue_data(
.fetch(db)
.map_ok(|r| Task {
workers: workers_by_commit.remove(&r.hash).unwrap_or_default(),
commit: CommitLink::new(base, r.hash, &r.message, r.reachable),
commit: LinkCommit::new(base, r.hash, &r.message, r.reachable),
since: util::format_delta_from_now(r.date),
priority: r.priority,
odd: false,
@ -164,6 +164,7 @@ pub async fn get_queue_inner(
#[derive(Template)]
#[template(path = "queue.html")]
struct QueueTemplate {
link_queue_js: Link,
base: Base,
inner: QueueInnerTemplate,
}
@ -179,6 +180,7 @@ pub async fn get_queue(
let workers = get_workers(&db, &sorted_workers, &base).await?;
let tasks = get_queue_data(&db, &sorted_workers, &base).await?;
Ok(QueueTemplate {
link_queue_js: base.link("/queue.js"), // TODO Static link
base,
inner: QueueInnerTemplate { workers, tasks },
})

View file

@ -13,7 +13,10 @@ use crate::{
somehow,
};
use super::{paths::PathWorkerByName, Base, Tab};
use super::{
base::{Base, Tab},
paths::PathWorkerByName,
};
#[derive(Template)]
#[template(path = "worker.html")]