Trait AsEnvStr

Source
pub trait AsEnvStr {
    // Required method
    fn as_env_str<T: EnvKey>(&self) -> String;
}
Expand description

AsEnvTypeStr is a trait that covert some type to a string with Key Type, which is the environment type. This trait can extend the existing configuration struct to get the environment type.

§Example

use env_type::types::{EnvType, EnvKey, AsEnvStr};
use std::collections::HashMap;

struct Config {
   map: HashMap<&'static str, String>,
}

impl AsEnvStr for Config {
  fn as_env_str<T: EnvKey>(&self) -> String {
     self.map.get(T::key()).map(|v|v.to_string()).unwrap_or_default()
 }
}
let mut map = HashMap::new();
map.insert("ENV", "test".to_string());
let config = Config {
  map,
};
assert_eq!("test", config.as_env_str::<EnvType>());

Required Methods§

Source

fn as_env_str<T: EnvKey>(&self) -> String

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl AsEnvStr for EnvType

EnvType is an implementation of the AsEnvStr trait. EnvType is based on env var.