tildes-statistics/source/templates/mod.rs

49 lines
1.1 KiB
Rust
Raw Normal View History

2022-10-06 16:53:45 +00:00
//! All HTML templates.
use {
askama::Template,
async_std::{fs::write, path::PathBuf},
chrono::NaiveDate,
color_eyre::Result,
};
2022-10-08 20:11:52 +00:00
use crate::{group_data::GroupDataModel, utilities::today};
/// The template for the home page.
#[derive(Template)]
#[template(path = "index.html")]
pub struct HomeTemplate {
2022-10-08 20:11:52 +00:00
/// The groups to create the table with.
pub groups: Vec<GroupDataModel>,
/// The string for the `<title>` element.
pub page_title: String,
/// The date of today's snapshot.
pub today: NaiveDate,
/// The user count from the group with the most subscribers.
pub user_count: String,
}
impl HomeTemplate {
/// Create a new [`HomeTemplate`].
2022-10-08 20:11:52 +00:00
pub fn new(groups: Vec<GroupDataModel>, user_count: Option<i64>) -> Self {
Self {
2022-10-08 20:11:52 +00:00
groups,
page_title: "Tildes Statistics".to_string(),
today: today(),
user_count: user_count
.map(|n| n.to_string())
.unwrap_or_else(|| "unknown".to_string()),
}
}
/// Render the template and write it to file.
pub async fn render_to_file(&self, parent: &PathBuf) -> Result<()> {
write(parent.join("index.html"), self.render()?).await?;
Ok(())
}
}