From e12e01c1f6e888e55339a0054891a578d89dd373 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 5 Jan 2024 07:48:08 +0100 Subject: [PATCH] Final changes --- micronfig/Cargo.toml | 1 - micronfig/src/cache.rs | 133 +++++++++++++++++++++--------------- micronfig/src/lib.rs | 4 ++ micronfig_macros/Cargo.toml | 1 - micronfig_macros/src/lib.rs | 2 +- 5 files changed, 83 insertions(+), 58 deletions(-) diff --git a/micronfig/Cargo.toml b/micronfig/Cargo.toml index 281ab14..b6db6b4 100644 --- a/micronfig/Cargo.toml +++ b/micronfig/Cargo.toml @@ -11,7 +11,6 @@ categories = ["config"] [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--document-private-items"] [features] default = ["envvars", "envfiles", "envdot"] diff --git a/micronfig/src/cache.rs b/micronfig/src/cache.rs index 83ee089..a03c395 100644 --- a/micronfig/src/cache.rs +++ b/micronfig/src/cache.rs @@ -20,7 +20,7 @@ pub struct Cache { /// /// More can be added with [`Cache::envdot_register`]. #[cfg(feature = "envdot")] - pub envdot: Vec + pub envdot: Vec, } 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(&mut self, path: Path) + where Path: AsRef + 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 = "envfiles")] + pub fn get_from_envfiles(&self, key: &OsStr) -> Option { + crate::envfiles::get(key) + } + #[cfg(not(feature = "envfiles"))] + pub fn get_from_envfiles(&self, _key: &OsStr) -> Option { + None + } + + #[cfg(feature = "envvars")] + pub fn get_from_envvars(&self, key: &OsStr) -> Option { + crate::envvars::get(key) + } + #[cfg(not(feature = "envvars"))] + pub fn get_from_envvars(&self, _key: &OsStr) -> Option { + None + } + #[cfg(feature = "envdot")] - pub fn envdot_register(&mut self, path: Path) - where Path: AsRef + Debug - { - let dotenv = crate::envdot::parse_dotenv(path); - if let Some(dotenv) = dotenv { - self.envdot.push(dotenv); + pub fn get_from_envdot(&self, key: &OsStr) -> Option { + 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 { + 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"); - } + 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"); - } + 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())); - } + cache.envdot_register(envdot_file.as_os_str()); + assert_eq!(cache.get("ENVFILES".as_ref()), Some("envfiles".to_string())); + assert_eq!(cache.get("ENVVARS".as_ref()), Some("envvars".to_string())); + assert_eq!(cache.get("ENVDOT".as_ref()), Some("envdot".to_string())); assert_eq!(cache.get("NONE".as_ref()), None); } } diff --git a/micronfig/src/lib.rs b/micronfig/src/lib.rs index 7387ecb..c479e85 100644 --- a/micronfig/src/lib.rs +++ b/micronfig/src/lib.rs @@ -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; diff --git a/micronfig_macros/Cargo.toml b/micronfig_macros/Cargo.toml index 38c309d..82bf60b 100644 --- a/micronfig_macros/Cargo.toml +++ b/micronfig_macros/Cargo.toml @@ -11,7 +11,6 @@ categories = ["config"] [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--document-private-items"] [dependencies] syn = "2.0" diff --git a/micronfig_macros/src/lib.rs b/micronfig_macros/src/lib.rs index 40fe434..4051ab9 100644 --- a/micronfig_macros/src/lib.rs +++ b/micronfig_macros/src/lib.rs @@ -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)); }, };