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§
Source§impl<S, T> Environments<S, T>where
T: DeserializeOwned + Serialize,
S: DeserializeOwned + Serialize + Ord + PartialOrd + TryFrom<String>,
impl<S, T> Environments<S, T>where
T: DeserializeOwned + Serialize,
S: DeserializeOwned + Serialize + Ord + PartialOrd + TryFrom<String>,
Trait Implementations§
Source§impl<S, T: Clone> Clone for Environments<S, T>
impl<S, T: Clone> Clone for Environments<S, T>
Source§fn clone(&self) -> Environments<S, T>
fn clone(&self) -> Environments<S, T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<S, T: Debug> Debug for Environments<S, T>
impl<S, T: Debug> Debug for Environments<S, T>
Source§impl<'de, S, T> Deserialize<'de> for Environments<S, T>
impl<'de, S, T> Deserialize<'de> for Environments<S, T>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<S, T> Serialize for Environments<S, T>
impl<S, T> Serialize for Environments<S, T>
Source§impl<'a, S, T> TryFrom<&'a ArgMatches<'a>> for Environments<S, T>where
T: DeserializeOwned + Serialize,
S: DeserializeOwned + Serialize + Ord + PartialOrd + TryFrom<String>,
impl<'a, S, T> TryFrom<&'a ArgMatches<'a>> for Environments<S, T>where
T: DeserializeOwned + Serialize,
S: DeserializeOwned + Serialize + Ord + PartialOrd + TryFrom<String>,
Auto Trait Implementations§
impl<S, T> Freeze for Environments<S, T>
impl<S, T> RefUnwindSafe for Environments<S, T>where
S: RefUnwindSafe,
T: RefUnwindSafe,
impl<S, T> Send for Environments<S, T>
impl<S, T> Sync for Environments<S, T>
impl<S, T> Unpin for Environments<S, T>
impl<S, T> UnwindSafe for Environments<S, T>where
S: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more