Module :: genfile
A trait-based template processing and file generation library for Rust. genfile provides self-contained template archives with parameter storage, pluggable rendering engines, and testable in-memory file systems.
Features
- Self-Contained Archives: Template files with embedded parameters stored inside (JSON/YAML serialization)
- Binary + Text Support: Handle both text templates and binary files (images, etc.) with base64 encoding
- Pluggable Architecture: Trait-based design for custom value types, renderers, and file systems
- Testable: Built-in
MemoryFileSystemfor fast, isolated testing without disk I/O - Security: Path traversal validation prevents directory escape attacks
- External Content: Support for
FileRefandUrlRefwith custom resolvers and storage backends - Template Engine: Default Handlebars renderer with support for custom engines
- 215 Tests: Comprehensive test coverage including 27 security tests
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
Basic Example
use ;
use ;
// Create a template archive
let mut archive = new;
// Add template files
archive.add_text_file;
archive.add_text_file;
// Set parameter values
archive.set_value;
archive.set_value;
// Materialize to memory filesystem (for testing)
let renderer = new;
let mut fs = new;
archive.materialize_with_components.unwrap;
// Verify generated files
assert!;
assert_eq!;
Sample
Examples
Archive with Binary Files
use ;
use PathBuf;
let mut archive = new;
// Text template
archive.add_text_file;
// Binary file (PNG header)
archive.add_binary_file;
archive.set_value;
Serialization and Persistence
use TemplateArchive;
// Create and serialize to JSON
let archive = new;
let json = archive.to_json_pretty.unwrap;
// Save to file
write.unwrap;
// Load from file
let json = read_to_string.unwrap;
let restored = from_json.unwrap;
External Content Sources
use ;
use PathBuf;
let mut archive = new;
// Reference external file
archive.add_file_from;
// Reference URL
archive.add_file_from;
Custom Storage Backend
use ;
use Path;
;
Parameter Discovery and Analysis
use ;
use PathBuf;
let mut archive = new;
archive.add_text_file;
// Add parameter definitions
archive.add_parameter;
// Discover parameters used in templates
let discovered = archive.discover_parameters;
// Analyze parameter usage
let usage = archive.analyze_parameter_usage;
// Find undefined parameters
let undefined = archive.get_undefined_parameters;
API Overview
Core Types
TemplateArchive- Main entity for template operations, stores files, parameters, and valuesTemplate<V,R>- Alternative API with custom value types and renderersTemplateFile- Individual file with content, metadata, and optional external sourceFileContent- Enum forText(String)orBinary(Vec<u8>)Value- Default parameter value type:String,Number,Bool,List
Traits
TemplateValue- Trait for custom parameter value typesTemplateRenderer- Trait for pluggable rendering engines (default: Handlebars)FileSystem- Trait for file operations (RealFileSystemorMemoryFileSystem)ContentResolver- Trait for resolving external content sourcesContentStorage- Trait for custom storage backends
Content Sources
ContentSource::Inline- Content embedded directly in archiveContentSource::File- Reference to external file pathContentSource::Url- Reference to remote URL
Security
All file paths are validated to prevent directory traversal attacks:
use validate_path;
use Path;
// Valid paths
assert!;
assert!;
// Invalid paths (rejected)
assert!;
assert!;
27 dedicated security tests ensure protection against malicious paths.
Testing
Use MemoryFileSystem for fast, isolated tests:
use ;
use Path;