Struct tomlenv::Environments[][src]

pub struct Environments<S, T> where
    S: Ord
{ /* fields omitted */ }

Hold environment specific data as a map from your environment hierarchy key to data struct containg the config for that particular environment.

Example

// Your environment specific data struct
// *NOTE*: This must implement `Deserialize` and `Serialize`
#[derive(Debug, Deserialize, Getters, Serialize)]
struct RuntimeEnv {
  #[get]
  name: String,
  #[get]
  key: Option<String>,
}

// Your environment specific configuration
let toml = r#"[envs.prod]
name = "Production"
key = "abcd-123-efg-45"

[envs.stage]
name = "Stage"

[envs.test]
name = "Test"

[envs.dev]
name = "Development"

[envs.local]
name = "Local"
"#;

// Deserialize the TOML config into your environment structs
let mut cursor = Cursor::new(toml);
let envs: Environments<Environment, RuntimeEnv> = Environments::from_reader(&mut cursor)?;

// Test that all the environments are present
env::set_var("env", "prod");
let mut current = envs.current()?;
assert_eq!(current.name(), "Production");
assert_eq!(current.key(), &Some("abcd-123-efg-45".to_string()));

env::set_var("env", "stage");
current = envs.current()?;
assert_eq!(current.name(), "Stage");
assert_eq!(current.key(), &None);

env::set_var("env", "test");
current = envs.current()?;
assert_eq!(current.name(), "Test");
assert_eq!(current.key(), &None);

env::set_var("env", "dev");
current = envs.current()?;
assert_eq!(current.name(), "Development");
assert_eq!(current.key(), &None);

env::set_var("env", "local");
current = envs.current()?;
assert_eq!(current.name(), "Local");
assert_eq!(current.key(), &None);

Methods

impl<S, T> Environments<S, T> where
    T: DeserializeOwned + Serialize,
    S: DeserializeOwned + Serialize + Ord + PartialOrd + TryFrom<String>,
    Error: From<<S as TryFrom<String>>::Error>, 
[src]

Load the environments from a path.

Load the environments from a reader.

Get the current environment

Trait Implementations

impl<S: Debug, T: Debug> Debug for Environments<S, T> where
    S: Ord
[src]

Formats the value using the given formatter. Read more

impl<'a, S, T> TryFrom<&'a ArgMatches<'a>> for Environments<S, T> where
    T: DeserializeOwned + Serialize,
    S: DeserializeOwned + Serialize + Ord + PartialOrd + TryFrom<String>,
    Error: From<<S as TryFrom<String>>::Error>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

Auto Trait Implementations

impl<S, T> Send for Environments<S, T> where
    S: Send,
    T: Send

impl<S, T> Sync for Environments<S, T> where
    S: Sync,
    T: Sync