Skip to main content

SettingsDecl

Struct SettingsDecl 

Source
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

Source

pub fn new(feature: impl Into<FeatureName>) -> SettingsDecl

Creates the declaration of the settings of the feature named feature, with no key yet.

Source

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.

Source

pub fn config_from<D, C>(self, docs: impl Into<String>) -> SettingsDecl
where D: SettingsData, C: Options + TryFrom<D>, <C as TryFrom<D>>::Error: Display,

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.

Source

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).

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn entries(&self) -> &[EntryDecl]

The entries of other features’ keys that this feature declares (entry_data), in the order declared.

Source

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.

Source

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

Source

pub fn feature(&self) -> &FeatureName

The name of the feature whose settings this declaration describes.

Source

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.

Source

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.

Source

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.

Source

pub fn key(&self, name: &str) -> Option<KeyDecl>

The settings key name, or None when the feature does not declare it.

Source

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.

Source

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).

Source

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.

Source

pub fn has_mark(&self, path: &str, mark: Mark) -> bool

Whether the value at path carries mark.

Source

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.

Source

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

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 SettingsDecl

Source§

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

Formats the value using the given formatter. Read more

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
where Arc<str>: Send, RawVecInner: Send, &'static str: Send, TypeId: Send, Arc<dyn Fn(Value) -> Result<ReadValue, ReadError> + Send + Sync>: Send, Arc<dyn Fn(&mut FeatureBuilder<'_>, Bind, String) -> Result<(), BuildError> + Send + Sync>: Send, Arc<dyn Fn(&mut SchemaGenerator) -> Schema + Send + Sync>: Send, RawTable<usize>: Send, OnceLock<Arc<Value>>: Send,

§

impl Sync for SettingsDecl
where Arc<str>: Sync, RawVecInner: Sync, TypeId: Sync, Arc<dyn Fn(Value) -> Result<ReadValue, ReadError> + Send + Sync>: Sync, Arc<dyn Fn(&mut FeatureBuilder<'_>, Bind, String) -> Result<(), BuildError> + Send + Sync>: Sync, Arc<dyn Fn(&mut SchemaGenerator) -> Schema + Send + Sync>: Sync, RawTable<usize>: Sync, OnceLock<Arc<Value>>: Sync,

§

impl Unpin for SettingsDecl
where Arc<str>: Unpin, *const u8 is !null: Unpin, &'static str: Unpin, *const (): Unpin, Arc<dyn Fn(Value) -> Result<ReadValue, ReadError> + Send + Sync>: Unpin, Arc<dyn Fn(&mut FeatureBuilder<'_>, Bind, String) -> Result<(), BuildError> + Send + Sync>: Unpin, Arc<dyn Fn(&mut SchemaGenerator) -> Schema + Send + Sync>: Unpin, Align4<u32>: Unpin, Arc<Value>: Unpin,

§

impl UnsafeUnpin for SettingsDecl

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.