pub struct MultiTemplate { /* private fields */ }Expand description
A template engine supporting mixed literal text and string transformation operations.
MultiTemplate can contain any combination of literal text sections and template
sections that apply string operations to input data. This enables creating complex
text formatting patterns while maintaining high performance through intelligent caching.
§Template Structure
Templates are parsed into sections:
- Literal sections: Static text that appears unchanged in output
- Template sections: Operation sequences in
{op1|op2|...}format
§Caching Strategy
The template engine employs multiple levels of caching:
- Split cache: Common string splitting operations are cached globally
- Regex cache: Compiled regex patterns are cached for reuse
- Operation cache: Results of template sections are cached per input hash
§Debug Support
When debug mode is enabled, templates provide detailed execution tracing:
- Section-by-section processing breakdown
- Operation timing and cache statistics
- Input/output transformations
§Examples
§Basic Template Usage
use string_pipeline::Template;
let template = Template::parse("Result: {upper|trim}").unwrap();
assert_eq!(template.format(" hello ").unwrap(), "Result: HELLO");§Multi-Section Templates
use string_pipeline::Template;
let template = Template::parse("User: {split: :0} ({split: :1})").unwrap();
assert_eq!(template.format("john smith").unwrap(), "User: john (smith)");§Debug Mode
use string_pipeline::Template;
let template = Template::parse_with_debug("{split:,:..|sort|join: \\| }", Some(true)).unwrap();
let result = template.format("c,a,b").unwrap(); // Prints debug info to stderr
assert_eq!(result, "a | b | c");Implementations§
Source§impl MultiTemplate
impl MultiTemplate
Sourcepub fn parse(template: &str) -> Result<Self, String>
pub fn parse(template: &str) -> Result<Self, String>
Parse a template string into a MultiTemplate instance.
Parses template syntax containing literal text and {operation} blocks,
with support for complex operation pipelines, debug information is suppressed.
§Arguments
template- The template string to parse
§Returns
Ok(MultiTemplate)- Successfully parsed templateErr(String)- Parse error description
§Template Syntax
- Literal text appears as-is in output
{operation}blocks apply transformations to input- Multiple operations:
{op1|op2|op3} - Debug markers:
{!debug}are suppressed
§Examples
use string_pipeline::Template;
// Simple template
let template = Template::parse("Hello {upper}!").unwrap();
// Complex pipeline
let template = Template::parse("{split:,:..|sort|join: - }").unwrap();Sourcepub fn parse_with_debug(
template: &str,
debug: Option<bool>,
) -> Result<Self, String>
pub fn parse_with_debug( template: &str, debug: Option<bool>, ) -> Result<Self, String>
Parse a template string into a MultiTemplate instance.
Parses template syntax containing literal text and {operation} blocks,
with support for complex operation pipelines and debug mode.
§Arguments
template- The template string to parsedebug- Optional debug mode override (None uses template’s debug markers)
§Returns
Ok(MultiTemplate)- Successfully parsed templateErr(String)- Parse error description
§Template Syntax
- Literal text appears as-is in output
{operation}blocks apply transformations to input- Multiple operations:
{op1|op2|op3} - Debug markers:
{!debug}
§Examples
use string_pipeline::Template;
// Simple template
let template = Template::parse_with_debug("Hello {upper}!", None).unwrap();
// Complex pipeline
let template = Template::parse_with_debug("{split:,:..|sort|join: - }", None).unwrap();
// Debug enabled
let template = Template::parse_with_debug("{!upper|trim}", None).unwrap();
// Debug override
let template = Template::parse_with_debug("{upper}", Some(true)).unwrap();Sourcepub fn format(&self, input: &str) -> Result<String, String>
pub fn format(&self, input: &str) -> Result<String, String>
Apply the template to input data, producing formatted output.
Processes each template section in sequence, applying literal text directly and executing operation pipelines on the input data. Results are cached per input to optimize repeated operations.
§Arguments
input- The input string to transform
§Returns
Ok(String)- The formatted resultErr(String)- Error description if processing fails
§Performance
- Template sections with identical operations and input are cached
- Single split operations use a fast path
- Common separators are interned to reduce allocations
- ASCII-only operations use optimized algorithms where possible
§Debug Output
When debug mode is enabled, detailed execution information is printed to stderr:
- Section-by-section processing
- Operation timing and cache statistics
- Input/output transformations
§Examples
use string_pipeline::Template;
let template = Template::parse("Name: {split: :0}, Age: {split: :1}").unwrap();
let result = template.format("John 25").unwrap();
assert_eq!(result, "Name: John, Age: 25");
// List processing
let template = Template::parse("Items: {split:,:..|sort|join: \\| }").unwrap();
let result = template.format("apple,banana,cherry").unwrap();
assert_eq!(result, "Items: apple | banana | cherry");Sourcepub fn template_string(&self) -> &str
pub fn template_string(&self) -> &str
Get the original template string.
Returns the raw template string that was used to create this instance, useful for debugging, serialization, or displaying template definitions.
§Examples
use string_pipeline::Template;
let template = Template::parse("Hello {upper}!").unwrap();
assert_eq!(template.template_string(), "Hello {upper}!");Sourcepub fn section_count(&self) -> usize
pub fn section_count(&self) -> usize
Get the total number of sections in the template.
Returns the count of all sections (both literal and template sections) that make up this template.
§Examples
use string_pipeline::Template;
let template = Template::parse("Hello {upper} world!").unwrap();
assert_eq!(template.section_count(), 3); // "Hello ", "{upper}", " world!"Sourcepub fn template_section_count(&self) -> usize
pub fn template_section_count(&self) -> usize
Get the number of template sections (excluding literals).
Returns the count of sections that contain operations, excluding literal text sections.
§Examples
use string_pipeline::Template;
let template = Template::parse("Hello {upper} world {lower}!").unwrap();
assert_eq!(template.template_section_count(), 2); // {upper} and {lower}Sourcepub fn is_debug(&self) -> bool
pub fn is_debug(&self) -> bool
Check if debug mode is enabled.
Returns true if this template will output debug information during
formatting operations.
§Examples
use string_pipeline::Template;
let template = Template::parse_with_debug("{upper}", Some(true)).unwrap();
assert!(template.is_debug());Sourcepub fn with_debug(self, debug: bool) -> Self
pub fn with_debug(self, debug: bool) -> Self
Create a new template instance with debug mode set.
Returns a new template with the specified debug setting, leaving the original unchanged.
§Arguments
debug- Whether to enable debug mode
Trait Implementations§
Source§impl Clone for MultiTemplate
impl Clone for MultiTemplate
Source§fn clone(&self) -> MultiTemplate
fn clone(&self) -> MultiTemplate
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MultiTemplate
impl Debug for MultiTemplate
Source§impl Display for MultiTemplate
Provides string representation of the template.
impl Display for MultiTemplate
Provides string representation of the template.
Returns the original template string, making it easy to display or serialize template definitions.
§Examples
use string_pipeline::Template;
let template = Template::parse("Hello {upper}!").unwrap();
println!("{}", template); // Prints: Hello {upper}!