use genfile_core::
{
TemplateArchive,
WriteMode,
Value,
HandlebarsRenderer,
MemoryFileSystem,
FileSystem,
};
use std::path::PathBuf;
fn main() -> Result< (), Box< dyn core::error::Error > >
{
println!( "=== Binary Files Example ===" );
println!();
let mut archive = TemplateArchive::new( "website" );
archive.add_text_file(
PathBuf::from( "index.html" ),
"<!DOCTYPE html>\n<html>\n<head><title>{{title}}</title></head>\n<body>\n <h1>{{title}}</h1>\n <img src=\"logo.png\" alt=\"Logo\">\n</body>\n</html>",
WriteMode::Rewrite
);
let png_bytes = vec![ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A ];
archive.add_binary_file(
PathBuf::from( "logo.png" ),
png_bytes
);
let jpeg_bytes = vec![ 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46 ];
archive.add_binary_file(
PathBuf::from( "background.jpg" ),
jpeg_bytes
);
let ico_bytes = vec![ 0x00, 0x00, 0x01, 0x00, 0x01, 0x00 ];
archive.add_binary_file(
PathBuf::from( "favicon.ico" ),
ico_bytes
);
println!( "Archive statistics:" );
println!( " Total files: {}", archive.file_count() );
println!( " Text files: {}", archive.text_file_count() );
println!( " Binary files: {}", archive.binary_file_count() );
println!();
archive.set_value( "title", Value::String( "My Website".into() ) );
let json = archive.to_json_pretty()?;
println!( "JSON representation (truncated):" );
let json_preview: String = json.chars().take( 500 ).collect();
println!( "{json_preview}..." );
println!();
let restored = TemplateArchive::from_json( &json )?;
println!( "✅ Successfully deserialized from JSON" );
println!( " Restored files: {}", restored.file_count() );
println!();
let renderer = HandlebarsRenderer::new();
let mut fs = MemoryFileSystem::new();
restored.materialize_with_components(
PathBuf::from( "/website" ).as_path(),
&renderer,
&mut fs
)?;
println!( "Generated files:" );
for path in [ "index.html", "logo.png", "background.jpg", "favicon.ico" ]
{
let full_path = PathBuf::from( format!( "/website/{path}" ) );
let exists = fs.exists( &full_path );
println!( " {} - {}", path, if exists { "✅ exists" } else { "❌ missing" } );
}
println!();
let html = fs.read( &PathBuf::from( "/website/index.html" ) )?;
println!( "Generated HTML:" );
println!( "{html}" );
println!();
println!( "✅ Example completed successfully" );
Ok( () )
}