webcomment/src/templates.rs

59 lines
1.3 KiB
Rust

use crate::{config::Config, db::*};
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 {
pub fn new(dir: &Path, config: &Config) -> Self {
let dir = dir.join(&config.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}"));
}
}
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>,
pub author: String,
pub editable: bool,
pub id: String,
pub last_edit_time: Option<Time>,
pub needs_approval: bool,
pub original: Option<OriginalComment>,
pub post_time: Time,
pub text: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct OriginalComment {
pub author: String,
pub editable: bool,
pub last_edit_time: Option<Time>,
pub post_time: Time,
pub text: String,
}