external_content/
external_content.rs1use genfile_core::
9{
10 TemplateArchive,
11 FileRef,
12 InlineContent,
13 FileContent,
14 WriteMode,
15 Value,
16 HandlebarsRenderer,
17 MemoryFileSystem,
18 DefaultContentResolver,
19 FileSystem,
20};
21use std::path::PathBuf;
22
23fn main() -> Result< (), Box< dyn core::error::Error > >
24{
25 println!( "=== External Content Sources Example ===" );
26 println!();
27
28 let mut archive = TemplateArchive::new( "docs" );
30
31 archive.add_file_from(
33 PathBuf::from( "inline.txt" ),
34 InlineContent::new( FileContent::Text( "This is inline content".into() ) ),
35 WriteMode::Rewrite
36 );
37
38 archive.add_file_from(
40 PathBuf::from( "external_header.txt" ),
41 FileRef::new( PathBuf::from( "/templates/header.hbs" ) ),
42 WriteMode::Rewrite
43 );
44
45 archive.add_file_from(
46 PathBuf::from( "external_footer.txt" ),
47 FileRef::new( PathBuf::from( "/templates/footer.hbs" ) ),
48 WriteMode::Rewrite
49 );
50
51 println!( "Archive with mixed content sources:" );
52 println!( " Total files: {}", archive.file_count() );
53 println!();
54
55 println!( "=== Internalization (fetch and embed) ===" );
58 println!();
59
60 let mut mock_fs = MemoryFileSystem::new();
62 mock_fs.write(
63 &PathBuf::from( "/templates/header.hbs" ),
64 "=== {{title}} ===\n"
65 )?;
66 mock_fs.write(
67 &PathBuf::from( "/templates/footer.hbs" ),
68 "© {{year}} {{author}}\n"
69 )?;
70
71 let _resolver = DefaultContentResolver::new();
73
74 println!( "To internalize external content:" );
79 println!( " let resolver = CustomContentResolver::new();" );
80 println!( " archive.internalize( &resolver )?;" );
81 println!();
82 println!( "This fetches all external content and embeds it in the archive." );
83 println!();
84
85 println!( "=== Externalization (extract to files) ===" );
88 println!();
89
90 let mut inline_archive = TemplateArchive::new( "app" );
92 inline_archive.add_text_file(
93 PathBuf::from( "config.txt" ),
94 "app={{app_name}}\nversion={{version}}\n",
95 WriteMode::Rewrite
96 );
97 inline_archive.add_text_file(
98 PathBuf::from( "readme.txt" ),
99 "# {{app_name}}\n",
100 WriteMode::Rewrite
101 );
102
103 println!( "Archive before externalization:" );
104 println!( " Total files: {}", inline_archive.file_count() );
105 println!();
106
107 println!( "To externalize inline content:" );
109 println!( " let storage = CustomContentStorage::new();" );
110 println!( " archive.externalize( &storage, Path::new( \"/templates\" ) )?;" );
111 println!();
112 println!( "This extracts inline content to external files and updates references." );
113 println!();
114
115 println!( "=== Serialization ===" );
118 println!();
119
120 let json = archive.to_json_pretty()?;
121 println!( "JSON with external sources (first 600 chars):" );
122 let json_preview: String = json.chars().take( 600 ).collect();
123 println!( "{json_preview}" );
124 println!( "..." );
125 println!();
126
127 println!( "External file references are preserved in serialization." );
128 println!( "FileRef stores the path, not the content." );
129 println!();
130
131 println!( "=== Materialization ===" );
134 println!();
135
136 let mut demo_archive = TemplateArchive::new( "demo" );
138 demo_archive.add_text_file(
139 PathBuf::from( "output.txt" ),
140 "Title: {{title}}\nAuthor: {{author}}\nYear: {{year}}\n",
141 WriteMode::Rewrite
142 );
143
144 demo_archive.set_value( "title", Value::String( "External Content Guide".into() ) );
145 demo_archive.set_value( "author", Value::String( "genfile_core".into() ) );
146 demo_archive.set_value( "year", Value::Number( 2024 ) );
147
148 let renderer = HandlebarsRenderer::new();
149 let mut fs = MemoryFileSystem::new();
150
151 demo_archive.materialize_with_components(
152 PathBuf::from( "/output" ).as_path(),
153 &renderer,
154 &mut fs
155 )?;
156
157 let content = fs.read( &PathBuf::from( "/output/output.txt" ) )?;
158 println!( "Generated content:" );
159 println!( "{content}" );
160 println!();
161
162 println!( "✅ Example completed successfully" );
163
164 Ok( () )
165}