|
| 1 | +use std::path::{Path, PathBuf}; |
| 2 | + |
| 3 | +use handlebars::{DirectorySourceOptions, Handlebars}; |
| 4 | +use serde::Serialize; |
| 5 | + |
| 6 | +use crate::json::Progress; |
| 7 | + |
| 8 | +pub struct Templates<'h> { |
| 9 | + reg: Handlebars<'h>, |
| 10 | +} |
| 11 | + |
| 12 | +impl<'h> Templates<'h> { |
| 13 | + pub fn new() -> anyhow::Result<Self> { |
| 14 | + let templates = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../templates"); |
| 15 | + Self::from_templates_dir(&templates) |
| 16 | + } |
| 17 | + |
| 18 | + pub fn from_templates_dir(dir_path: impl AsRef<Path>) -> anyhow::Result<Self> { |
| 19 | + let dir_path = dir_path.as_ref(); |
| 20 | + let mut reg = Handlebars::new(); |
| 21 | + |
| 22 | + reg.set_strict_mode(true); |
| 23 | + |
| 24 | + reg.register_templates_directory(dir_path, DirectorySourceOptions::default())?; |
| 25 | + assert!(reg.get_template("updates").is_some()); |
| 26 | + |
| 27 | + reg.register_helper("markdown_to_html", Box::new(markdown_to_html)); |
| 28 | + |
| 29 | + Ok(Templates { reg }) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +handlebars::handlebars_helper!(markdown_to_html: |md: String| comrak::markdown_to_html(&md, &Default::default())); |
| 34 | + |
| 35 | +/// The parameters expected by the `updates.md` template. |
| 36 | +#[derive(Serialize, Debug)] |
| 37 | +pub struct Updates { |
| 38 | + pub milestone: String, |
| 39 | + pub flagship_goals: Vec<UpdatesFlagshipGoal>, |
| 40 | + pub other_goals: Vec<UpdatesOtherGoal>, |
| 41 | +} |
| 42 | + |
| 43 | +impl Updates { |
| 44 | + pub fn render(self) -> anyhow::Result<String> { |
| 45 | + let templates = Templates::new()?; |
| 46 | + Ok(templates.reg.render("updates", &self)?) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +#[derive(Serialize, Debug)] |
| 51 | +pub struct UpdatesFlagshipGoal { |
| 52 | + /// Title of the tracking issue |
| 53 | + pub title: String, |
| 54 | + |
| 55 | + /// Tracking issue number on the project goals repository |
| 56 | + pub issue_number: u64, |
| 57 | + |
| 58 | + /// Comma-separated list of assignees |
| 59 | + pub issue_assignees: String, |
| 60 | + |
| 61 | + /// URL of the tracking issue |
| 62 | + pub issue_url: String, |
| 63 | + |
| 64 | + /// Progress towards the goal |
| 65 | + pub progress: Progress, |
| 66 | + |
| 67 | + /// Updates provided towards the goal |
| 68 | + pub updates: Vec<UpdatesFlagshipGoalUpdate>, |
| 69 | +} |
| 70 | + |
| 71 | +#[derive(Serialize, Debug)] |
| 72 | +pub struct UpdatesFlagshipGoalUpdate { |
| 73 | + /// Username of the person who wrote the update |
| 74 | + pub author: String, |
| 75 | + |
| 76 | + /// Formatted like "Oct 26" |
| 77 | + pub date: String, |
| 78 | + |
| 79 | + /// Text of the update |
| 80 | + pub update: String, |
| 81 | + |
| 82 | + /// URL of the comment |
| 83 | + pub url: String, |
| 84 | +} |
| 85 | + |
| 86 | +#[derive(Serialize, Debug)] |
| 87 | +pub struct UpdatesOtherGoal { |
| 88 | + /// Title of the tracking issue |
| 89 | + pub title: String, |
| 90 | + |
| 91 | + /// Tracking issue number on the project goals repository |
| 92 | + pub issue_number: u64, |
| 93 | + |
| 94 | + /// Comma-separated list of assignees |
| 95 | + pub issue_assignees: String, |
| 96 | + |
| 97 | + /// URL of the tracking issue |
| 98 | + pub issue_url: String, |
| 99 | + |
| 100 | + /// Markdown with update text (bullet list) |
| 101 | + pub updates_markdown: String, |
| 102 | + |
| 103 | + /// Progress towards the goal |
| 104 | + pub progress: Progress, |
| 105 | +} |
0 commit comments