Skip to main content

TemplateService

Struct TemplateService 

Source
pub struct TemplateService;
Expand description

Core template processing service

This handles all template-related business logic:

  • Validation of template content against requirements
  • Placeholder substitution with actual values
  • Content generation for both template and built-in modes

PURE FUNCTIONS DESIGN: All methods are pure functions that take input and produce output without side effects. This makes testing easy and behavior predictable.

Implementations§

Source§

impl TemplateService

Source

pub fn should_use_template(config: &TemplateConfig) -> bool

Determines if templates should be used based on configuration

Business rule: Templates are used when:

  1. Template system is enabled in config
  2. Template file path is specified and non-empty

This is a pure function that only examines configuration.

Source

pub fn validate_template( content: &str, config: &TemplateConfig, ) -> ValidationResult

Validates template content against configuration requirements

Checks that required placeholders are present. This catches configuration errors early, before note creation fails.

VALIDATION RULES:

  • If require_title is true, {{title}} must be present
  • If require_link is true, {{link}} must be present
  • Unknown placeholders are allowed (forward compatibility)

EXAMPLES:

let config = TemplateConfig { require_title: true, require_link: true, .. };
let content = "# {{title}}\n\nParent: {{link}}";
let result = TemplateService::validate_template(content, &config);
assert!(result.valid);
Source

pub fn generate_content( template_content: Option<&str>, title: &str, backlink_content: &str, ) -> String

Generates final note content using template or built-in format

This is the core content generation function. It handles both template mode (with placeholder substitution) and built-in mode (standard format).

TEMPLATE MODE: Replaces all placeholders with provided values. Unknown placeholders are left unchanged for forward compatibility.

BUILT-IN MODE: Creates standard “# Title\n\nBacklink” format when no template provided.

BUSINESS RULES:

  • Empty title is handled gracefully
  • Empty backlink is handled gracefully
  • Whitespace is preserved from template
  • Multiple occurrences of same placeholder are all replaced

EXAMPLES:

// Template mode
let template = "# {{title}}\n\nParent: {{link}}";
let content = TemplateService::generate_content(
    Some(template), "My Note", "[[parent]]"
);
// Result: "# My Note\n\nParent: [[parent]]"

// Built-in mode
let content = TemplateService::generate_content(
    None, "My Note", "[[parent]]"
);
// Result: "# My Note\n\n[[parent]]"
Source

pub fn resolve_template_path(config: &TemplateConfig) -> TemplateResult<String>

Resolves template file path based on configuration

Handles both single template file and template directory modes. Returns the final template file path to read.

RESOLUTION LOGIC:

  1. If specific file is configured, use that
  2. If directory is configured, use default template within directory
  3. Validate configuration makes sense

This is pure logic - actual file reading is handled by CLI layer.

Source

pub fn create_template_context( title: &str, backlink: &str, ) -> HashMap<String, String>

Creates a context map for advanced template processing (future feature)

This prepares for more sophisticated template systems that might use templating engines like Handlebars or Tera. Currently returns basic key-value pairs for the placeholders we support.

FUTURE ENHANCEMENT: Could support conditional logic, loops, includes, etc.

# {{title}}
{{#if parent}}
Parent: {{parent}}
{{/if}}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.