[][src]Struct figment::providers::Serialized

pub struct Serialized<T> {
    pub value: T,
    pub key: Option<String>,
    pub profile: Profile,
    // some fields omitted
}

A Provider that sources values directly from a serialize type.

Provider Details

  • Profile

    This provider does not set a profile.

  • Metadata

    This provider is named T (via std::any::type_name). The source location is set to the call site of the constructor.

  • Data (Unkeyed)

    When data is not keyed, T is expected to serialize to a Dict and is emitted directly as the value for the configured profile.

  • Data (Keyed)

    When keyed, the T can serialize as any Value and is emitted as the value of the configured key. Specifically, nested dictionaries are created for every path component delimited by . in the key string (3 in a.b.c), each dictionary mapping to its parent, and the value mapping to the leaf.

Fields

value: T

The value to be serialized and used as the provided data.

key: Option<String>

The key path (a.b.c) to emit the value to or the root if None.

profile: Profile

The profile to emit the value to. Defaults to Profile::Default.

Implementations

impl<T> Serialized<T>[src]

pub fn from<P: Into<Profile>>(value: T, profile: P) -> Serialized<T>[src]

Constructs an (unkeyed) provider that emits value to the profile.

use serde::Deserialize;
use figment::{Figment, Jail, providers::Serialized, util::map};

#[derive(Debug, PartialEq, Deserialize)]
struct Config {
    numbers: Vec<usize>,
}

Jail::expect_with(|jail| {
    let map = map!["numbers" => &[1, 2, 3]];

    // This is also `Serialized::defaults(&map)`;
    let figment = Figment::from(Serialized::from(&map, "default"));
    let config: Config = figment.extract()?;
    assert_eq!(config, Config { numbers: vec![1, 2, 3] });

    // This is also `Serialized::defaults(&map).profile("debug")`;
    let figment = Figment::from(Serialized::from(&map, "debug"));
    let config: Config = figment.select("debug").extract()?;
    assert_eq!(config, Config { numbers: vec![1, 2, 3] });

    Ok(())
});

pub fn defaults(value: T) -> Serialized<T>[src]

Emits value, which must serialize to a Dict, to the Default profile.

Equivalent to Serialized::from(value, Profile::Default).

See Serialized::from().

pub fn globals(value: T) -> Serialized<T>[src]

Emits value, which must serialize to a Dict, to the Global profile.

Equivalent to Serialized::from(value, Profile::Global).

See Serialized::from().

pub fn default(key: &str, value: T) -> Serialized<T>[src]

Emits a nested dictionary to the Default profile keyed by key with the final key mapping to value.

Equivalent for Serialized::from(value, Profile::Default).key(key).

See Serialized::from() and Serialized::key().

pub fn global(key: &str, value: T) -> Serialized<T>[src]

Emits a nested dictionary to the Global profile keyed by key with the final key mapping to value.

Equivalent to Serialized::from(value, Profile::Global).key(key).

See Serialized::from() and Serialized::key().

pub fn profile<P: Into<Profile>>(self, profile: P) -> Self[src]

pub fn key(self, key: &str) -> Self[src]

Trait Implementations

impl<T: Clone> Clone for Serialized<T>[src]

impl<T: Debug> Debug for Serialized<T>[src]

impl<T: Serialize> Provider for Serialized<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Serialized<T> where
    T: RefUnwindSafe

impl<T> Send for Serialized<T> where
    T: Send

impl<T> Sync for Serialized<T> where
    T: Sync

impl<T> Unpin for Serialized<T> where
    T: Unpin

impl<T> UnwindSafe for Serialized<T> where
    T: UnwindSafe

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

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,