89 lines
2.2 KiB
Rust
89 lines
2.2 KiB
Rust
use std::cell::RefCell;
|
|
|
|
use lazy_static::lazy_static;
|
|
use rust_embed::RustEmbed;
|
|
use serde::{Deserialize, Serialize};
|
|
use tera::*;
|
|
|
|
lazy_static! {
|
|
pub static ref TEMPLATES: Tera = {
|
|
let mut tera = match Tera::new("templates/**/*") {
|
|
Ok(t) => t,
|
|
Err(e) => {
|
|
println!("Parsing error(s): {}", e);
|
|
::std::process::exit(1);
|
|
}
|
|
};
|
|
register_templates(&mut tera);
|
|
tera.autoescape_on(vec![".html", ".sql"]);
|
|
tera
|
|
};
|
|
}
|
|
|
|
#[derive(RustEmbed)]
|
|
#[folder = "templates/"]
|
|
pub struct Templates;
|
|
|
|
impl Templates {
|
|
pub fn get_template(t: &TemplateFile) -> Option<String> {
|
|
match Self::get(t.path) {
|
|
Some(file) => Some(String::from_utf8_lossy(&file.data).into_owned()),
|
|
None => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct TemplateFile {
|
|
pub name: &'static str,
|
|
pub path: &'static str,
|
|
}
|
|
|
|
impl TemplateFile {
|
|
pub const fn new(name: &'static str, path: &'static str) -> Self {
|
|
Self { name, path }
|
|
}
|
|
|
|
pub fn register(&self, t: &mut Tera) -> std::result::Result<(), tera::Error> {
|
|
t.add_raw_template(self.name, &Templates::get_template(self).expect(self.name))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[allow(dead_code)]
|
|
pub fn register_from_file(&self, t: &mut Tera) -> std::result::Result<(), tera::Error> {
|
|
use std::path::Path;
|
|
t.add_template_file(Path::new("templates/").join(self.path), Some(self.name))
|
|
}
|
|
}
|
|
|
|
pub const INDEX: TemplateFile = TemplateFile::new("index", "index.html");
|
|
|
|
pub fn register_templates(t: &mut tera::Tera) {
|
|
for template in [INDEX].iter() {
|
|
template.register(t).expect(template.name);
|
|
}
|
|
}
|
|
#[derive(Clone)]
|
|
pub struct ViewResult {
|
|
ctx: RefCell<Context>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Payload {
|
|
pub results: crate::runner::results::ArchivableResult,
|
|
}
|
|
|
|
const PAYLOAD_KEY: &str = "payload";
|
|
|
|
impl ViewResult {
|
|
pub fn new(payload: Payload) -> Self {
|
|
let mut ctx = Context::new();
|
|
ctx.insert(PAYLOAD_KEY, &payload);
|
|
let ctx = RefCell::new(ctx);
|
|
Self { ctx }
|
|
}
|
|
|
|
pub fn render(&self) -> String {
|
|
TEMPLATES.render(INDEX.name, &self.ctx.borrow()).unwrap()
|
|
}
|
|
}
|