Trait spirit::helpers::Helper

source ·
pub trait Helper<O, C> {
    fn apply(self, builder: Builder<O, C>) -> Builder<O, C>;
}
Expand description

The basic helper trait.

It allows being plugged into a builder and modifying it in an arbitrary way.

It is more common to apply the helper by the Builder::with method than directly.

There’s an implementation of Helper for FnOnce(Builder) -> Builder, so helpers can be either custom types or just closures (which are often more convenient than defining an empty type and the implementation).

use spirit::{Builder, Empty, Spirit};
use spirit::helpers::Helper;

struct CfgPrint;

impl Helper<Empty, Empty> for CfgPrint {
    fn apply(self, builder: Builder<Empty, Empty>) -> Builder<Empty, Empty> {
        builder.on_config(|_opts, _config| println!("Config changed"))
    }
}

Spirit::<Empty, Empty>::new()
    .with(CfgPrint)
    .run(|_spirit| {
        println!("Running...");
        Ok(())
    })
use spirit::{Builder, Empty, Spirit};

fn cfg_print(builder: Builder<Empty, Empty>) -> Builder<Empty, Empty> {
    builder.on_config(|_opts, _config| println!("Config changed"))
}

Spirit::<Empty, Empty>::new()
    .with(cfg_print)
    .run(|_spirit| {
        println!("Running...");
        Ok(())
    })

Required Methods§

Perform the transformation on the given builder.

And yes, it is possible to do multiple primitive transformations inside one helper (this is what makes helpers useful for 3rd party crates, they can integrate with just one call of with).

Implementors§