Prepare more type-safe linking

This commit is contained in:
Joscha 2023-08-13 19:53:49 +02:00
parent 4ccf06db8b
commit 18e35184a5
2 changed files with 47 additions and 0 deletions

View file

@ -1,5 +1,6 @@
mod admin; mod admin;
mod api; mod api;
mod base;
mod commit; mod commit;
mod index; mod index;
mod link; mod link;

46
src/server/web/base.rs Normal file
View file

@ -0,0 +1,46 @@
use std::fmt;
use crate::config::Config;
pub enum Tab {
None,
Index,
Queue,
}
#[derive(Clone)]
pub struct Base {
web_base: String,
repo_name: String,
tab: &'static str,
}
impl Base {
pub fn new(config: &Config, tab: Tab) -> Self {
let tab = match tab {
Tab::None => "",
Tab::Index => "index",
Tab::Queue => "queue",
};
Self {
web_base: config.web_base.clone(),
repo_name: config.repo_name.clone(),
tab,
}
}
pub fn link<P: fmt::Display>(&self, to: P) -> Link {
let to = format!("{to}");
assert!(!self.web_base.ends_with('/'));
assert!(to.starts_with('/'));
Link(format!("{}{to}", self.web_base))
}
}
pub struct Link(String);
impl fmt::Display for Link {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}