[][src]Struct figment::Jail

pub struct Jail { /* fields omitted */ }
This is supported on crate feature test only.

A "sandboxed" environment with isolated env and file system namespace.

Jail creates a pseudo-sandboxed (not actually sandboxed) environment for testing configurations. Specifically, Jail:

Additionally, because Jail expects functions that return a Result, the ? operator can be used liberally in a jail:

use figment::{Figment, Jail, providers::{Format, Toml, Env}};

figment::Jail::expect_with(|jail| {
    jail.create_file("Cargo.toml", r#"
      name = "test"
      authors = ["bob"]
      publish = false
    "#)?;

    jail.set_env("CARGO_NAME", "env-test");

    let config: Config = Figment::new()
        .merge(Toml::file("Cargo.toml"))
        .merge(Env::prefixed("CARGO_"))
        .extract()?;

    Ok(())
});

Implementations

impl Jail[src]

pub fn expect_with<F: FnOnce(&mut Jail) -> Result<()>>(f: F)[src]

Creates a new jail that calls f, passing itself to f.

Panics

Panics if f returns an Err; prints the error message.

Example

figment::Jail::expect_with(|jail| {
    /* in the jail */

    Ok(())
});

pub fn try_with<F: FnOnce(&mut Jail) -> Result<()>>(f: F) -> Result<()>[src]

Creates a new jail that calls f, passing itself to f. Does not panic; returns the result from f.

Example

let result = figment::Jail::try_with(|jail| {
    /* in the jail */

    Ok(())
});

pub fn directory(&self) -> &Path[src]

Returns the directory the jail has switched into. The contents of this directory will be cleared when Jail is dropped.

Example

figment::Jail::expect_with(|jail| {
    let tmp_directory = jail.directory();

    Ok(())
});

pub fn create_file<P: AsRef<Path>>(
    &self,
    path: P,
    contents: &str
) -> Result<File>
[src]

Creates a file with contents contents in the jail's directory. The file will be deleted with the jail is dropped.

Example

figment::Jail::expect_with(|jail| {
    jail.create_file("MyConfig.json", "contents...");
    Ok(())
});

pub fn set_env<K: AsRef<str>, V: Display>(&mut self, k: K, v: V)[src]

Set the environment variable k to value v. The variable will be removed when the jail is dropped.

Example

const VAR_NAME: &str = "my-very-special-figment-var";

assert!(std::env::var(VAR_NAME).is_err());

figment::Jail::expect_with(|jail| {
    jail.set_env(VAR_NAME, "value");
    assert!(std::env::var(VAR_NAME).is_ok());
    Ok(())
});

assert!(std::env::var(VAR_NAME).is_err());

Trait Implementations

impl Drop for Jail[src]

Auto Trait Implementations

impl RefUnwindSafe for Jail

impl Send for Jail

impl Sync for Jail

impl Unpin for Jail

impl UnwindSafe for Jail

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