From f3d7597947d72ef0ff09ac853bdda7d90756547b Mon Sep 17 00:00:00 2001 From: simonsan <14062932+simonsan@users.noreply.github.com> Date: Sat, 23 Mar 2024 23:07:40 +0100 Subject: [PATCH] convert ReflectionSummary to PaceReflection to wrap tera::Context Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com> --- crates/core/src/commands/reflect.rs | 6 ++--- crates/core/src/template.rs | 41 +++++++++++++++++++++++++++++ templates/reflections/basic.html | 35 +++++------------------- 3 files changed, 50 insertions(+), 32 deletions(-) diff --git a/crates/core/src/commands/reflect.rs b/crates/core/src/commands/reflect.rs index 2019edc2..84af5eaa 100644 --- a/crates/core/src/commands/reflect.rs +++ b/crates/core/src/commands/reflect.rs @@ -8,7 +8,6 @@ use pace_time::{ }; use serde_derive::Serialize; use std::path::PathBuf; -use tera::Context; use tracing::debug; use typed_builder::TypedBuilder; @@ -18,7 +17,7 @@ use crate::{ error::{PaceResult, TemplatingErrorKind, UserMessage}, service::{activity_store::ActivityStore, activity_tracker::ActivityTracker}, storage::get_storage_from_config, - template::TEMPLATES, + template::{PaceReflectionTemplate, TEMPLATES}, }; /// `reflect` subcommand options @@ -182,8 +181,7 @@ impl ReflectCommandOptions { } Some(ReflectionsFormatKind::Html) => { - let context = Context::from_serialize(reflection) - .map_err(TemplatingErrorKind::FailedToGenerateContextFromSerialize)?; + let context = PaceReflectionTemplate::from(reflection).into_context(); let html = if template_file.is_none() { TEMPLATES diff --git a/crates/core/src/template.rs b/crates/core/src/template.rs index 8729fa3f..8a892e3e 100644 --- a/crates/core/src/template.rs +++ b/crates/core/src/template.rs @@ -1,6 +1,8 @@ use lazy_static::lazy_static; use tera::Tera; +use crate::prelude::{ReflectionSummary, SummaryActivityGroup}; + lazy_static! { pub static ref TEMPLATES: Tera = { let mut tera = match Tera::new("templates/reflections/**") { @@ -15,3 +17,42 @@ lazy_static! { tera }; } + +#[derive(Debug)] +pub struct PaceReflectionTemplate { + context: tera::Context, +} + +impl PaceReflectionTemplate { + pub fn into_context(self) -> tera::Context { + self.context + } +} + +impl From for PaceReflectionTemplate { + fn from(value: ReflectionSummary) -> Self { + let mut context = tera::Context::new(); + context.insert("time_range_start", &value.time_range().start()); + context.insert("time_range_end", &value.time_range().end()); + + context.insert("total_time_spent", &value.total_time_spent()); + context.insert("total_break_duration", &value.total_break_duration()); + + // key must be a string, because of the way tera works with nested objects + // we need to convert the key to a string + + // merge key tuples into a single string + let summary_groups_by_category = value + .summary_groups_by_category() + .iter() + .map(|((category, subcategory), summary_group)| { + let key = format!("{category}::{subcategory}"); + (key, summary_group) + }) + .collect::>(); + + context.insert("summary_groups_by_category", &summary_groups_by_category); + + Self { context } + } +} diff --git a/templates/reflections/basic.html b/templates/reflections/basic.html index 8610221f..d0650c0e 100644 --- a/templates/reflections/basic.html +++ b/templates/reflections/basic.html @@ -29,11 +29,14 @@

Activity Report

Time Range

-

Start:

-

End:

+

Start: {{ time_range_start }}

+

End: {{ time_range_end }}

Total Time Spent

-

seconds

+

{{ total_time_spent }} seconds

+ +

Total Break Duration

+

{{ total_break_duration }} seconds

Summary Groups by Category

@@ -44,32 +47,8 @@

Summary Groups by Category

Description Adjusted Duration + {{ summary_groups_by_category }} - - - \ No newline at end of file