SAL Text - Text Processing and Manipulation Utilities
SAL Text provides a comprehensive collection of text processing utilities for both Rust applications and Rhai scripting environments.
Features
- Text Indentation: Remove common leading whitespace (
dedent) and add prefixes (prefix) - String Normalization: Sanitize strings for filenames (
name_fix) and paths (path_fix) - Text Replacement: Powerful
TextReplacerfor regex and literal replacements - Template Rendering:
TemplateBuilderusing Tera engine for dynamic text generation
Rust API
Text Indentation
use ;
// Remove common indentation
let indented = " line 1\n line 2\n line 3";
let dedented = dedent;
assert_eq!;
// Add prefix to each line
let text = "line 1\nline 2";
let prefixed = prefix;
assert_eq!;
String Normalization
use ;
// Sanitize filename
let unsafe_name = "User's File [Draft].txt";
let safe_name = name_fix;
assert_eq!;
// Sanitize path (preserves directory structure)
let unsafe_path = "/path/to/User's File.txt";
let safe_path = path_fix;
assert_eq!;
Text Replacement
use TextReplacer;
// Simple literal replacement
let replacer = builder
.pattern
.replacement
.build
.expect;
let result = replacer.replace;
assert_eq!;
// Regex replacement
let replacer = builder
.pattern
.replacement
.regex
.build
.expect;
let result = replacer.replace;
assert_eq!;
// Chained operations
let replacer = builder
.pattern
.replacement
.and
.pattern
.replacement
.regex
.build
.expect;
Template Rendering
use TemplateBuilder;
let result = open
.expect
.add_var
.add_var
.render
.expect;
Rhai Scripting
All functionality is available in Rhai scripts when using herodo:
// Text indentation
let dedented = dedent(" hello\n world");
let prefixed = prefix("line1\nline2", "> ");
// String normalization
let safe_name = name_fix("User's File [Draft].txt");
let safe_path = path_fix("/path/to/User's File.txt");
// Text replacement
let builder = text_replacer_new();
builder = pattern(builder, "hello");
builder = replacement(builder, "hi");
builder = regex(builder, false);
let replacer = build(builder);
let result = replace(replacer, "hello world");
// Template rendering
let template = template_builder_open("template.txt");
template = add_var(template, "name", "World");
let result = render(template);
Testing
Run the comprehensive test suite:
# Unit tests
# Rhai integration tests
Dependencies
regex: For regex-based text replacementtera: For template renderingserde: For template variable serializationrhai: For Rhai scripting integration
License
Apache-2.0