use genfile_core::
{
Template,
HandlebarsRenderer,
MemoryFileSystem,
FileDescriptor,
WriteMode,
Value,
FileSystem,
};
use std::path::PathBuf;
fn main() -> Result< (), Box< dyn core::error::Error > >
{
println!( "=== Low-Level Template API Example ===" );
println!();
let renderer = HandlebarsRenderer::new();
let mut template_fs = MemoryFileSystem::new();
template_fs.write(
&PathBuf::from( "/templates/greeting.hbs" ),
"Hello, {{name}}!\n"
)?;
template_fs.write(
&PathBuf::from( "/templates/config.hbs" ),
"server={{server}}\nport={{port}}\n"
)?;
println!( "Template files prepared in memory filesystem" );
println!();
let mut template = Template::< Value, _, _ >::new( renderer, template_fs );
template.insert_value( "name", Value::String( "World".into() ) );
template.insert_value( "server", Value::String( "localhost".into() ) );
template.insert_value( "port", Value::Number( 8080 ) );
template.add_file( FileDescriptor
{
template_path: PathBuf::from( "/templates/greeting.hbs" ),
file_path: PathBuf::from( "/output/greeting.txt" ),
write_mode: WriteMode::Rewrite,
});
template.add_file( FileDescriptor
{
template_path: PathBuf::from( "/templates/config.hbs" ),
file_path: PathBuf::from( "/output/config.txt" ),
write_mode: WriteMode::Rewrite,
});
println!( "File descriptors added:" );
println!( " greeting.hbs -> greeting.txt" );
println!( " config.hbs -> config.txt" );
println!();
println!( "Materializing templates..." );
template.materialize()?;
println!( "✅ Materialization complete" );
println!();
println!( "Files were written to the internal filesystem:" );
println!( " /output/greeting.txt" );
println!( " /output/config.txt" );
println!();
println!( "Note: Template API owns the filesystem, so you cannot" );
println!( "easily read back the results when using MemoryFileSystem." );
println!();
println!( "For testing with readable results, use TemplateArchive instead." );
println!();
println!( "=== Advantages of Template API ===" );
println!();
println!( "• Generic over value types (V: TemplateValue)" );
println!( "• Generic over renderers (R: TemplateRenderer)" );
println!( "• Generic over filesystems (FS: FileSystem)" );
println!( "• Direct control over materialization process" );
println!( "• Useful for custom value types or renderers" );
println!();
println!( "=== When to use Template vs TemplateArchive ===" );
println!();
println!( "Use TemplateArchive when:" );
println!( " • You need serialization (JSON/YAML)" );
println!( " • You want self-contained archives" );
println!( " • You need parameter discovery and analysis" );
println!( " • Default Value type is sufficient" );
println!();
println!( "Use Template when:" );
println!( " • You need custom value types" );
println!( " • You need custom renderers" );
println!( " • You want fine-grained control" );
println!( " • Serialization is not needed" );
println!();
println!( "✅ Example completed successfully" );
Ok( () )
}