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
impl TemplateService
Sourcepub fn should_use_template(config: &TemplateConfig) -> bool
pub fn should_use_template(config: &TemplateConfig) -> bool
Determines if templates should be used based on configuration
Business rule: Templates are used when:
- Template system is enabled in config
- Template file path is specified and non-empty
This is a pure function that only examines configuration.
Sourcepub fn validate_template(
content: &str,
config: &TemplateConfig,
) -> ValidationResult
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);Sourcepub fn generate_content(
template_content: Option<&str>,
title: &str,
backlink_content: &str,
) -> String
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]]"Sourcepub fn resolve_template_path(config: &TemplateConfig) -> TemplateResult<String>
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:
- If specific file is configured, use that
- If directory is configured, use default template within directory
- Validate configuration makes sense
This is pure logic - actual file reading is handled by CLI layer.
Sourcepub fn create_template_context(
title: &str,
backlink: &str,
) -> HashMap<String, String>
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}}