pub struct Generator {
pub pipeline: Pipeline,
pub ctx: GenContext,
}Expand description
Main generator that orchestrates template processing and file generation
Fields§
§pipeline: Pipeline§ctx: GenContextImplementations§
Source§impl Generator
impl Generator
Sourcepub fn new(pipeline: Pipeline, ctx: GenContext) -> Self
pub fn new(pipeline: Pipeline, ctx: GenContext) -> Self
Create a new generator with a pipeline and context
§Example
use ggen_core::generator::{Generator, GenContext};
use ggen_core::pipeline::Pipeline;
use std::path::PathBuf;
let pipeline = Pipeline::new()?;
let ctx = GenContext::new(
PathBuf::from("template.tmpl"),
PathBuf::from("output")
);
let generator = Generator::new(pipeline, ctx);Sourcepub fn generate(&mut self) -> Result<PathBuf>
pub fn generate(&mut self) -> Result<PathBuf>
Generate output from the template
Processes the template, renders it with the provided context, and writes the output to the specified location.
§Returns
Returns the path to the generated file. The path is returned even in
dry run mode (when dry_run is true), allowing you to preview where
the file would be written without actually creating it.
The output path is determined as follows:
- If the template frontmatter specifies a
tofield, that path is used (rendered with template variables) - Otherwise, the output path defaults to the template filename with a
.outextension in the output root directory
§Behavior
- Parent directories: Automatically creates parent directories as needed
- Frozen sections: If the output file already exists and contains frozen
sections (marked with
# frozencomments), those sections are preserved and merged with the new generated content - Dry run: When
dry_runistrue, the file is not written to disk, but the path is still returned
§Errors
Returns an error if:
- The template file cannot be read
- The template syntax is invalid
- Template variables are missing or invalid
- RDF processing fails (if RDF is used)
- The template path has no file stem (cannot determine default output name)
- The output file cannot be written (unless in dry run mode)
- File system permissions are insufficient
- Parent directories cannot be created
§Examples
§Success case
use ggen_core::generator::{Generator, GenContext};
use ggen_core::pipeline::Pipeline;
use std::collections::BTreeMap;
use std::path::PathBuf;
let pipeline = Pipeline::new()?;
let mut vars = BTreeMap::new();
vars.insert("name".to_string(), "MyApp".to_string());
let ctx = GenContext::new(
PathBuf::from("template.tmpl"),
PathBuf::from("output")
).with_vars(vars);
let mut generator = Generator::new(pipeline, ctx);
let output_path = generator.generate()?;
println!("Generated: {:?}", output_path);§Error case - Template file not found
use ggen_core::generator::{Generator, GenContext};
use ggen_core::pipeline::Pipeline;
use std::path::PathBuf;
let pipeline = Pipeline::new()?;
let ctx = GenContext::new(
PathBuf::from("nonexistent.tmpl"), // File doesn't exist
PathBuf::from("output")
);
let mut generator = Generator::new(pipeline, ctx);
// This will fail because the template file doesn't exist
let result = generator.generate();
assert!(result.is_err());Auto Trait Implementations§
impl Freeze for Generator
impl !RefUnwindSafe for Generator
impl Send for Generator
impl Sync for Generator
impl Unpin for Generator
impl !UnwindSafe for Generator
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> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request