[][src]Struct spirit_cfg_helpers::CfgDump

pub struct CfgDump { /* fields omitted */ }

A command line fragment to add --dump-config to allow showing loaded configuration.

When this is added into the command line options structure, the --dump-config and --dump-config-as options are added.

These dump the current configuration and exit.

In case the configuration is collected over multiple configuration files, directories and possibly environment variables and command line overrides, it is not always clear what exact configuration is actually used. This allows the user to query the actual configuration the application would use.

The fragment can be either used manually with the dump method or automatically by registering its extension.

Requirements

For this to work, the configuration structure must implement Serialize. This is not mandated by Spirit itself. However, all the fragments provided by spirit crates implement it. For custom structures it is often sufficient to stick #[derive(Serialize)] onto them.

Examples

use serde_derive::{Deserialize, Serialize};
use spirit::Spirit;
use spirit::prelude::*;
use spirit_cfg_helpers::CfgDump;
use structopt::StructOpt;

#[derive(Default, Deserialize, Serialize)]
struct Cfg {
    option: Option<String>,
}

#[derive(Debug, StructOpt)]
struct Opts {
    #[structopt(flatten)]
    dump: CfgDump,
}

impl Opts {
    fn dump(&self) -> &CfgDump {
        &self.dump
    }
}

fn main() {
    Spirit::<Opts, Cfg>::new()
        .with(CfgDump::extension(Opts::dump))
        .run(|_| Ok(()));
}

Implementations

impl CfgDump[src]

pub fn dump<C: Serialize>(&self, cfg: &C)[src]

Dump configuration if it is asked for in the options.

If the parsed options specify to dump the configuration, this does so and exits. If the options don't specify that, it does nothing.

This can be used manually. However, the common way is to register the extension within an Extensible (either spirit::Spirit or spirit::Builder) and let it do everything automatically.

pub fn extension<E, F>(extract: F) -> impl Extension<E> where
    E: Extensible<Ok = E>,
    F: FnOnce(&E::Opts) -> &Self + Send + 'static,
    E::Config: Serialize
[src]

An extension that can be registered with Extensible::with.

The parameter is an extractor, a function that takes the whole command line options structure and returns a reference to just the CfgDump instance in there.

Note that for configuration to be dumped, it needs to be parsed first. Therefore it'll fail to dump it if the configuration is invalid.

Also, as this exits if the dumping is requested, it makes some sense to register it sooner than later. It registers itself as a config_validator and it is not needed to validate parts of the configuration only to throw it out on the exit.

Trait Implementations

impl Clone for CfgDump[src]

impl Debug for CfgDump[src]

impl Default for CfgDump[src]

impl StructOpt for CfgDump[src]

impl StructOptInternal for CfgDump[src]

Auto Trait Implementations

impl RefUnwindSafe for CfgDump

impl Send for CfgDump

impl Sync for CfgDump

impl Unpin for CfgDump

impl UnwindSafe for CfgDump

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> IntoResult<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.