Struct tomlenv::Environments

source ·
pub struct Environments<S, T>where
    S: Ord,
{ /* private fields */ }
Expand description

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);

Implementations

Load the environments from a path.

Load the environments from a reader.

Get the current environment

Get the current environment from the given variable

Trait Implementations

Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Serialize this value into the given Serde serializer. Read more
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.