FileRef

Struct FileRef 

Source
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: PathBuf

Implementations§

Source§

impl FileRef

Source

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 Clone for FileRef

Source§

fn clone(&self) -> FileRef

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FileRef

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&str> for FileRef

Source§

fn from(path: &str) -> Self

Converts to this type from the input type.
Source§

impl From<PathBuf> for FileRef

Source§

fn from(path: PathBuf) -> Self

Converts to this type from the input type.
Source§

impl IntoContentSource for FileRef

Source§

fn into_content_source(self) -> ContentSource

Convert self into a ContentSource
Source§

impl PartialEq for FileRef

Source§

fn eq(&self, other: &FileRef) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for FileRef

Source§

impl StructuralPartialEq for FileRef

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.