genfile_core 0.12.0

File generation tools for code generation and template materialization.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/// Serialization and file I/O operations for [`TemplateArchive`].
///
/// Provides JSON/YAML serialization, file save/load, directory packing,
/// and content internalize/externalize operations.
/// Split from mod.rs to keep both files within source file size limits.
use std ::path ::Path;

use crate ::
{
  Error,
  FileSystem,
  HandlebarsRenderer,
  RealFileSystem,
  TemplateRenderer,
  Values,
  WriteMode,
  validate_path,
};

use super ::
{
  FileContent,
  MaterializationReport,
  TemplateArchive,
};

impl TemplateArchive
{
  // === Serialization ===

  /// Serialize to JSON string
  ///
  /// # Examples
  ///
  /// ```rust
  /// use genfile_core::TemplateArchive;
  ///
  /// let archive = TemplateArchive::new("test");
  /// let json = archive.to_json().unwrap();
  /// assert!(json.contains("\"name\":\"test\""));
  /// ```
  ///
  /// # Errors
  ///
  /// Returns error if JSON serialization fails.
  #[cfg(feature = "json")]
  pub fn to_json( &self ) -> Result< String, Error >
  {
    serde_json::to_string( self )
      .map_err( | e | Error::Render( format!( "JSON serialization failed: {e}" ) ) )
  }

  /// Serialize to pretty-printed JSON
  ///
  /// # Errors
  ///
  /// Returns error if JSON serialization fails.
  #[cfg(feature = "json")]
  pub fn to_json_pretty( &self ) -> Result< String, Error >
  {
    serde_json::to_string_pretty( self )
      .map_err( | e | Error::Render( format!( "JSON serialization failed: {e}" ) ) )
  }

  /// Deserialize from JSON string
  ///
  /// # Examples
  ///
  /// ```rust
  /// use genfile_core::TemplateArchive;
  ///
  /// let json = r#"{"name":"test","version":"1.0.0","files":[],"parameters":{"descriptors":[]}}"#;
  /// let archive = TemplateArchive::from_json(json).unwrap();
  /// assert_eq!(archive.name, "test");
  /// ```
  ///
  /// # Errors
  ///
  /// Returns error if JSON deserialization fails.
  #[cfg(feature = "json")]
  pub fn from_json( json: &str ) -> Result< Self, Error >
  {
    serde_json::from_str( json )
      .map_err( | e | Error::Render( format!( "JSON deserialization failed: {e}" ) ) )
  }

  /// Serialize to YAML string
  ///
  /// # Errors
  ///
  /// Returns error if YAML serialization fails.
  #[cfg(feature = "yaml")]
  pub fn to_yaml( &self ) -> Result< String, Error >
  {
    serde_yaml::to_string( self )
      .map_err( | e | Error::Render( format!( "YAML serialization failed: {e}" ) ) )
  }

  /// Deserialize from YAML string
  ///
  /// # Errors
  ///
  /// Returns error if YAML deserialization fails.
  #[cfg(feature = "yaml")]
  pub fn from_yaml( yaml: &str ) -> Result< Self, Error >
  {
    serde_yaml::from_str( yaml )
      .map_err( | e | Error::Render( format!( "YAML deserialization failed: {e}" ) ) )
  }

  // === Materialization ===

  /// Materialize archive to filesystem at `base_path`
  ///
  /// Creates all directories and files, applying template rendering
  /// with current parameter values.
  ///
  /// # Errors
  ///
  /// Returns error if template rendering or file creation fails.
  pub fn materialize( &self, base_path: &Path ) -> Result< MaterializationReport, Error >
  {
    let renderer = HandlebarsRenderer::new();
    let mut filesystem = RealFileSystem::new();
    self.materialize_with_components( base_path, &renderer, &mut filesystem )
  }

