109 lines
2.5 KiB
Rust
109 lines
2.5 KiB
Rust
use super::queries::*;
|
|
|
|
use handlebars::Handlebars;
|
|
use serde::Serialize;
|
|
use serde_json::value::Value as Json;
|
|
use std::path::Path;
|
|
|
|
const TEMPLATES_DIR: &str = "templates";
|
|
static TEMPLATE_FILES: &[(&str, &str)] = &[
|
|
("admin.html", include_str!("../templates/admin.html")),
|
|
(
|
|
"admin_group.html",
|
|
include_str!("../templates/admin_group.html"),
|
|
),
|
|
(
|
|
"admin_login.html",
|
|
include_str!("../templates/admin_login.html"),
|
|
),
|
|
("group.html", include_str!("../templates/group.html")),
|
|
("index.html", include_str!("../templates/index.html")),
|
|
];
|
|
|
|
pub struct Templates<'reg> {
|
|
pub hb: Handlebars<'reg>,
|
|
}
|
|
|
|
pub fn load_templates<'reg>(dir: &Path) -> Templates<'reg> {
|
|
let dir = dir.join(TEMPLATES_DIR);
|
|
std::fs::create_dir_all(&dir).expect("Cannot create templates dir");
|
|
|
|
for &(file, default) in TEMPLATE_FILES {
|
|
let file_path = dir.join(file);
|
|
if !file_path.is_file() {
|
|
std::fs::write(file_path, default)
|
|
.unwrap_or_else(|_| panic!("Cannot write template file {}", file));
|
|
}
|
|
}
|
|
|
|
let mut hb = Handlebars::new();
|
|
for &(file, _) in TEMPLATE_FILES {
|
|
hb.register_template_string(
|
|
file,
|
|
std::str::from_utf8(
|
|
&std::fs::read(dir.join(file))
|
|
.unwrap_or_else(|_| panic!("Cannot read template file`{}`", file)),
|
|
)
|
|
.unwrap_or_else(|_| panic!("Bad encoding in template file `{}`", file)),
|
|
)
|
|
.unwrap();
|
|
}
|
|
Templates { hb }
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct CommonTemplate<'a> {
|
|
pub lang: &'a str,
|
|
pub root_url: &'a str,
|
|
pub title: &'a str,
|
|
pub errors: &'a [ErrorTemplate<'a>],
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct IndexTemplate<'a> {
|
|
#[serde(flatten)]
|
|
pub common: CommonTemplate<'a>,
|
|
pub ads: &'a Json,
|
|
pub groups: &'a Json,
|
|
pub new_ad_form_refill: Option<NewAdQuery>,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct AdminLoginTemplate<'a> {
|
|
#[serde(flatten)]
|
|
pub common: CommonTemplate<'a>,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct AdminTemplate<'a> {
|
|
#[serde(flatten)]
|
|
pub common: CommonTemplate<'a>,
|
|
pub ads: &'a Json,
|
|
pub groups: &'a Json,
|
|
}
|
|
|
|
// TODO add subgroups, parent groups, title
|
|
#[derive(Serialize)]
|
|
pub struct GroupTemplate<'a> {
|
|
#[serde(flatten)]
|
|
pub common: CommonTemplate<'a>,
|
|
pub ads: &'a Json,
|
|
pub groups: &'a Json,
|
|
pub new_ad_form_refill: Option<NewAdQuery>,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct AdminGroupTemplate<'a> {
|
|
#[serde(flatten)]
|
|
pub common: CommonTemplate<'a>,
|
|
pub ads: &'a Json,
|
|
pub group: &'a Json,
|
|
pub groups: &'a Json,
|
|
pub new_group_form_refill: Option<AdminNewGroupQuery>,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct ErrorTemplate<'a> {
|
|
pub text: &'a str,
|
|
}
|