pub struct Template {
pub name: String,
pub package: String,
pub output: String,
pub engine: String,
pub text: String,
pub source: Option<String>,
pub accepts: Accepts,
pub requires: Vec<Name>,
pub config: Map<String, Value>,
pub settings: Option<Value>,
pub delimiters: Option<Delimiters>,
pub artifacts: Vec<TemplateArtifact>,
}Expand description
A compose template: a text with named places for an output’s regions, applied by a template engine (see the module documentation).
A feature contributes one to TEMPLATES_SLOT; Template::from_data makes one from
a description given as data and the readers of the files it gives.
Fields§
§name: StringThe compose template’s name, which the setting compose.template gives.
Template::from_data reads it from the key name of the description.
package: StringThe package that ships the compose template, as messages show it (for instance the
name of a compiled package, or the identity of an extension’s package,
<identity>#<package>). A
problem with two compose templates of the same name gives the packages that ship
them.
output: StringThe name of the output it serves. Template::from_data reads it from the key
output of the description.
engine: StringThe name of the template engine that applies it.
text: StringIts text, in the template engine’s language; empty for a template engine that reads
no text (content-only). Template::from_data reads it from the file that the key
main of the description gives, and leaves it empty without main.
source: Option<String>The place of its text: the qualified path of the file that the text was read from
(ext+NAME:templates/html/page/page.html, std:templates/html/simple/simple.html;
see the module documentation). A template
engine reads a path written without a root beside that file. None for a text that
no file holds: a compose template made in code, or a description without main.
accepts: AcceptsThe pieces it accepts: the pieces that a render request places on the backend of
the compose template’s output (accepted_pieces).
requires: Vec<Name>The pieces it requires: each is placed although it may be declared off by default
(select_pieces), and a plan in which no stage provides one of them fails.
config: Map<String, Value>Its own settings, with their defaults, which the render option template_config
can change (ComposeOptions::template_config). Template::from_data reads them
from the default of each setting of settings. For a
compose template written as a module of an extension, they are the defaults that
the module’s describe returns.
settings: Option<Value>The JSON Schema of its own settings: an object schema, each of whose properties
is one setting with its type, its description, its default and its marks
(Mark). The feature that contributes the compose template declares it as the
entry template_config.OUTPUT.NAME of the feature compose
(declare_settings).
Template::from_datareads it from the keysettingsof the description; a description without that key gives a schema with no setting.- For a compose template written as a module of an extension, it is the schema
that the extension declares for the module, each setting given the default that
configholds. - It is
Nonefor a compose template that declares nothing of its settings (one made in code): what is written for it is given to it as written.
delimiters: Option<Delimiters>The delimiters of its language, where they differ from the template engine’s usual ones.
artifacts: Vec<TemplateArtifact>The files it ships, in the order of its description (see the module documentation).
Implementations§
Source§impl Template
impl Template
Sourcepub fn from_data(
package: &str,
description: Value,
text: &dyn Fn(&str) -> Result<Option<String>, String>,
files: &dyn Fn(&str) -> Result<Option<Vec<u8>>, String>,
folders: &dyn Fn(&str) -> Result<Option<Vec<(String, Vec<u8>)>>, String>,
place: &dyn Fn(&str) -> Option<String>,
path: &dyn Fn(&str) -> Result<String, String>,
) -> Result<Template, String>
pub fn from_data( package: &str, description: Value, text: &dyn Fn(&str) -> Result<Option<String>, String>, files: &dyn Fn(&str) -> Result<Option<Vec<u8>>, String>, folders: &dyn Fn(&str) -> Result<Option<Vec<(String, Vec<u8>)>>, String>, place: &dyn Fn(&str) -> Option<String>, path: &dyn Fn(&str) -> Result<String, String>, ) -> Result<Template, String>
The compose template of the package package (the factory’s name), from its
description as data, text, which reads the file of its text, files, which reads
a file the description lists, folders, which reads a folder it lists,
place, which gives the place of a file it reads, and path, which reads a path
that a setting’s default writes.
The description is a map of these keys:
name: the compose template’s name, by which the settingcompose.templatechooses it;output: the output the compose template serves;engine: the name of its template engine;main(optional): the path of the file that holds its text, relative to the folder of the description; without it, the compose template’s text is empty;accepts: the pieces it accepts,allor a list of piece names;requires(optional): the pieces it requires, a list of piece names;settings(optional): the JSON Schema of its own settings (Template::settings), an object schema whosepropertiesare the settings, each with itstype, itsdescription(plain text), itsdefaultand its marks (x-flm-marks); every setting gives adefault, and these are the compose template’s defaults (Template::config), each text of a default that the schema marksMark::Pathaspathgives it; without it, the compose template has no setting;delimiters(optional): the delimiters of its language where they differ from the usual ones (Delimiters);artifacts(optional): the files and folders it ships (see the module documentation), a map from each one’s name to a map of these keys:file, the file’s path, relative to the folder of the description, whose extension is the artifact’s;template(optional, defaultfalse), whether the file’s text is applied as a template of the same engine;media_type(optional), the file’s media type, by default the one its extension gives (media_type_of). In place offile, the keydirgives the path of a folder, relative to the folder of the description, shipped whole as one folder artifact (TemplateArtifact::Folder);templateandmedia_typeare refused with it, since a folder is never applied as a template and its files’ media types come from their extensions.
text(path) returns the text of the file at path as the description’s main
writes it, Ok(None) when there is no such file, and Err with a text for a person
when it cannot be read. It is called once when the description gives main, and not
at all otherwise.
files(path) returns the bytes of the file at path as the description writes it,
Ok(None) when there is no such file, and Err with a text for a person when it
cannot be read. It is called once per file listed.
folders(path) returns the files of the folder at path as the description writes
it, each by its path relative to that folder (forward slashes, no leading slash) and
its bytes, listed at every depth in lexical order of the paths; Ok(None) when
there is no such folder; and Err with a complete text for a person when the folder
cannot be shipped (it cannot be read, or its path is refused), which from_data
returns as it is. The reader decides which names it leaves out (an extension’s
reader leaves out the names beginning with .). It is called once per folder
listed.
place(path) returns the place of the file at path as the description writes it:
the qualified path of the file (see the module
documentation), or None where the files
read have no qualified paths. It is called for the text and for each file listed,
once each has been read, and gives Template::source and the field source of
TemplateArtifact::File.
path(written) returns the path written, a text of a setting’s default that the
schema marks as a path, as the reader of the setting receives it: a qualified path,
a bare path given the root and the folder of the description; or Err with a text
for a person when the path has no meaning there. The defaults of
config and of settings both hold its
answer.
§Examples
use flm_core::stdlib::features::compose::{Accepts, Template};
use serde_json::json;
let template = Template::from_data(
"mine",
json!({ "name": "page", "output": "html", "engine": "host", "main": "page.html", "accepts": "all" }),
&|path| Ok((path == "page.html").then(|| String::from("<main>{{ regions.content }}</main>"))),
&|_| Ok(None),
&|_| Ok(None),
&|path| Some(format!("ext+mine:{path}")),
&|written| Ok(format!("ext+mine:{written}")),
)
.unwrap();
assert_eq!((template.name.as_str(), template.output.as_str()), ("page", "html"));
assert_eq!(template.accepts, Accepts::All);
assert_eq!(template.text, "<main>{{ regions.content }}</main>");
assert_eq!(template.source.as_deref(), Some("ext+mine:page.html"));§Errors
Returns a text, for a person to read, that says what is wrong:
- the description cannot be read: it is not a map, it holds an unknown key, it
lacks
name,output,engineoraccepts, a value has the wrong shape,acceptsis neitherallnor a list of names,settingsis not an object schema withproperties, a setting gives nodefault, orpathrefuses the path of a default; - its text cannot be read: there is no file at
main, or it cannot be read (the text oftext’sErr); - a file it lists cannot be shipped: there is no such file, it cannot be read, it
has no extension, its media type is not known, or it is applied as a template
while it is not text or the engine is
content-only; - a folder it lists cannot be shipped: the artifact gives both
fileanddir, or neither, ordirtogether withtemplateormedia_type, there is no such folder, it holds no file, or the text offolders’sErr.
Trait Implementations§
impl StructuralPartialEq for Template
Auto Trait Implementations§
impl Forget for Template
impl Freeze for Template
impl Move for Template
impl RefUnwindSafe for Template
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl UnsafeUnpin for Template
impl UnwindSafe for Template
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> ⓘ
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> ⓘ
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 more