2020-12-17 10:05:26 +01:00
|
|
|
use super::queries::*;
|
|
|
|
|
2020-10-26 23:43:18 +01:00
|
|
|
use handlebars::Handlebars;
|
2020-12-17 10:05:26 +01:00
|
|
|
use serde::Serialize;
|
|
|
|
use serde_json::value::Value as Json;
|
2020-10-26 23:43:18 +01:00
|
|
|
use std::path::Path;
|
|
|
|
|
2020-12-08 11:05:59 +01:00
|
|
|
const TEMPLATES_DIR: &str = "templates";
|
|
|
|
static TEMPLATE_FILES: &[(&str, &str)] = &[
|
2020-11-06 15:23:17 +01:00
|
|
|
("admin.html", include_str!("../templates/admin.html")),
|
2022-02-25 15:41:50 +01:00
|
|
|
(
|
|
|
|
"admin_group.html",
|
|
|
|
include_str!("../templates/admin_group.html"),
|
|
|
|
),
|
2020-11-06 15:23:17 +01:00
|
|
|
(
|
|
|
|
"admin_login.html",
|
|
|
|
include_str!("../templates/admin_login.html"),
|
|
|
|
),
|
2022-02-25 15:41:50 +01:00
|
|
|
("group.html", include_str!("../templates/group.html")),
|
|
|
|
("index.html", include_str!("../templates/index.html")),
|
2020-11-06 15:23:17 +01:00
|
|
|
];
|
2020-10-26 23:43:18 +01:00
|
|
|
|
|
|
|
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)
|
2020-12-08 11:05:59 +01:00
|
|
|
.unwrap_or_else(|_| panic!("Cannot write template file {}", file));
|
2020-10-26 23:43:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2020-12-08 11:05:59 +01:00
|
|
|
.unwrap_or_else(|_| panic!("Cannot read template file`{}`", file)),
|
2020-10-26 23:43:18 +01:00
|
|
|
)
|
2020-12-08 11:05:59 +01:00
|
|
|
.unwrap_or_else(|_| panic!("Bad encoding in template file `{}`", file)),
|
2020-10-26 23:43:18 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
Templates { hb }
|
|
|
|
}
|
2020-12-17 10:05:26 +01:00
|
|
|
|
|
|
|
#[derive(Serialize)]
|
2020-12-17 23:08:31 +01:00
|
|
|
pub struct CommonTemplate<'a> {
|
2020-12-17 10:05:26 +01:00
|
|
|
pub lang: &'a str,
|
2020-12-17 17:13:11 +01:00
|
|
|
pub root_url: &'a str,
|
2020-12-17 23:08:31 +01:00
|
|
|
pub title: &'a str,
|
2020-12-17 10:05:26 +01:00
|
|
|
pub errors: &'a [ErrorTemplate<'a>],
|
2020-12-17 23:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct IndexTemplate<'a> {
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub common: CommonTemplate<'a>,
|
2020-12-17 10:05:26 +01:00
|
|
|
pub ads: &'a Json,
|
2022-02-25 15:41:50 +01:00
|
|
|
pub groups: &'a Json,
|
2020-12-17 10:05:26 +01:00
|
|
|
pub new_ad_form_refill: Option<NewAdQuery>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct AdminLoginTemplate<'a> {
|
2020-12-17 23:08:31 +01:00
|
|
|
#[serde(flatten)]
|
|
|
|
pub common: CommonTemplate<'a>,
|
2020-12-17 10:05:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct AdminTemplate<'a> {
|
2020-12-17 23:08:31 +01:00
|
|
|
#[serde(flatten)]
|
|
|
|
pub common: CommonTemplate<'a>,
|
2020-12-17 10:05:26 +01:00
|
|
|
pub ads: &'a Json,
|
2022-02-25 15:41:50 +01:00
|
|
|
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>,
|
2020-12-17 10:05:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct ErrorTemplate<'a> {
|
|
|
|
pub text: &'a str,
|
|
|
|
}
|