String Template Processor
A flexible, composable string transformation CLI tool and library for Rust originally created as a parser for television. It allows you to chain operations like split, join, slice, replace, case conversion, trim, and more, using a concise template syntax.
Use Cases
- Data extraction: Parse CSV, logs, or structured text
- Text transformation: Clean and format strings in pipelines
- File processing: Extract parts of filenames or paths
- Configuration parsing: Process environment variables or config files
- Shell scripting: Quick text manipulation in scripts
Features
- Composable operations: Chain multiple string operations in a single template.
- Split and join: Extract and reassemble parts of strings.
- Slice and range: Select substrings or sublists by index or range.
- Replace: Regex-based search and replace, with sed-like syntax.
- Case conversion: Uppercase and lowercase.
- Trim and strip: Remove whitespace or custom characters.
- Append and prepend: Add text before or after.
- Escaping: Use
\:
and\\
to include literal colons and backslashes in arguments. - Negative indices: Support for negative indices (like Python) in ranges and slices.
- Stdin support: Read input from stdin when no input argument is provided.
- Tested: Comprehensive test suite.
📦 Crate
You can find this crate on crates.io:
[]
= "0.5.0"
🚀 Usage
As a CLI
# With input argument
# With stdin input
|
Examples:
# Get the second item in a comma-separated list
# Output: b
# Using stdin
|
# Output: b
# Replace all spaces with underscores and uppercase
# Output: FOO_BAR_BAZ
# Trim, split, and append
# Output: a!,b!,c!,d!,e!
# Using stdin for processing file content
|
# Pipeline processing
|
# Output: HELLO | WORLD
Template Syntax
Templates are enclosed in {}
and consist of a chain of operations separated by :
.
Arguments to operations are separated by :
as well.
To include a literal :
or \
in an argument, escape it as \:
or \\
.
Supported operations:
split:<sep>:<range>
— Split by separator, select by index or range.join:<sep>
— Join a list with separator.slice:<range>
— Slice string or list elements by range.replace:s/<pattern>/<replacement>/<flags>
— Regex replace (sed-like).upper
— Uppercase.lower
— Lowercase.trim
— Trim whitespace.strip:<chars>
— Trim custom characters.append:<suffix>
— Append text.prepend:<prefix>
— Prepend text.
Range syntax:
- Single index:
2
or-1
- Range:
1..3
,..2
,2..
,1..=3
,-3..-1
, etc.
Examples:
# Get the last item
# Output: c
# Get a range of items
# Output: b,c,d
# Replace 'foo' with 'bar' globally
# Output: bar bar
# Chain operations: uppercase, then append
# Output: HELLO!
# Prepend with a colon (escaped)
# Output: :foobar
# Complex chaining: split, select range, join, replace, uppercase
# Output: X-B
# Slice string characters
# Output: ell
# Split, trim each item, then prepend
|
# Output: item_a,item_b,item_c
# Strip custom characters
# Output: hello
Advanced Examples
# Process CSV-like data
|
# Output: age,city
# Format file paths
|
# Output: dir file.txt
# Extract file extension
|
# Output: PDF
# Process log entries with timestamps
|
# Output: error failed to connect
Error Handling
The tool provides helpful error messages for common issues:
# Invalid template format
# Error: Template must start with '{' and end with '}'
# Invalid range
# Error: Invalid index
# Invalid regex
# Error: regex parse error
Running Tests
Enjoy fast, composable string transformations!
Contributions and suggestions welcome.