pub struct Template { /* private fields */ }Expand description
A loaded template: its chain, its declared set and its digest.
Implementations§
Source§impl Template
impl Template
Sourcepub fn expand(&self, answers: &Answers) -> Result<Self, TemplateError>
pub fn expand(&self, answers: &Answers) -> Result<Self, TemplateError>
This template with every template an expression names for answers added to its
chain — each with the files its own literals name — and their front matter merged into
the declared set under the same rules as the rest of the chain.
Which file an expression names can depend on the answers, so it is found by rendering: leniently, from the answers that fit their declarations and the defaults of the rest, every tag naming a template by an expression reporting what it named. A file found that way may declare a variable, or a nearer default, that decides what an expression names, so it repeats until a render names what the one before it did; a file only an earlier render named is not part of the chain. Each file so found joins the chain at the tag that names it, exactly as a file a literal names would: its nearness is its own distance from the rendered template. The expanded chain is in first-load order — the order that last render first read each file, then any it did not read. A template named by no expression expands to its own files in that order.
Template::unanswered, Template::resolve and Template::render each expand
first, so a caller need not.
§Errors
TemplateError::Malformed, TemplateError::Unreadable and
TemplateError::NotFound for a file found this way, or one its literals name, as for
any chain file; TemplateError::ChainConflict for a declaration of it whose type
or items another chain file’s contradicts; and TemplateError::Malformed for the
rendered template when the files its expressions name give defaults that name one
another in turn, so that what they name never settles.
Sourcepub fn variables(&self) -> &[TemplateVariable]
pub fn variables(&self) -> &[TemplateVariable]
The declared set, in declaration order along the chain.
Sourcepub fn chain(&self) -> impl Iterator<Item = &str>
pub fn chain(&self) -> impl Iterator<Item = &str>
The resolved name of every file of the chain, in first-load order: the files
Template::digest is taken over, in its order. For a loaded template these are the
rendered one and each extends, include and import its literals reach; what
Template::expand answers adds each template an expression names.
Sourcepub fn describe(&self) -> TemplateVariables
pub fn describe(&self) -> TemplateVariables
The declared set as template variables reports it.
Sourcepub fn unanswered(
&self,
answers: &Answers,
) -> Result<Vec<TemplateVariable>, TemplateError>
pub fn unanswered( &self, answers: &Answers, ) -> Result<Vec<TemplateVariable>, TemplateError>
The declared variables answers leaves unanswered, in declaration order — whether or
not a default covers them — over the chain Template::expand reaches for answers.
What a prompting caller asks for; answering one can name a further template, so a
caller asks again until nothing new is left.
§Errors
TemplateError::UnknownAnswer and TemplateError::MistypedAnswer, as
Template::render refuses them.
Sourcepub fn resolve(
&self,
answers: &Answers,
) -> Result<BTreeMap<String, Value>, TemplateError>
pub fn resolve( &self, answers: &Answers, ) -> Result<BTreeMap<String, Value>, TemplateError>
Every declared variable’s value — over the chain Template::expand reaches for
answers — its answer, else its default, else null for an optional one.
§Errors
TemplateError::UnknownAnswer naming every answer to no declared variable,
TemplateError::MistypedAnswer for an answer not of its variable’s type, and
TemplateError::MissingRequired naming every required variable left unanswered.
Sourcepub fn render(
&self,
answers: &Answers,
) -> Result<RenderedTemplate, TemplateError>
pub fn render( &self, answers: &Answers, ) -> Result<RenderedTemplate, TemplateError>
Render the template from answers, over the chain Template::expand reaches for
them: a template an expression names is part of the chain, its declarations part of
the declared set the answers are held to, and its bytes part of the digest.
§Errors
Every refusal of Template::expand and Template::resolve, and
TemplateError::Render for a render that fails — an undefined name among them.