1
Fork 0
mirror of https://github.com/Steffo99/micronfig.git synced 2024-10-16 14:37:29 +00:00

Final changes

This commit is contained in:
Steffo 2024-01-05 07:48:08 +01:00
parent eb70a3f180
commit e12e01c1f6
Signed by: steffo
GPG key ID: 2A24051445686895
5 changed files with 83 additions and 58 deletions

View file

@ -11,7 +11,6 @@ categories = ["config"]
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--document-private-items"]
[features]
default = ["envvars", "envfiles", "envdot"]

View file

@ -20,7 +20,7 @@ pub struct Cache {
///
/// More can be added with [`Cache::envdot_register`].
#[cfg(feature = "envdot")]
pub envdot: Vec<crate::envdot::DotEnv>
pub envdot: Vec<crate::envdot::DotEnv>,
}
impl Cache {
@ -28,14 +28,42 @@ impl Cache {
pub fn new() -> Self {
let mut this = Self::default();
if cfg!(feature = "envdot") {
this.envdot_register("./.env.local");
this.envdot_register("./.env");
}
this.init_envfiles();
this.init_envvars();
this.init_envdot();
this
}
#[cfg(feature = "envfiles")]
fn init_envfiles(&mut self) {}
#[cfg(not(feature = "envfiles"))]
fn init_envfiles(&mut self) {}
#[cfg(feature = "envvars")]
fn init_envvars(&mut self) {}
#[cfg(not(feature = "envvars"))]
fn init_envvars(&mut self) {}
#[cfg(feature = "envdot")]
fn init_envdot(&mut self) {
self.envdot_register("./.env.local");
self.envdot_register("./.env");
}
#[cfg(not(feature = "envdot"))]
fn init_envdot(&mut self) {}
/// Register a new `.env` file in the cache, if it exists.
#[cfg(feature = "envdot")]
pub fn envdot_register<Path>(&mut self, path: Path)
where Path: AsRef<std::path::Path> + Debug
{
let dotenv = crate::envdot::parse_dotenv(path);
if let Some(dotenv) = dotenv {
self.envdot.push(dotenv);
}
}
/// Get a value from the cache.
///
/// The following sources, if the respective feature is enabled, are checked in the following order:
@ -47,35 +75,44 @@ impl Cache {
{
let mut value = None;
if cfg!(feature = "envfiles") {
value = crate::envfiles::get(key);
}
if cfg!(feature = "envvars") && value.is_none() {
value = crate::envvars::get(key);
}
if cfg!(feature = "envdot") && value.is_none() {
for dotenv in self.envdot.iter() {
value = crate::envdot::get(dotenv, key);
if value.is_some() {
break;
}
}
}
if value.is_none() { value = self.get_from_envfiles(key); }
if value.is_none() { value = self.get_from_envvars(key); }
if value.is_none() { value = self.get_from_envdot(key); }
value
}
/// Register a new `.env` file in the cache, if it exists.
#[cfg(feature = "envdot")]
pub fn envdot_register<Path>(&mut self, path: Path)
where Path: AsRef<std::path::Path> + Debug
{
let dotenv = crate::envdot::parse_dotenv(path);
if let Some(dotenv) = dotenv {
self.envdot.push(dotenv);
#[cfg(feature = "envfiles")]
pub fn get_from_envfiles(&self, key: &OsStr) -> Option<String> {
crate::envfiles::get(key)
}
#[cfg(not(feature = "envfiles"))]
pub fn get_from_envfiles(&self, _key: &OsStr) -> Option<String> {
None
}
#[cfg(feature = "envvars")]
pub fn get_from_envvars(&self, key: &OsStr) -> Option<String> {
crate::envvars::get(key)
}
#[cfg(not(feature = "envvars"))]
pub fn get_from_envvars(&self, _key: &OsStr) -> Option<String> {
None
}
#[cfg(feature = "envdot")]
pub fn get_from_envdot(&self, key: &OsStr) -> Option<String> {
for dotenv in self.envdot.iter() {
let value = crate::envdot::get(dotenv, key);
if value.is_some() {
return value
}
}
None
}
#[cfg(not(feature = "envdot"))]
pub fn get_from_envdot(&self, _key: &OsStr) -> Option<String> {
None
}
}
@ -140,6 +177,7 @@ mod tests {
}
#[test]
#[cfg(all(feature = "envdot", feature = "envfiles", feature = "envvars"))]
fn priority() {
let mut cache = Cache::default();
@ -154,36 +192,21 @@ mod tests {
"#
);
if cfg!(feature = "envfiles") {
std::env::set_var("ENVFILES_FILE", envfiles_file.as_os_str());
std::env::remove_var("ENVVARS_FILE");
std::env::remove_var("ENVDOT_FILE");
std::env::remove_var("NONE_FILE");
}
if cfg!(feature = "envvars") {
std::env::set_var("ENVFILES", "envvars");
std::env::set_var("ENVVARS", "envvars");
std::env::remove_var("ENVDOT");
std::env::remove_var("NONE");
}
if cfg!(feature = "envdot") {
cache.envdot_register(envdot_file.as_os_str());
}
if cfg!(feature = "envfiles") {
assert_eq!(cache.get("ENVFILES".as_ref()), Some("envfiles".to_string()));
}
if cfg!(feature = "envvars") {
assert_eq!(cache.get("ENVVARS".as_ref()), Some("envvars".to_string()));
}
if cfg!(feature = "envdot") {
assert_eq!(cache.get("ENVDOT".as_ref()), Some("envdot".to_string()));
}
assert_eq!(cache.get("NONE".as_ref()), None);
}
}

View file

@ -43,11 +43,13 @@
//! # std::env::set_var("VARIABLE_C", "c");
//! # std::env::set_var("PATH", "/bin");
//! #
//! # if cfg!(feature = "envvars") {
//! // These will all return `&'static str` values.
//! println!("{}", VARIABLE_A());
//! println!("{}", VARIABLE_B());
//! println!("{}", VARIABLE_C());
//! println!("{}", PATH());
//! # }
//! ```
//!
//! > ***Note***
@ -171,6 +173,8 @@
//! By default, all of them are enabled.
//!
#![doc(html_logo_url = "https://raw.githubusercontent.com/Steffo99/micronfig/main/icon.png")]
/// The macro described at the crate's root.
pub use micronfig_macros::config;

View file

@ -11,7 +11,6 @@ categories = ["config"]
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--document-private-items"]
[dependencies]
syn = "2.0"

View file

@ -169,7 +169,7 @@ pub fn config(input: TokenStream) -> TokenStream {
true => quote! {},
false => quote! {
let value: String = value
.expect(&format!("to be have {} set", #identifier_string));
.expect(&format!("that configuration variable {} was set", #identifier_string));
},
};