pub struct PackageFactory { /* private fields */ }Expand description
A package known before it is built: its name, the names of the packages it
uses, its features each with the declaration of its settings, and the
function that builds the package’s PackageEntry from the final
settings.
A factory serves a program that chooses packages by name and reads their
settings as data, for example from a document’s front matter. The program
reads the settings written for the package’s features with the
declarations the factory lists (settings),
combines them over all its sources, and then builds the package once with
build. The factory lists the packages it uses by
name only: the program builds each of them from its own factory, so the
entry that build returns should use no package. A package that holds the
standard features builds them in its build function with
crate::stdlib::add_features_from_settings.
The factory finds its features by building the package once, when it is created, from empty settings: it lists each feature of that entry, in order, with the name and the settings declaration the feature returns. A feature that the package includes only under some settings is therefore not listed.
§Examples
use std::sync::Arc;
use flm_core::core::registry::{
BuildError, FeatureBuilder, FeatureInfo, FlmFeature, PackageEntry, PackageFactory,
};
use flm_core::core::settings::PackageSettings;
struct Empty;
impl FlmFeature for Empty {
fn info(&self) -> FeatureInfo { FeatureInfo::new("empty", "Empty") }
fn register(&self, _b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> { Ok(()) }
}
let factory = PackageFactory::new("mine", |settings: &PackageSettings| {
let mut entry = PackageEntry::new("mine");
if !settings.is_off("empty") {
entry = entry.feature(Arc::new(Empty));
}
Ok(entry)
})?;
assert_eq!(factory.features().len(), 1);
assert_eq!(factory.features()[0].name, "empty");
let entry = factory.build(&PackageSettings::new())?;
assert_eq!(entry.features.len(), 1);Implementations§
Source§impl PackageFactory
impl PackageFactory
Sourcepub fn new(
name: impl Into<Name>,
build: impl Fn(&PackageSettings) -> Result<PackageEntry, PackageError> + Send + Sync + 'static,
) -> Result<PackageFactory, PackageError>
pub fn new( name: impl Into<Name>, build: impl Fn(&PackageSettings) -> Result<PackageEntry, PackageError> + Send + Sync + 'static, ) -> Result<PackageFactory, PackageError>
The factory of the package name, whose entry the function build
builds; the package uses no other package until
uses adds one.
build receives the final settings of the package’s features and
returns the package’s entry; it should leave out every feature the
settings switch off (PackageSettings::is_off). This constructor
calls build once, with empty settings, and lists the features of the
entry it returns (see features).
§Errors
The PackageError that build returns for empty settings.
Sourcepub fn uses(self, package: impl Into<Name>) -> PackageFactory
pub fn uses(self, package: impl Into<Name>) -> PackageFactory
Adds the name of a package this package uses, after those already
added. Returns self, for chaining.
Sourcepub fn features(&self) -> &[PackageFeature]
pub fn features(&self) -> &[PackageFeature]
The package’s features, in the package’s order: the features of the entry built from empty settings, each with its name and its settings declaration.
Sourcepub fn settings(&self, feature: &str) -> Option<&SettingsDecl>
pub fn settings(&self, feature: &str) -> Option<&SettingsDecl>
The declaration of the settings of the feature feature, or None
when the package does not hold that feature or the feature takes no
settings as data.
Sourcepub fn build(
&self,
settings: &PackageSettings,
) -> Result<PackageEntry, PackageError>
pub fn build( &self, settings: &PackageSettings, ) -> Result<PackageEntry, PackageError>
Builds the package’s entry from the final settings of its features, by
calling the function given to new.
§Errors
The PackageError that the package’s build function returns.
Trait Implementations§
Source§impl Clone for PackageFactory
impl Clone for PackageFactory
Auto Trait Implementations§
impl !RefUnwindSafe for PackageFactory
impl !UnwindSafe for PackageFactory
impl Forget for PackageFactory
impl Freeze for PackageFactorywhere
*const ArcInner<str> is !null: Freeze,
PhantomData<ArcInner<str>>: Freeze,
*const u8 is !null: Freeze,
PhantomData<Name>: Freeze,
PhantomData<PackageFeature>: Freeze,
*const ArcInner<dyn Fn(&PackageSettings) -> Result<PackageEntry, PackageError> + Send + Sync> is !null: Freeze,
PhantomData<ArcInner<dyn Fn(&PackageSettings) -> Result<PackageEntry, PackageError> + Send + Sync>>: Freeze,
impl Move for PackageFactory
impl Send for PackageFactorywhere
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,
Arc<dyn Fn(&PackageSettings) -> Result<PackageEntry, PackageError> + Send + Sync>: Send,
impl Sync for PackageFactorywhere
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,
Arc<dyn Fn(&PackageSettings) -> Result<PackageEntry, PackageError> + Send + Sync>: Sync,
impl Unpin for PackageFactorywhere
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,
Arc<dyn Fn(&PackageSettings) -> Result<PackageEntry, PackageError> + Send + Sync>: Unpin,
impl UnsafeUnpin for PackageFactorywhere
*const ArcInner<str> is !null: UnsafeUnpin,
PhantomData<ArcInner<str>>: UnsafeUnpin,
*const u8 is !null: UnsafeUnpin,
PhantomData<Name>: UnsafeUnpin,
PhantomData<PackageFeature>: UnsafeUnpin,
*const ArcInner<dyn Fn(&PackageSettings) -> Result<PackageEntry, PackageError> + Send + Sync> is !null: UnsafeUnpin,
PhantomData<ArcInner<dyn Fn(&PackageSettings) -> Result<PackageEntry, PackageError> + Send + Sync>>: UnsafeUnpin,
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