pub struct FileRef(pub PathBuf);Expand description
File reference source type.
Represents a reference to a file in the local filesystem.
§Examples
use genfile_core::FileRef;
use std::path::PathBuf;
let source = FileRef::new( "/templates/main.hbs" );
let source = FileRef( PathBuf::from( "/templates/main.hbs" ) );Tuple Fields§
§0: PathBufImplementations§
Source§impl FileRef
 
impl FileRef
Sourcepub fn new(path: impl Into<PathBuf>) -> Self
 
pub fn new(path: impl Into<PathBuf>) -> Self
Create a new file reference
Examples found in repository?
examples/external_content.rs (line 41)
23fn main() -> Result< (), Box< dyn core::error::Error > >
24{
25  println!( "=== External Content Sources Example ===" );
26  println!();
27
28  // Create archive
29  let mut archive = TemplateArchive::new( "docs" );
30
31  // Add file with inline content (normal way)
32  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  // Add file referencing external file
39  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  // === Internalization Example ===
56
57  println!( "=== Internalization (fetch and embed) ===" );
58  println!();
59
60  // Create a mock filesystem with template content
61  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  // Create resolver that can fetch from our mock filesystem
72  let _resolver = DefaultContentResolver::new();
73
74  // Note: DefaultContentResolver doesnt support actual file fetching,
75  // but demonstrates the API. In real usage, implement custom ContentResolver
76  // that reads from disk, HTTP, database, etc.
77
78  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  // === Externalization Example ===
86
87  println!( "=== Externalization (extract to files) ===" );
88  println!();
89
90  // Create archive with inline content
91  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  // Externalize would extract inline content to separate files
108  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  // === Serialization with External Sources ===
116
117  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  // === Materialization ===
132
133  println!( "=== Materialization ===" );
134  println!();
135
136  // For demonstration, use inline content version
137  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}Trait Implementations§
Source§impl IntoContentSource for FileRef
 
impl IntoContentSource for FileRef
Source§fn into_content_source(self) -> ContentSource
 
fn into_content_source(self) -> ContentSource
Convert self into a 
ContentSourceimpl Eq for FileRef
impl StructuralPartialEq for FileRef
Auto Trait Implementations§
impl Freeze for FileRef
impl RefUnwindSafe for FileRef
impl Send for FileRef
impl Sync for FileRef
impl Unpin for FileRef
impl UnwindSafe for FileRef
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
    T: Clone,
 
impl<T> CloneToUninit for Twhere
    T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
 
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
 
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
 
fn equivalent(&self, key: &K) -> bool
Compare self to 
key and return true if they are equal.