webcomment/src/templates.rs

48 lines
1 KiB
Rust
Raw Normal View History

2022-10-26 14:03:22 +00:00
use crate::{config::Config, db::*};
2022-10-15 13:32:57 +00:00
use serde::Serialize;
use std::path::Path;
use tera::Tera;
static TEMPLATE_FILES: &[(&str, &str)] = &[
("comments.html", include_str!("../templates/comments.html")),
(
"admin_login.html",
include_str!("../templates/admin_login.html"),
),
];
pub struct Templates {
pub tera: Tera,
}
impl Templates {
2022-10-26 14:03:22 +00:00
pub fn new(dir: &Path, config: &Config) -> Self {
let dir = dir.join(&config.templates_dir);
2022-10-15 13:32:57 +00:00
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)
2022-10-26 14:03:22 +00:00
.unwrap_or_else(|_| panic!("Cannot write template file {file}"));
2022-10-15 13:32:57 +00:00
}
}
Self {
tera: Tera::new(dir.join("**/*").to_str().unwrap()).expect("Failed to parse templates"),
}
}
}
#[derive(Clone, Debug, Serialize)]
pub struct CommentWithId {
pub addr: Option<String>,
2022-10-15 13:32:57 +00:00
pub author: String,
2022-10-22 15:56:24 +00:00
pub editable: bool,
2022-10-15 13:32:57 +00:00
pub id: String,
pub needs_approval: bool,
pub post_time: Time,
pub text: String,
}