micronfig/
lib.rs

1//! Crate for macro-based configuration management.
2//!
3//! ## Description
4//!
5//! This crate and it's sister [`micronfig_macros`] combine to provide the [`config`] macro, which allows the developer to define all configuration variables required by an application in a single place and have them expanded to static references of the desired types.
6//!
7//! ```
8//! micronfig::config! {
9//! 	DATABASE_URI,
10//! 	APPLICATION_NAME: String,
11//! 	MAX_CONCURRENT_USERS: String > u64,
12//! 	SHOWN_ALERT?,
13//! }
14//! ```
15//!
16//! ## Examples
17//!
18//! ### Strings configuration
19//!
20//! To define configuration variables returning a string, create a [`config`] block and list their names separated by commas `,`:
21//!
22//! ```
23//! micronfig::config! {
24//! 	VARIABLE_A,
25//! 	VARIABLE_B,
26//! 	VARIABLE_C,
27//! 	PATH,
28//! }
29//! ```
30//!
31//! To access them, call their name as if it was a function:
32//!
33//! ```
34//! # micronfig::config! {
35//! # 	VARIABLE_A,
36//! # 	VARIABLE_B,
37//! # 	VARIABLE_C,
38//! # 	PATH,
39//! # }
40//! #
41//! # std::env::set_var("VARIABLE_A", "a");
42//! # std::env::set_var("VARIABLE_B", "b");
43//! # std::env::set_var("VARIABLE_C", "c");
44//! # std::env::set_var("PATH", "/bin");
45//! #
46//! # if cfg!(feature = "envvars") {
47//! // These will all return `&'static str` values.
48//! println!("{}", VARIABLE_A());
49//! println!("{}", VARIABLE_B());
50//! println!("{}", VARIABLE_C());
51//! println!("{}", PATH());
52//! # }
53//! ```
54//!
55//! > ***Note***
56//! >
57//! > Both the [`config`] block and variables defined in it are lazily initialized on first call.
58//! >
59//! > The first time one of these functions is called, configuration files will be parsed, and the first time each is called, its value is retrieved and stored.
60//!
61//! ### Required and optional variables
62//!
63//! By default, configuration variables are all required, causing a [panic] if their value is missing the first time their function is called.
64//!
65//! Configuration variables can be marked as [Option]al by suffixing a question mark `?` to their name, making them return a `&'static` [`Option`] instead:
66//!
67//! ```
68//! micronfig::config! {
69//! 	VARIABLE_REQUIRED,
70//! 	VARIABLE_OPTIONAL?,
71//! }
72//! ```
73//!
74//! ### Conversions
75//!
76//! All variables are read from their source as strings; therefore, the following explicit syntax for defining them is supported:
77//!
78//! ```
79//! micronfig::config! {
80//! 	VARIABLE_A: String,
81//! 	VARIABLE_B: String,
82//! 	VARIABLE_C: String,
83//! }
84//! ```
85//!
86//! Strings are not the best option for most situations, so the crate makes use of some traits to allow their conversion to different types:
87//!
88//! | Trait | Symbol | Notes |
89//! |---|---|---|
90//! | [`From`] | `->` |  |
91//! | [`TryFrom`] | `=>` | Will panic if the conversion fails. |
92//! | [`std::str::FromStr`] | `>` | Will panic if the parsing fails. |
93//!
94//! The syntax for conversion is as follows:
95//!
96//! ```
97//! use std::net::SocketAddr;
98//!
99//! micronfig::config! {
100//!  	// use FromStr to parse the String as an isize
101//! 	REQUIRED_SIGNED: String > isize,
102//!  	// use FromStr to parse the String as a SocketAddr
103//!  	REQUIRED_SOCKETADDR: String > SocketAddr,
104//! 	// use From to convert the String to... another String
105//! 	REQUIRED_STRING: String -> String,
106//! 	// use TryFrom to convert the String to another String
107//!  	// (there aren't many types in std to make valid examples from!)
108//! 	REQUIRED_TRYSTRING: String => String,
109//! 	// the conversion will not be performed for missing optional variables
110//!		OPTIONAL_UNSIGNED?: String > usize,
111//! }
112//! ```
113//!
114//! Custom types can be used as well:
115//!
116//! ```
117//! struct Duplicator {
118//! 	copy_a: String,
119//! 	copy_b: String,
120//! }
121//!
122//! impl From<String> for Duplicator {
123//! 	fn from(value: String) -> Self {
124//!         Self {
125//! 			copy_a: value.clone(),
126//! 			copy_b: value
127//! 		}
128//!     }
129//! }
130//!
131//! micronfig::config! {
132//! 	MY_CUSTOM_TYPE: String -> Duplicator,
133//! }
134//!
135//! # fn main() {}
136//! ```
137//!
138//! Conversions can be chained too:
139//!
140//! ```
141//! struct ChatId(u64);
142//!
143//! impl From<u64> for ChatId {
144//! 	fn from(value: u64) -> Self {
145//!         Self(value)
146//!     }
147//! }
148//!
149//! micronfig::config! {
150//! 	// First parse the string as an u64 with FromStr, then convert it to a ChatId with From.
151//! 	RESPOND_TO_MESSAGES_IN: String > u64 -> ChatId,
152//! }
153//!
154//! # fn main() {}
155//! ```
156//!
157//! ## Crate features
158//!
159//! ### Value sources
160//!
161//! The crate supports retrieving values from various different sources depending on the needs of the application.
162//!
163//! The sources can be toggled on and off via crate features, and are listed in the following table in order of retrieval priority, where the topmost one is the first source that is checked, and the following ones are checked only if no value is detected in the ones above.
164//!
165//! | Feature | Description | Use case |
166//! |---|---|---|
167//! | `envfiles` | Contents of the file at the path indicated by the `{NAME}_FILE` environment variable. | Docker [configs](https://docs.docker.com/engine/swarm/configs/) and [secrets](https://docs.docker.com/engine/swarm/secrets/). |
168//! | `envvars` | The `{NAME}` environment variable. | Most command-line applications. |
169//! | `envdot` | The `.env` and `.env.local` files in the current working directory. | Application development. |
170//!
171//! By default, all of them are enabled.
172//!
173
174#![doc(html_logo_url = "https://raw.githubusercontent.com/Steffo99/micronfig/main/.media/icon-128x128_round.png")]
175
176/// The macro described at the crate's root.
177pub use micronfig_macros::config;
178
179pub mod cache;
180
181#[cfg(feature = "envvars")]
182pub mod envvars;
183
184#[cfg(feature = "envfiles")]
185pub mod envfiles;
186
187#[cfg(feature = "envdot")]
188pub mod envdot;
189
190#[cfg(test)]
191pub mod testing;
192