  /// Materialize with custom components (for testing)
  ///
  /// # Errors
  ///
  /// Returns error if template rendering, file creation, or path validation fails.
  pub fn materialize_with_components<R, FS>(
    &self,
    base_path: &Path,
    renderer: &R,
    filesystem: &mut FS
  ) -> Result< MaterializationReport, Error >
  where
    R: TemplateRenderer,
    FS: FileSystem,
  {
    let mut report = MaterializationReport::default();

    // Get values or use empty if none set
    let values = self.values.as_ref().map( Values::to_serializable ).unwrap_or_default();

    // Track directories that will be created
    // RealFileSystem creates directories automatically in write()
    // MemoryFileSystem doesnt need directory creation
    for dir in self.list_directories()
    {
      report.directories_created.push( dir );
    }

    // Process each file
    for file in &self.files
    {
      // Validate path for security (prevent directory traversal)
      validate_path( &file.path )?;

      let full_path = base_path.join( &file.path );

      let final_content = match &file.content
      {
        FileContent::Text( template ) =>
        {
          // Render template
          renderer.render( template, &values )?
        }
        FileContent::Binary( bytes ) =>
        {
          // Convert binary to string for filesystem write
          // In real impl, FileSystem trait would need write_bytes method
          String::from_utf8_lossy( bytes ).to_string()
        }
      };

      let existed = filesystem.exists( &full_path );

      // Write file (RealFileSystem creates parent dirs automatically)
      filesystem.write( &full_path, &final_content )?;

      report.total_bytes_written += final_content.len();

      if existed
      {
        report.files_updated.push( file.path.clone() );
      }
      else
      {
        report.files_created.push( file.path.clone() );
      }
    }

    Ok( report )
  }

  /// Materialize archive with custom content resolver.
  ///
  /// Allows using external content sources (files, URLs, custom providers)
  /// instead of only inline content.
  ///
  /// # Parameters
  ///
  /// - `base_path`: Base directory for output files
  /// - `renderer`: Template rendering engine
  /// - `filesystem`: File system for writing output
  /// - `resolver`: Content resolver for external sources
  ///
  /// # Examples
  ///
  /// ```rust,no_run
  /// use genfile_core::{ TemplateArchive, DefaultContentResolver, HandlebarsRenderer, RealFileSystem };
  /// use std::path::Path;
  ///
  /// let archive = TemplateArchive::new( "test" );
  /// let resolver = DefaultContentResolver::new();
  /// let renderer = HandlebarsRenderer::new();
  /// let mut filesystem = RealFileSystem::new();
  ///
  /// archive.materialize_with_resolver(
  ///     Path::new( "/output" ),
  ///     &renderer,
  ///     &mut filesystem,
  ///     &resolver
  /// ).unwrap();
  /// ```
  ///
  /// # Errors
  ///
  /// Returns error if content resolution, template rendering, or file creation fails.
  #[cfg(feature = "external_content")]
  pub fn materialize_with_resolver<R, FS, CR>(
    &self,
    base_path: &Path,
    renderer: &R,
    filesystem: &mut FS,
    resolver: &CR
  ) -> Result< MaterializationReport, Error >
  where
    R: TemplateRenderer,
    FS: FileSystem,
    CR: crate::ContentResolver,
  {
    let mut report = MaterializationReport::default();

    // Get values or use empty if none set
    let values = self.values.as_ref().map( Values::to_serializable ).unwrap_or_default();

    // Track directories
    for dir in self.list_directories()
    {
      report.directories_created.push( dir );
    }

    // Process each file
    for file in &self.files
    {
      // Validate path for security (prevent directory traversal)
      validate_path( &file.path )?;

      let full_path = base_path.join( &file.path );

      // Resolve content from source (external or inline)
      let content = if let Some( source ) = &file.content_source
      {
        // Use external source via resolver
        resolver.resolve( source )?
      }
      else
      {
        // Use inline content
        file.content.clone()
      };

      // Render content
      let final_content = match &content
      {
        FileContent::Text( template ) =>
        {
          renderer.render( template, &values )?
        }
        FileContent::Binary( bytes ) =>
        {
          String::from_utf8_lossy( bytes ).to_string()
        }
      };

      let existed = filesystem.exists( &full_path );

      // Write file
      filesystem.write( &full_path, &final_content )?;

      report.total_bytes_written += final_content.len();

      if existed
      {
        report.files_updated.push( file.path.clone() );
      }
      else
      {
        report.files_created.push( file.path.clone() );
      }
    }

    Ok( report )
  }

