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]
impl<S, T> Environments<S, T> where
T: DeserializeOwned + Serialize,
S: DeserializeOwned + Serialize + Ord + PartialOrd + TryFrom<String>,
Error: From<<S as TryFrom<String>>::Error>,
pub fn from_path(path: &Path) -> Result<Self>
[src]
pub fn from_path(path: &Path) -> Result<Self>
Load the environments from a path.
pub fn from_reader<R>(reader: &mut R) -> Result<Self> where
R: Read,
[src]
pub fn from_reader<R>(reader: &mut R) -> Result<Self> where
R: Read,
Load the environments from a reader.
pub fn current(&self) -> Result<&T>
[src]
pub fn current(&self) -> Result<&T>
Get the current environment
Trait Implementations
impl<S: Debug, T: Debug> Debug for Environments<S, T> where
S: Ord,
[src]
impl<S: Debug, T: Debug> Debug for Environments<S, T> where
S: Ord,
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
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]
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>,
Auto Trait Implementations
impl<S, T> Send for Environments<S, T> where
S: Send,
T: Send,
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,
impl<S, T> Sync for Environments<S, T> where
S: Sync,
T: Sync,