Crates.io | dotenv-enum |
lib.rs | dotenv-enum |
version | 0.2.4 |
source | src |
created_at | 2023-02-12 19:26:11.044052 |
updated_at | 2023-02-12 22:40:50.157613 |
description | This crate creates an enum from a `.env` variable and simplify acces to them. |
homepage | |
repository | https://github.com/bhuzgujj/dotenv-enum.git |
max_upload_size | |
id | 783403 |
size | 32,501 |
This crate creates an enum from .env
variables and simplify access to them.
The macro will create a tests suite per enum to check if the variables are present in the .env
file.
note: this is my first crate, so it may not be the best for production
use dotenv_enum::{env_enum, EnvironmentVariable};
use strum::IntoEnumIterator;
env_enum!(TheEnumNameEnv, enum_test_module, [
ValueOne,
ValueTwo,
//...
]);
note: the comas on your last entry will break the macro
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 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()));
});
}
}
where all //... represent the same code as the previous one for all the new enum value you add.