  /// Materialize archive using `ContentStorage` abstraction.
  ///
  /// Instead of using `FileSystem` trait, uses `ContentStorage` for maximum
  /// flexibility. This allows writing to databases, cloud storage, etc.
  ///
  /// # Parameters
  ///
  /// - `base_path`: Base directory for output files
  /// - `renderer`: Template rendering engine
  /// - `storage`: Content storage backend
  /// - `resolver`: Content resolver for external sources
  ///
  /// # Examples
  ///
  /// ```rust,no_run
  /// use genfile_core::{ TemplateArchive, DefaultContentResolver, DefaultContentStorage, HandlebarsRenderer };
  /// use std::path::Path;
  ///
  /// let archive = TemplateArchive::new( "test" );
  /// let resolver = DefaultContentResolver::new();
  /// let mut storage = DefaultContentStorage::new();
  /// let renderer = HandlebarsRenderer::new();
  ///
  /// archive.materialize_with_storage(
  ///     Path::new( "/output" ),
  ///     &renderer,
  ///     &mut storage,
  ///     &resolver
  /// ).unwrap();
  /// ```
  ///
  /// # Errors
  ///
  /// Returns error if content resolution, storage operations, or file creation fails.
  #[cfg(feature = "external_content")]
  pub fn materialize_with_storage<R, CS, CR>(
    &self,
    base_path: &Path,
    renderer: &R,
    storage: &mut CS,
    resolver: &CR
  ) -> Result< MaterializationReport, Error >
  where
    R: TemplateRenderer,
    CS: crate::ContentStorage,
    CR: crate::ContentResolver,
  {
    let mut report = MaterializationReport::default();

    // Get values or use empty if none set
    let values = self.values.as_ref().map( Values::to_serializable ).unwrap_or_default();

    // Track directories
    for dir in self.list_directories()
    {
      report.directories_created.push( dir );
    }

    // Process each file
    for file in &self.files
    {
      // Validate path for security (prevent directory traversal)
      validate_path( &file.path )?;

      let full_path = base_path.join( &file.path );

      // Resolve content from source (external or inline)
      let content = if let Some( source ) = &file.content_source
      {
        resolver.resolve( source )?
      }
      else
      {
        file.content.clone()
      };

      // Render content
      let rendered_content = match &content
      {
        FileContent::Text( template ) =>
        {
          FileContent::Text( renderer.render( template, &values )? )
        }
        FileContent::Binary( bytes ) =>
        {
          FileContent::Binary( bytes.clone() )
        }
      };

      // Store using ContentStorage
      storage.store( &full_path, &rendered_content )?;

      report.total_bytes_written += match &rendered_content
      {
        FileContent::Text( s ) => s.len(),
        FileContent::Binary( b ) => b.len(),
      };

      report.files_created.push( file.path.clone() );
    }

    Ok( report )
  }

  // === Pack/Unpack Operations ===

  /// Create archive from directory tree.
  ///
  /// Scans a directory recursively and packs all files into an archive.
  /// Text files are stored as text content, binary files as binary content.
  ///
  /// # Parameters
  ///
  /// - `name`: Archive name
  /// - `base_path`: Root directory to pack
  ///
  /// # Returns
  ///
  /// New archive containing all files from directory tree
  ///
  /// # Errors
  ///
  /// Returns error if directory cant be read or files cant be accessed
  ///
  /// # Examples
  ///
  /// ```rust,no_run
  /// use genfile_core::TemplateArchive;
  /// use std::path::Path;
  ///
  /// let archive = TemplateArchive::pack_from_dir(
  ///     "my-template",
  ///     Path::new( "/templates/project" )
  /// ).unwrap();
  ///
  /// println!( "Packed {} files", archive.file_count() );
  /// ```
  pub fn pack_from_dir( name: impl Into< String >, base_path: &Path ) -> Result< Self, Error >
  {
    let mut archive = Self::new( name );

    // Recursively walk directory
    fn visit_dir( archive: &mut TemplateArchive, base: &Path, current: &Path ) -> Result< (), Error >
    {
      for entry in std::fs::read_dir( current )?
      {
        let entry = entry?;
        let path = entry.path();

        if path.is_dir()
        {
          visit_dir( archive, base, &path )?;
        }
        else if path.is_file()
        {
          // Get relative path from base
          let rel_path = path.strip_prefix( base )
            .map_err( | e | Error::Render( format!( "Path error: {e}" ) ) )?
            .to_path_buf();

          // Read file
          let data = std::fs::read( &path )?;

          // Detect if text or binary
          let content = match String::from_utf8( data.clone() )
          {
            Ok( text ) => FileContent::Text( text ),
            Err( _ ) => FileContent::Binary( data ),
          };

          archive.add_file( rel_path, content, WriteMode::Rewrite );
        }
      }

      Ok( () )
    }

    visit_dir( &mut archive, base_path, base_path )?;

    Ok( archive )
  }

