Skip to main content

Template

Struct Template 

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

The compose template’s name, which the setting compose.template gives. Template::from_data reads it from the key name of the description.

§package: String

The 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: String

The name of the output it serves. Template::from_data reads it from the key output of the description.

§engine: String

The name of the template engine that applies it.

§text: String

Its 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: Accepts

The 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_data reads it from the key settings of 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 config holds.
  • It is None for 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

Source

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 setting compose.template chooses 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, all or 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 whose properties are the settings, each with its type, its description (plain text), its default and its marks (x-flm-marks); every setting gives a default, and these are the compose template’s defaults (Template::config), each text of a default that the schema marks Mark::Path as path gives 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, default false), 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 of file, the key dir gives the path of a folder, relative to the folder of the description, shipped whole as one folder artifact (TemplateArtifact::Folder); template and media_type are 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, engine or accepts, a value has the wrong shape, accepts is neither all nor a list of names, settings is not an object schema with properties, a setting gives no default, or path refuses the path of a default;
  • its text cannot be read: there is no file at main, or it cannot be read (the text of text’s Err);
  • 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 file and dir, or neither, or dir together with template or media_type, there is no such folder, it holds no file, or the text of folders’s Err.

Trait Implementations§

Source§

impl Clone for Template

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Template

Source§

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

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

impl PartialEq for Template

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Template

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.