Trait dotenv_enum::EnvironmentVariable
source · pub trait EnvironmentVariablewhere
Self: IntoEnumIterator,{
// Required method
fn get_key(&self) -> String;
// Provided methods
fn does_key_exist(key: &str) -> bool
where Self: IntoEnumIterator { ... }
fn get_enum_value_from_key(key: &str) -> Option<Self> { ... }
fn get_value(&self) -> EnvEnumResult<String> { ... }
fn unwrap_value(&self) -> String { ... }
fn get_casted_value<T: FromStr + Clone>(&self) -> EnvEnumResult<T>
where <T as FromStr>::Err: Debug { ... }
fn unwrap_casted_value<T: FromStr + Clone>(&self) -> T
where <T as FromStr>::Err: Debug { ... }
fn create_env_string(enum_name: &str, enum_value: &str) -> String { ... }
fn get_env_strings(enum_value: &str) -> Vec<String> { ... }
fn split_string_on_capitalize(value: String) -> Vec<String> { ... }
}Expand description
EnvironmentVariable
This trait is a link between the dotenv and your enums. The macro env_enum simplifies significantly its creation and its safety. The macro creates a suite of test verifying the enum value exist withing the dotenv.
Macro use
use dotenv_enum::{env_enum, EnvironmentVariable};
use strum::IntoEnumIterator;
env_enum!(TheEnumNameEnv, enum_test_module, [ValueOne, ValueTwo]);Self made
use dotenv_enum::EnvironmentVariable;
use strum::IntoEnumIterator;
#[derive(Copy, Clone, strum_macros::EnumIter, PartialEq, Debug)]
enum TheEnumNameEnv {
ValueOne,
ValueTwo,
// ...
}
impl EnvironmentVariable for TheEnumNameEnv {
fn get_key(&self) -> String {
match self {
TheEnumNameEnv::ValueOne => "THE_ENUM_NAME_VALUE_ONE".to_string(),
TheEnumNameEnv::ValueTwo => "THE_ENUM_NAME_VALUE_TWO".to_string(),
// ...
}
}
}
mod enum_test_module {
extern crate self as my_crate;
use std::assert_ne;
use strum::IntoEnumIterator;
use dotenv_enum::EnvironmentVariable;
#[allow(non_snake_case)]
mod when_using_an_element_it_should_be_in_dotenv {
extern crate self as my_crate;
use dotenv_enum::EnvironmentVariable;
#[test]
fn ValueOne() {
dotenv::dotenv().ok();
assert!(!my_crate::$enum_name::$var_name.unwrap_value().is_empty());
}
#[test]
fn ValueTwo() {
dotenv::dotenv().ok();
assert!(!my_crate::$enum_name::$var_name.unwrap_value().is_empty());
}
//...
}
#[test]
fn when_comparing_elements_they_are_all_different() {
my_crate::TheEnumNameEnv::iter().enumerate().for_each(|(index, env_var)| {
my_crate::TheEnumNameEnv::iter().enumerate()
.filter(|(index2, _)| index != *index2)
.for_each(|(_, env_var2)| assert_ne!(env_var.get_key(), env_var2.get_key()));
});
}
}Required Methods§
sourcefn get_key(&self) -> String
fn get_key(&self) -> String
Get the key string of this enum value
use dotenv_enum::{env_enum, EnvironmentVariable};
use strum::IntoEnumIterator;
env_enum!(TheEnumNameEnv, enum_test_module, [ValueOne, ValueTwo]);
assert_eq!(TheEnumNameEnv::ValueOne.get_key(), "THE_ENUM_NAME_VALUE_ONE".to_string());Provided Methods§
sourcefn does_key_exist(key: &str) -> boolwhere
Self: IntoEnumIterator,
fn does_key_exist(key: &str) -> boolwhere Self: IntoEnumIterator,
Verify the key exist within the enum
use dotenv_enum::{env_enum, EnvironmentVariable};
use strum::IntoEnumIterator;
env_enum!(TheEnumNameEnv, enum_test_module, [ValueOne, ValueTwo]);
assert_eq!(TheEnumNameEnv::does_key_exist("THE_ENUM_NAME_VALUE_ONE"), true);
assert_eq!(TheEnumNameEnv::does_key_exist("THE_ENUM_NAME_VALUE_THREE"), false);sourcefn get_enum_value_from_key(key: &str) -> Option<Self>
fn get_enum_value_from_key(key: &str) -> Option<Self>
Get the enum value having the key provided or None
use dotenv_enum::{env_enum, EnvironmentVariable};
use strum::IntoEnumIterator;
env_enum!(TheEnumNameEnv, enum_test_module, [ValueOne, ValueTwo]);
assert_eq!(TheEnumNameEnv::get_enum_value_from_key("THE_ENUM_NAME_VALUE_ONE"), Some(TheEnumNameEnv::ValueOne));
assert_eq!(TheEnumNameEnv::get_enum_value_from_key("THE_ENUM_NAME_VALUE_THREE"), None);sourcefn get_value(&self) -> EnvEnumResult<String>
fn get_value(&self) -> EnvEnumResult<String>
Get the value from the .env related to the enum value
use dotenv_enum::{env_enum, EnvironmentVariable};
use strum::IntoEnumIterator;
use dotenv_enum::env_errors::EnvEnumResult;
dotenv::dotenv().ok();
env_enum!(TheEnumNameEnv, enum_test_module, [ValueOne, ValueTwo]);
// Assuming the line:
// THE_ENUM_NAME_VALUE_ONE = "val"
// exist in the .env
assert_eq!(TheEnumNameEnv::ValueOne.get_value(), EnvEnumResult::Ok("val".to_string()));
// Assuming the key THE_ENUM_NAME_VALUE_TWO does not exist in .env
assert_eq!(TheEnumNameEnv::ValueTwo.get_value(), EnvEnumResult::Absent("No THE_ENUM_NAME_VALUE_TWO in .env file".to_string()));sourcefn unwrap_value(&self) -> String
fn unwrap_value(&self) -> String
Get the value from the .env related to the enum value and unwrap it This function will panic instead of sending an Err
use dotenv_enum::{env_enum, EnvironmentVariable};
use strum::IntoEnumIterator;
dotenv::dotenv().ok();
env_enum!(TheEnumNameEnv, enum_test_module, [ValueOne, ValueTwo]);
// Assuming the line:
// THE_ENUM_NAME_VALUE_ONE = "val"
// exist in the .env
assert_eq!(TheEnumNameEnv::ValueOne.unwrap_value(), "val".to_string());sourcefn get_casted_value<T: FromStr + Clone>(&self) -> EnvEnumResult<T>where
<T as FromStr>::Err: Debug,
fn get_casted_value<T: FromStr + Clone>(&self) -> EnvEnumResult<T>where <T as FromStr>::Err: Debug,
Get the value from the .env related to the enum value and casted it into the type T
use dotenv_enum::{env_enum, EnvironmentVariable};
use strum::IntoEnumIterator;
use dotenv_enum::env_errors::EnvEnumResult;
dotenv::dotenv().ok();
env_enum!(TheEnumNameEnv, enum_test_module, [ValueOne, ValueTwo]);
// Assuming the line:
// THE_ENUM_NAME_VALUE_ONE = "val"
// exist in the .env
assert_eq!(TheEnumNameEnv::ValueOne.get_casted_value::<String>(), EnvEnumResult::Ok("val".to_string()));
assert_eq!(TheEnumNameEnv::ValueOne.get_casted_value::<u32>(), EnvEnumResult::IncorrectCast("Cannot cast THE_ENUM_NAME_VALUE_ONE into u32".to_string()));
// Assuming the key THE_ENUM_NAME_VALUE_TWO does not exist in .env
assert_eq!(TheEnumNameEnv::ValueTwo.get_value(), EnvEnumResult::Absent("No THE_ENUM_NAME_VALUE_TWO in .env file".to_string()));sourcefn unwrap_casted_value<T: FromStr + Clone>(&self) -> Twhere
<T as FromStr>::Err: Debug,
fn unwrap_casted_value<T: FromStr + Clone>(&self) -> Twhere <T as FromStr>::Err: Debug,
Get the value from the .env related to the enum value and casted it into the type T This function will panic instead of sending an Err
use dotenv_enum::{env_enum, EnvironmentVariable};
use strum::IntoEnumIterator;
dotenv::dotenv().ok();
env_enum!(TheEnumNameEnv, enum_test_module, [ValueOne, ValueTwo]);
// Assuming the line:
// THE_ENUM_NAME_VALUE_ONE = "val"
// exist in the .env
assert_eq!(TheEnumNameEnv::ValueOne.unwrap_casted_value::<String>(), "val".to_string());sourcefn create_env_string(enum_name: &str, enum_value: &str) -> String
fn create_env_string(enum_name: &str, enum_value: &str) -> String
Create a full capitalize, seperated by underscored, without suffix Env, and merge name_value
use dotenv_enum::{env_enum, EnvironmentVariable};
use strum::IntoEnumIterator;
env_enum!(TheEnumNameEnv, enum_test_module, [Value]);
assert_eq!(TheEnumNameEnv::create_env_string("LolEnv", "AValue"), "LOL_A_VALUE".to_string())sourcefn get_env_strings(enum_value: &str) -> Vec<String>
fn get_env_strings(enum_value: &str) -> Vec<String>
Create a vector of all the words seperated by either underscores or capital letters
use dotenv_enum::{env_enum, EnvironmentVariable};
use strum::IntoEnumIterator;
env_enum!(TheEnumNameEnv, enum_test_module, [Value]);
assert_eq!(TheEnumNameEnv::get_env_strings("LolEnv_Ok"), vec!["Lol", "Env", "Ok"])sourcefn split_string_on_capitalize(value: String) -> Vec<String>
fn split_string_on_capitalize(value: String) -> Vec<String>
Split a string on capital letter and keep it
use dotenv_enum::{env_enum, EnvironmentVariable};
use strum::IntoEnumIterator;
env_enum!(TheEnumNameEnv, enum_test_module, [Value]);
assert_eq!(TheEnumNameEnv::split_string_on_capitalize("LolEnv_Ok".to_string()), vec!["Lol", "Env_", "Ok"])