  /// Internalize all external content references.
  ///
  /// Resolves all external sources (files, URLs) and converts them to
  /// inline content. This makes the archive self-contained.
  ///
  /// # Parameters
  ///
  /// - `resolver`: Content resolver to fetch external content
  ///
  /// # Returns
  ///
  /// Ok(()) on success, Error if any external source cant be resolved
  ///
  /// # Examples
  ///
  /// ```rust,no_run
  /// use genfile_core::{ TemplateArchive, DefaultContentResolver };
  ///
  /// let mut archive = TemplateArchive::new( "test" );
  /// // ... add files with external sources ...
  ///
  /// let resolver = DefaultContentResolver::new();
  /// archive.internalize( &resolver ).unwrap();
  ///
  /// // Now all content is inline
  /// ```
  ///
  /// # Errors
  ///
  /// Returns error if content resolution fails for any external reference.
  #[cfg(feature = "external_content")]
  pub fn internalize< CR >( &mut self, resolver: &CR ) -> Result< (), Error >
  where
    CR: crate::ContentResolver,
  {
    for file in &mut self.files
    {
      if let Some( source ) = &file.content_source
      {
        // Resolve external content
        let content = resolver.resolve( source )?;

        // Replace inline content
        file.content = content;

        // Remove external source reference
        file.content_source = None;
      }
    }

    Ok( () )
  }

  /// Externalize inline content to file references.
  ///
  /// Writes all inline content to files in the specified directory
  /// and replaces inline content with file references. This reduces
  /// archive size when serialized.
  ///
  /// # Parameters
  ///
  /// - `base_path`: Directory where content files will be written
  ///
  /// # Errors
  ///
  /// Returns error if directory creation or file writing fails.
  ///
  /// # Examples
  ///
  /// ```rust,no_run
  /// use genfile_core::TemplateArchive;
  /// use std::path::Path;
  ///
  /// let mut archive = TemplateArchive::new( "test" );
  /// // ... add files with inline content ...
  ///
  /// archive.externalize( Path::new( "/archive-content" ) ).unwrap();
  ///
  /// // Now content is stored in /archive-content/ and referenced
  /// ```
  #[cfg(feature = "external_content")]
  pub fn externalize( &mut self, base_path: &Path ) -> Result< (), Error >
  {
    // Create base directory
    std::fs::create_dir_all( base_path )?;

    for file in &mut self.files
    {
      if file.content_source.is_none()
      {
        // Generate unique filename for content
        let content_filename = format!( "{}.content", file.path.display() ).replace( '/', "_" );
        let content_path = base_path.join( &content_filename );

        // Write content to file
        match &file.content
        {
          FileContent::Text( text ) =>
          {
            std::fs::write( &content_path, text )?;
          }
          FileContent::Binary( bytes ) =>
          {
            std::fs::write( &content_path, bytes )?;
          }
        }

        // Replace with file reference
        file.content_source = Some( crate::ContentSource::File { path: content_path } );

        // Clear inline content (or leave placeholder)
        file.content = FileContent::Text( String::new() );
      }
    }

    Ok( () )
  }

  /// Save archive to file using JSON format.
  ///
  /// # Parameters
  ///
  /// - `path`: File path where archive will be saved
  ///
  /// # Errors
  ///
  /// Returns error if JSON serialization or file writing fails.
  ///
  /// # Examples
  ///
  /// ```rust,no_run
  /// use genfile_core::TemplateArchive;
  /// use std::path::Path;
  ///
  /// let archive = TemplateArchive::new( "test" );
  /// archive.save_to_file( Path::new( "archive.json" ) ).unwrap();
  /// ```
  #[cfg(feature = "json")]
  pub fn save_to_file( &self, path: &Path ) -> Result< (), Error >
  {
    let json = self.to_json_pretty()?;
    std::fs::write( path, json )?;
    Ok( () )
  }

  /// Load archive from JSON file.
  ///
  /// # Parameters
  ///
  /// - `path`: File path to load archive from
  ///
  /// # Errors
  ///
  /// Returns error if file reading or JSON deserialization fails.
  ///
  /// # Examples
  ///
  /// ```rust,no_run
  /// use genfile_core::TemplateArchive;
  /// use std::path::Path;
  ///
  /// let archive = TemplateArchive::load_from_file( Path::new( "archive.json" ) ).unwrap();
  /// ```
  #[cfg(feature = "json")]
  pub fn load_from_file( path: &Path ) -> Result< Self, Error >
  {
    let json = std::fs::read_to_string( path )?;
    Self::from_json( &json )
  }
}