use serde_json::Value;
use crate::filters::{FilterError, FilterRegistry};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MissingValuePolicy {
Error,
Empty,
KeepPlaceholder,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LineBreakPolicy {
#[default]
InsertWordBreak,
SplitParagraph,
}
#[derive(Debug, Clone)]
pub struct TemplateOptions {
pub missing_value_policy: MissingValuePolicy,
pub line_break_policy: LineBreakPolicy,
pub preserve_whitespace: bool,
pub headers_and_footers: bool,
pub footnotes: bool,
pub filters: FilterRegistry,
}
impl TemplateOptions {
#[must_use]
pub fn missing_value_policy(mut self, policy: MissingValuePolicy) -> Self {
self.missing_value_policy = policy;
self
}
#[must_use]
pub fn line_break_policy(mut self, policy: LineBreakPolicy) -> Self {
self.line_break_policy = policy;
self
}
#[must_use]
pub fn preserve_whitespace(mut self, preserve: bool) -> Self {
self.preserve_whitespace = preserve;
self
}
#[must_use]
pub fn headers_and_footers(mut self, process: bool) -> Self {
self.headers_and_footers = process;
self
}
#[must_use]
pub fn footnotes(mut self, process: bool) -> Self {
self.footnotes = process;
self
}
#[must_use]
pub fn filter<F>(mut self, name: impl Into<String>, filter: F) -> Self
where
F: Fn(&Value, Option<&str>) -> Result<Value, FilterError> + Send + Sync + 'static,
{
self.filters.insert(name, filter);
self
}
}
impl Default for TemplateOptions {
fn default() -> Self {
Self {
missing_value_policy: MissingValuePolicy::Error,
line_break_policy: LineBreakPolicy::default(),
preserve_whitespace: true,
headers_and_footers: false,
footnotes: false,
filters: FilterRegistry::default(),
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]
use super::*;
#[test]
fn default_missing_policy_is_error() {
assert_eq!(
TemplateOptions::default().missing_value_policy,
MissingValuePolicy::Error
);
}
#[test]
fn default_line_break_policy_is_insert_word_break() {
assert_eq!(
TemplateOptions::default().line_break_policy,
LineBreakPolicy::InsertWordBreak,
);
}
#[test]
fn builder_sets_missing_policy() {
let opts = TemplateOptions::default().missing_value_policy(MissingValuePolicy::Empty);
assert_eq!(opts.missing_value_policy, MissingValuePolicy::Empty);
}
#[test]
fn builder_sets_line_break_policy() {
let opts = TemplateOptions::default().line_break_policy(LineBreakPolicy::SplitParagraph);
assert_eq!(opts.line_break_policy, LineBreakPolicy::SplitParagraph);
}
#[test]
fn builder_is_composable() {
let opts = TemplateOptions::default()
.missing_value_policy(MissingValuePolicy::Empty)
.line_break_policy(LineBreakPolicy::SplitParagraph);
assert_eq!(opts.missing_value_policy, MissingValuePolicy::Empty);
assert_eq!(opts.line_break_policy, LineBreakPolicy::SplitParagraph);
}
#[test]
fn default_preserve_whitespace_is_true() {
assert!(TemplateOptions::default().preserve_whitespace);
}
#[test]
fn builder_sets_preserve_whitespace_false() {
let opts = TemplateOptions::default().preserve_whitespace(false);
assert!(!opts.preserve_whitespace);
}
#[test]
fn builder_preserve_whitespace_true_is_explicit_set() {
let opts = TemplateOptions::default().preserve_whitespace(true);
assert!(opts.preserve_whitespace);
}
#[test]
fn secondary_parts_are_off_by_default() {
let opts = TemplateOptions::default();
assert!(!opts.headers_and_footers);
assert!(!opts.footnotes);
}
#[test]
fn builder_enables_secondary_parts() {
let opts = TemplateOptions::default()
.headers_and_footers(true)
.footnotes(true);
assert!(opts.headers_and_footers);
assert!(opts.footnotes);
}
#[test]
fn default_options_carry_the_builtin_filters() {
let opts = TemplateOptions::default();
for name in [
"upper",
"lower",
"trim",
"default",
"len",
"join",
"round",
"fixed",
"thousands",
"percent",
] {
assert!(opts.filters.contains(name), "{name}");
}
#[cfg(feature = "dates")]
for name in ["date", "timezone"] {
assert!(opts.filters.contains(name), "{name}");
}
}
#[test]
fn builder_registers_a_filter() {
let opts = TemplateOptions::default()
.filter("shout", |value, _| Ok(Value::String(format!("{value}!"))));
assert!(opts.filters.contains("shout"));
}
}