pub struct SettingsDecl { /* private fields */ }Expand description
A feature’s declaration of its settings: the Rust types they are read into, each with its binding, and marks on some of their keys.
A feature returns its declaration from
FlmFeature::settings. The
registry build declares the feature’s option types from it, and a package
factory lists it beside the feature’s name
(PackageFactory). A host uses it to
read the feature’s settings from data (SettingsDecl::read), to know the
keys and their marks, and to describe them (SettingsDecl::json_schema).
Create one with SettingsDecl::new, then add at most one configuration
(config or config_from),
any number of option types (options) and marks on
the values they declare (merge_entries,
remove_false,
default_on_null,
markup, path), each method
taking and returning the declaration. Two types that declare the same key,
and a mark whose path reaches no node of the schema, make these methods
panic: a declaration that contradicts itself is a mistake in the feature’s
code. The parts declared from data
(config_data,
config_data_read,
options_data) read their marks from the
nodes of their schema, and return a fault of the declaration as an error.
See the module documentation.
§Examples
use flm_core::core::settings::SettingsDecl;
use flm_core::model::options::Bind;
#[derive(Debug, Clone, Default, Hash, serde::Deserialize, schemars::JsonSchema)]
#[serde(default, deny_unknown_fields)]
struct PageOptions {
/// The page's width, in characters.
width: u32,
}
let decl = SettingsDecl::new("page").options::<PageOptions>(Bind::Render, "The page.");
assert_eq!(decl.keys().map(|key| key.name).collect::<Vec<_>>(), ["width"]);
let data = serde_json::json!({ "width": 72 });
let settings = decl.read(data.as_object().unwrap()).unwrap();
assert_eq!(settings.render_options().get::<PageOptions>().unwrap().width, 72);
let wrong = serde_json::json!({ "height": 72 });
let errors = decl.read(wrong.as_object().unwrap()).unwrap_err();
assert_eq!(errors[0].path, "page.height");Implementations§
Source§impl SettingsDecl
impl SettingsDecl
Sourcepub fn new(feature: impl Into<FeatureName>) -> SettingsDecl
pub fn new(feature: impl Into<FeatureName>) -> SettingsDecl
Creates the declaration of the settings of the feature named feature,
with no key yet.
Sourcepub fn config<C: SettingsData + Options>(
self,
docs: impl Into<String>,
) -> SettingsDecl
pub fn config<C: SettingsData + Options>( self, docs: impl Into<String>, ) -> SettingsDecl
Adds the feature’s configuration, the type C its constructor takes,
with the description docs, and returns the declaration. Every field of
C is a settings key, bound when the registry is built
(Bind::Registry).
C is read with its Deserialize implementation. An empty map must read
as C’s default, which #[serde(default)] on the type provides, because
read reads the configuration even when none of
its keys is written.
§Panics
When a field of C has the name of a key already declared or is named
ENABLED_KEY (enabled), when the declaration already has a
configuration, or when C is not read from a map of named fields.
Sourcepub fn config_from<D, C>(self, docs: impl Into<String>) -> SettingsDecl
pub fn config_from<D, C>(self, docs: impl Into<String>) -> SettingsDecl
Adds the feature’s configuration, the type C its constructor takes,
read from data as the type D and converted with C::try_from, and
returns the declaration. Use it for a configuration that holds values
data cannot state, or whose form as data differs from its Rust type.
Every field of D is a settings key; a conversion error is reported as
a SettingsError for the feature as a whole. Otherwise as
config.
§Panics
When a field of D has the name of a key already declared or is named
ENABLED_KEY (enabled), when the declaration already has a
configuration, or when D is not read from a map of named fields.
Sourcepub fn config_data(
self,
schema: Value,
docs: impl Into<String>,
) -> Result<SettingsDecl, SettingsError>
pub fn config_data( self, schema: Value, docs: impl Into<String>, ) -> Result<SettingsDecl, SettingsError>
Adds the feature’s configuration declared from data, and returns the
declaration: schema is a JSON Schema of an object, whose properties
are the configuration’s settings keys, bound when the registry is built
(Bind::Registry); docs describes it.
The configuration is the option type named by the feature’s name
(OptionKey::Data). read keeps the values
written as a DataValue, the empty map when none of the keys is
written, and checks nothing of them but their keys (see the module
documentation); a package reads it with
PackageSettings::config::<DataValue>. A feature that reads its
configuration when the settings are read declares it with
config_data_read.
The schema’s own $defs join those of the declaration’s schema
(json_schema): a $defs name shared with
another part of the declaration refers to one definition.
§Errors
A SettingsError of the kind SettingsErrorKind::InvalidDeclaration
when schema has no properties map, when one of its properties is a
key declared already or is named ENABLED_KEY (enabled), when
the declaration has a configuration already, or when the marks of its
nodes are not a list of mark names, lie outside every setting (on the
schema’s root, or under a keyword that no path follows), mark an
object as combined key by key with a default that is not a map, or
are read while a definition of $defs that refers to itself is read
(a mark is kept with its path, and a path names one depth of the
definition).
Sourcepub fn config_data_read(
self,
schema: Value,
docs: impl Into<String>,
read: Arc<dyn Fn(Value) -> Result<Value, ReadError> + Send + Sync>,
) -> Result<SettingsDecl, SettingsError>
pub fn config_data_read( self, schema: Value, docs: impl Into<String>, read: Arc<dyn Fn(Value) -> Result<Value, ReadError> + Send + Sync>, ) -> Result<SettingsDecl, SettingsError>
Adds the feature’s configuration declared from data, read by read,
and returns the declaration: as config_data,
except that read gives the configuration
written (the empty map when none of its keys is written) to read, and
keeps the value read answers as the DataValue that
PackageSettings::config::<DataValue> gives.
read is the twin of config_from’s
reading as D and conversion with C::try_from: it answers the value
the feature receives, or the value at fault as a ReadError, which
is reported as config_from’s conversion errors are, a
SettingsError of the kind SettingsErrorKind::InvalidValue whose
path is the feature’s name followed by the error’s path. A reader that
only checks the configuration answers it unchanged.
§Errors
As config_data.
Sourcepub fn options_data(
self,
name: impl Into<Name>,
bind: Bind,
schema: Value,
docs: impl Into<String>,
) -> Result<SettingsDecl, SettingsError>
pub fn options_data( self, name: impl Into<Name>, bind: Bind, schema: Value, docs: impl Into<String>, ) -> Result<SettingsDecl, SettingsError>
Adds the option type named name, declared from data, with the binding
bind, and returns the declaration: schema is a JSON Schema of an
object, whose properties are the option type’s settings keys; docs
describes it.
The option type is identified by name (OptionKey::Data); a stage
reads it by that name
(StageCx::option_named).
read keeps the values written as a DataValue
and checks nothing of them but their keys; when none of its keys is
written, the option type gets no value, and a read resolves to the empty
map.
The schema’s own $defs join those of the declaration’s schema
(json_schema): a $defs name shared with
another part of the declaration refers to one definition.
§Errors
As config_data, and when bind is
Bind::Registry (a value fixed with the registry is the feature’s
configuration).
Sourcepub fn options<O: SettingsData + Options>(
self,
bind: Bind,
docs: impl Into<String>,
) -> SettingsDecl
pub fn options<O: SettingsData + Options>( self, bind: Bind, docs: impl Into<String>, ) -> SettingsDecl
Adds the option type O of the feature, with the binding bind and the
description docs, and returns the declaration. Every field of O is a
settings key.
§Panics
When bind is Bind::Registry (a value fixed with the registry is
the feature’s configuration: see config), when
a field of O has the name of a key already declared or is named
ENABLED_KEY (enabled), or when O is not read from a map of named
fields.
Sourcepub fn merge_entries(
self,
path: impl Into<String>,
default_entries: DefaultEntries,
) -> SettingsDecl
pub fn merge_entries( self, path: impl Into<String>, default_entries: DefaultEntries, ) -> SettingsDecl
Marks the object at path as combined key by key
(Mark::MergeObjectKeyValues), and returns the declaration: a host that
combines two sources of settings combines that object one key at a time.
See KeyMark for the form of the path, which reaches a value at any
depth.
default_entries returns, as data, the entries the object holds when no
source writes it: the feature’s own default. A host treats them as the
lowest-ranked source, so an entry a source writes replaces the default
entry of that name, and the default entries no source writes stay. The
function usually writes the default of the key’s Rust field as data
(named::to_data for a list of named entries). Read them back with
default_entries; the JSON Schema
gives them as the default of the object’s node.
A collection of named things is also marked with
remove_false.
§Panics
When path reaches no node of the declaration’s schema, and when
default_entries fails or returns something other than a map: both are
mistakes in the feature’s code, not in any data.
Sourcepub fn default_entries(&self, path: &str) -> Option<&Map<String, Value>>
pub fn default_entries(&self, path: &str) -> Option<&Map<String, Value>>
The default entries of the object at path, marked as combined key by
key (Mark::MergeObjectKeyValues): the entries it holds when no source
of settings writes it, as a map from each entry’s name to the rest of the
entry, in the feature’s order. Returns None when the object is not
marked so.
Sourcepub fn remove_false(self, path: impl Into<String>) -> SettingsDecl
pub fn remove_false(self, path: impl Into<String>) -> SettingsDecl
Marks the object at path as one in which a key whose value is false
is removed (Mark::RemoveKeyWhenValueIsFalse) and returns the
declaration. See KeyMark for the form of the path.
§Panics
When path reaches no node of the declaration’s schema.
Sourcepub fn default_on_null(self, path: impl Into<String>) -> SettingsDecl
pub fn default_on_null(self, path: impl Into<String>) -> SettingsDecl
Marks the setting at path as one that a value of null gives its
default (Mark::SetToDefaultWithNull) and returns the declaration. See
KeyMark for the form of the path.
§Panics
When path reaches no node of the declaration’s schema.
Sourcepub fn markup(self, path: impl Into<String>) -> SettingsDecl
pub fn markup(self, path: impl Into<String>) -> SettingsDecl
Marks the value at path as text in FLM markup (Mark::Markup) and
returns the declaration. See KeyMark for the form of the path.
§Panics
When path reaches no node of the declaration’s schema.
Sourcepub fn path(self, path: impl Into<String>) -> SettingsDecl
pub fn path(self, path: impl Into<String>) -> SettingsDecl
Marks the value at path as a path to a file (Mark::Path) and
returns the declaration. See KeyMark for the form of the path.
§Panics
When path reaches no node of the declaration’s schema.
Sourcepub fn open_entries(self, path: impl Into<String>) -> SettingsDecl
pub fn open_entries(self, path: impl Into<String>) -> SettingsDecl
Marks the places path (see KeyMark for its form) as those where
other features declare entries (entry_data,
EntryDecl), and returns the declaration: template_config.*.*
lets another feature declare the schema of template_config.html.base.
§Panics
When path reaches no node of the declaration’s schema.
Sourcepub fn opens(&self, path: &str) -> bool
pub fn opens(&self, path: &str) -> bool
Whether another feature may declare the entry at path (its segments
joined by ., with no *): one of the places
open_entries gives names it.
Sourcepub fn entry_data(
self,
owner: impl Into<FeatureName>,
path: impl Into<String>,
label: impl Into<String>,
schema: Value,
) -> Result<SettingsDecl, SettingsError>
pub fn entry_data( self, owner: impl Into<FeatureName>, path: impl Into<String>, label: impl Into<String>, schema: Value, ) -> Result<SettingsDecl, SettingsError>
Declares the schema of the entry at path of a key of the feature
owner, and returns the declaration (see EntryDecl). path is the
entry’s path below the owner’s name, its segments joined by ., each
segment the name of an entry (template_config.html.base); label is
what the entry is, in the words of the person who writes its schema and
without an article (compose template ‘base’ for the output ‘html’),
which the messages about the entry name it by; schema is
a JSON Schema of an object, whose properties are the keys of the
entry, with the marks written on its nodes, read as those of a part
declared from data are (see the module
documentation). An entry is not a
key of the declaring feature: a declaration with entries and no key of
its own describes a feature that takes no settings
(takes_settings).
§Errors
A SettingsError of the kind SettingsErrorKind::InvalidDeclaration
when path holds * or an empty segment, when the declaration already
declares that entry, when schema has no properties map, or when the
marks of its nodes are faulty as config_data
states.
Sourcepub fn entries(&self) -> &[EntryDecl]
pub fn entries(&self) -> &[EntryDecl]
The entries of other features’ keys that this feature declares
(entry_data), in the order declared.
Sourcepub fn with_entries<'e>(
&self,
entries: impl IntoIterator<Item = &'e EntryDecl>,
) -> SettingsDecl
pub fn with_entries<'e>( &self, entries: impl IntoIterator<Item = &'e EntryDecl>, ) -> SettingsDecl
Returns this declaration combined with the declarations of its entries
entries, those in force (see EntryDecl): each entry of entries
whose owner is this feature and whose path this declaration opens
(opens); the others are ignored.
The result carries the marks of each entry’s schema at their paths
below the entry, with their default entries; the marks that this
declaration sets on the places of the entry and of the maps above it,
by a path with *, also at the entry’s own path, so that its JSON
Schema writes them on the entry’s node; and the entry’s schema at its
place in its JSON Schema (json_schema),
its $defs joining those of the result under names of the entry’s
own (its path, a . and the definition’s name). entry_in_force
gives each entry back. The cascade makes it from the entries declared by
the features in use.
Sourcepub fn entry_in_force(&self, path: &str) -> Option<&EntryDecl>
pub fn entry_in_force(&self, path: &str) -> Option<&EntryDecl>
The entry in force at path (its segments joined by .), declared by
another feature and combined with this declaration by
with_entries, if there is one.
Source§impl SettingsDecl
impl SettingsDecl
Sourcepub fn feature(&self) -> &FeatureName
pub fn feature(&self) -> &FeatureName
The name of the feature whose settings this declaration describes.
Sourcepub fn keys(&self) -> impl Iterator<Item = KeyDecl> + '_
pub fn keys(&self) -> impl Iterator<Item = KeyDecl> + '_
Every settings key the feature declares, with its binding: the fields of its configuration first, then those of each option type, in the order declared.
Sourcepub fn takes_settings(&self) -> bool
pub fn takes_settings(&self) -> bool
Whether the feature takes settings: whether the declaration has a key
of its own (keys). A declaration that only
declares entries of another feature’s keys
(entry_data) describes a feature that
takes no settings.
Sourcepub fn option_types(&self) -> impl Iterator<Item = (OptionKey, Bind)> + '_
pub fn option_types(&self) -> impl Iterator<Item = (OptionKey, Bind)> + '_
The Rust types the settings are read into, each identified by its option
key and given with its binding, in the order they were added; the
configuration’s binding is Bind::Registry. The registry build
declares each of them as an option type of the feature.
Sourcepub fn key(&self, name: &str) -> Option<KeyDecl>
pub fn key(&self, name: &str) -> Option<KeyDecl>
The settings key name, or None when the feature does not declare it.
Sourcepub fn marks(&self) -> &[KeyMark]
pub fn marks(&self) -> &[KeyMark]
The marks on the feature’s settings and the values below them, one
KeyMark per marked path, in the order each path was first marked.
Sourcepub fn is_marked(&self, place: &[String], mark: Mark) -> bool
pub fn is_marked(&self, place: &[String], mark: Mark) -> bool
Whether a mark of the declaration sets mark on the place place, the
key first, then the names below it: a mark’s path names the place when
each of its segments is the place’s name there, or * (see KeyMark).
Sourcepub fn mark_at(&self, path: &str) -> Option<&KeyMark>
pub fn mark_at(&self, path: &str) -> Option<&KeyMark>
The marks on the value at path (see KeyMark), or None when no
mark is set there.
Sourcepub fn read(
&self,
data: &Map<String, Value>,
) -> Result<FeatureSettings, Vec<SettingsError>>
pub fn read( &self, data: &Map<String, Value>, ) -> Result<FeatureSettings, Vec<SettingsError>>
Reads the feature’s settings from data, a map from a settings key to its
value.
Returns the configuration (its default when none of its keys is
written) and the option values of each option type at least one of whose
keys is written; an option type none of whose keys is written gets no
value. Values of an option type bound to the render request go to
FeatureSettings::render_options; values of the other option types go
to FeatureSettings::document_options.
§Errors
Returns every problem found, each as a SettingsError that identifies
the key by its path from the feature’s name: every key in data that the
feature does not declare, and every value that cannot be read as its key
requires.
Sourcepub fn json_schema(&self) -> Value
pub fn json_schema(&self) -> Value
Returns the JSON Schema of the feature’s settings.
The schema is an object whose description joins the descriptions given
with each Rust type of the declaration. Its properties are the feature’s
settings keys, each with the description from its field’s Rust
documentation and its binding under x-flm-bind; no other property is
allowed. The schemas of the types the keys are read into are under
$defs.
Every mark (marks) is written on the node at
its path, under MARKS_KEYWORD (x-flm-marks), as the list of the
mark names; the default of an object combined key by key is its default
entries (default_entries). A mark is
written at no other place: where the path of a mark passes through a
definition of $defs that another place refers to as well, and the
marks below the two places differ, the definition is written out in
place along that path; otherwise the marks are written in the
definition.
For a part declared from data, each key’s schema is the one its
properties give, with its binding added, and the part’s own $defs
join those of the result; its description is the docs given.
The schema of each entry in force (with_entries)
stands at its place: each map on the way to it gets the property of the
entry’s name beside its additionalProperties, and the entry’s
property is the entry’s schema, with its marks and those that this
declaration sets on the entry’s place; the entry’s own $defs join
those of the result, each named by the entry’s path, a . and its own
name.
Every description is the Rust documentation turned into plain text, for
any reader of the schema: a link is written as its words alone, without
its target; a code block is left out, and so is everything from the
first heading on (# Examples, # Panics). The rest is kept as written,
including code spans in backticks.
Trait Implementations§
Source§impl Clone for SettingsDecl
impl Clone for SettingsDecl
Auto Trait Implementations§
impl !Freeze for SettingsDecl
impl !RefUnwindSafe for SettingsDecl
impl !UnwindSafe for SettingsDecl
impl Forget for SettingsDecl
impl Move for SettingsDecl
impl Send for SettingsDecl
impl Sync for SettingsDecl
impl Unpin for SettingsDecl
impl UnsafeUnpin for SettingsDecl
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