Skip to content

Commit

Permalink
use tera one_off for rendering template from user
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Mar 23, 2024
1 parent 16bf1f8 commit 7a5a53c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ license = false
eula = false

[dependencies]
abscissa_core = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
clap = { workspace = true, features = ["env", "wrap_help", "derive"] }
clap_complete = { workspace = true }
Expand All @@ -109,17 +110,16 @@ pace_core = { workspace = true, features = ["cli"] }
pace_time = { workspace = true, features = ["cli"] }
serde = { workspace = true }
serde_derive = { workspace = true }

# Better error messages for Serde
# serde_path_to_error = "0.1.15"

abscissa_core = { workspace = true }
thiserror = { workspace = true }
toml = { workspace = true, features = ["preserve_order"] }

# optional: use `gimli` to capture backtraces
# see https://github.com/rust-lang/backtrace-rs/issues/189
# features = ["gimli-backtrace"]

# Better error messages for Serde
# serde_path_to_error = "0.1.15"

[dev-dependencies]
abscissa_core = { workspace = true, features = ["testing"] }
assert_cmd = { workspace = true }
Expand Down
38 changes: 32 additions & 6 deletions crates/core/src/commands/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,28 @@ pub struct ReflectCommandOptions {
category: Option<String>,

/// Case sensitive category filter
#[cfg_attr(feature = "clap", clap(long, value_name = "Case Sensitive"))]
#[cfg_attr(
feature = "clap",
clap(short = 'i', long, value_name = "Case Sensitive")
)]
case_sensitive: bool,

/// Specify output format (e.g., text, markdown, pdf)
#[cfg_attr(
feature = "clap",
clap(short, long, value_name = "Output Format", visible_alias = "format")
clap(short, long, value_name = "Output Format", visible_alias = "format",)
)]
output_format: Option<ReflectionsFormatKind>,

/// Use this template for rendering the reflection, overrides the default template
/// Used in conjunction with the `html` output format
// TODO: Make it dependent on the `output_format` argument
#[cfg_attr(
feature = "clap",
clap(short, long, value_name = "Template File", visible_alias = "template")
)]
template_file: Option<PathBuf>,

/// Export the reflections to a specified file
#[cfg_attr(
feature = "clap",
Expand Down Expand Up @@ -118,6 +130,8 @@ impl ReflectCommandOptions {
export_file,
time_flags,
date_flags,
template_file,
output_format,
// time_zone,
// time_zone_offset,
.. // TODO: ignore the rest of the fields for now,
Expand Down Expand Up @@ -145,7 +159,7 @@ impl ReflectCommandOptions {
));
};

match self.output_format() {
match output_format {
Some(ReflectionsFormatKind::Console) | None => {
return Ok(UserMessage::new(reflection.to_string()));
}
Expand All @@ -171,9 +185,21 @@ impl ReflectCommandOptions {
let context = Context::from_serialize(reflection)
.map_err(TemplatingErrorKind::FailedToGenerateContextFromSerialize)?;

let html = TEMPLATES
.render("base.html", &context)
.map_err(TemplatingErrorKind::RenderingToTemplateFailed)?;
let html = if template_file.is_none() {
TEMPLATES
.render("base.html", &context)
.map_err(TemplatingErrorKind::RenderingToTemplateFailed)?
} else {
let Some(user_tpl) = template_file.as_ref() else {
return Err(TemplatingErrorKind::TemplateFileNotSpecified.into());
};

let user_tpl = std::fs::read_to_string(user_tpl)
.map_err(TemplatingErrorKind::FailedToReadTemplateFile)?;

tera::Tera::one_off(&user_tpl, &context, true)
.map_err(TemplatingErrorKind::RenderingToTemplateFailed)?
};

debug!("Reflection: {}", html);

Check warning on line 204 in crates/core/src/commands/reflect.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/commands/reflect.rs#L204

Added line #L204 was not covered by tests

Expand Down
8 changes: 7 additions & 1 deletion crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use displaydoc::Display;
use miette::Diagnostic;
use pace_time::error::PaceTimeErrorKind;
use std::{error::Error, path::PathBuf};
use std::{error::Error, io, path::PathBuf};
use thiserror::Error;

use crate::domain::activity::{Activity, ActivityGuid};
Expand Down Expand Up @@ -260,6 +260,12 @@ pub enum TemplatingErrorKind {

/// Failed to render template: {0}
RenderingToTemplateFailed(tera::Error),

/// Failed to read template file: {0}
FailedToReadTemplateFile(io::Error),

/// Template file not specified
TemplateFileNotSpecified,
}

/// [`ActivityStoreErrorKind`] describes the errors that can happen while dealing with time.
Expand Down

0 comments on commit 7a5a53c

Please sign in to comment.