srctrait_note/
template.rs

1use std::borrow::Cow;
2
3use srctrait_common_chronox::DateTimeFormat;
4
5use crate::*;
6
7pub(crate) const TMPL_TODAY: &'static str = include_str!("../assets/note-templates/today.md.tmpl");
8pub(crate) const TMPL_IDEA: &'static str = include_str!("../assets/note-templates/idea.md.tmpl");
9pub(crate) const TMPL_TODO: &'static str = include_str!("../assets/note-templates/todo.md.tmpl");
10pub(crate) const TMPL_PLAN: &'static str = include_str!("../assets/note-templates/plan.md.tmpl");
11pub(crate) const TMPL_PLAN_TOPIC: &'static str = include_str!("../assets/note-templates/plan-topic.md.tmpl");
12
13impl NoteKind {
14    pub fn default_template_str(&self) -> &'static str {
15        match self {
16            NoteKind::Today => TMPL_TODAY,
17            NoteKind::Idea => TMPL_IDEA,
18            NoteKind::Todo => TMPL_TODO,
19            NoteKind::Plan => TMPL_PLAN,
20            NoteKind::PlanTopic => TMPL_PLAN_TOPIC,
21        }
22    }
23}
24
25pub fn render_template_str(tmpl: &str, vars: Vec<(&'static str, Cow<'_, str>)>) -> String {
26    let mut output = tmpl.to_string();
27    for (var, value) in vars {
28        output = output.replace(&format!("{{{{{var}}}}}"), &value);
29    }
30
31    output
32}
33
34pub fn build_template_vars(date: Date, topic: Option<&str>) -> Vec<(&'static str, Cow<'_, str>)> {
35    Vec::from([
36        ("long-date", Cow::Owned(date.display(DateTimeFormat::Long).to_string())),
37        ("date", Cow::Owned(date.display(DateTimeFormat::YmdDash).to_string())),
38        ("topic", Cow::Borrowed(topic.unwrap_or_default()))
39    ])
40}