Trait spirit::extension::Extension [−][src]
Expand description
The basic extension trait.
It allows being plugged into a Builder
or Spirit
and
modifying it in an arbitrary way.
It is more common to apply the helper by the
with
or with_singleton
method than
directly.
There’s an implementation of Extension
for FnOnce(Extensible) -> Result<Extensible, AnyError>
,
so extensions can be either custom types or just closures (which are often more convenient than
defining an empty type and the implementation).
It is better to define the extension in a generic way (eg. accepting any type that is
Extensible
) instead of eg. a Builder
directly. Note that sometimes it
is needed to restrict the acceptable Extensible
s to the base ones, with Ok = E
, like in
the example below. They’ll still be possible to apply to Result
s.
Examples
use spirit::{AnyError ,Empty, Spirit};
use spirit::extension::{Extension, Extensible};
use spirit::prelude::*;
struct CfgPrint;
impl<E: Extensible<Ok = E>> Extension<E> for CfgPrint {
fn apply(self, ext: E) -> Result<E, AnyError> {
Ok(ext.on_config(|_opts, _config| println!("Config changed")))
}
}
Spirit::<Empty, Empty>::new()
.with(CfgPrint)
.run(|_spirit| {
println!("Running...");
Ok(())
})
use spirit::{Empty, Extensible, Spirit};
use spirit::prelude::*;
fn cfg_print<E: Extensible<Ok = E>>(ext: E) -> E {
ext.on_config(|_opts, _config| println!("Config changed"))
}
Spirit::<Empty, Empty>::new()
.with(cfg_print)
.run(|_spirit| {
println!("Running...");
Ok(())
})