config_source/lib.rs
1//! # config-source
2//!
3//! Macro for deriving [config-rs](https://github.com/mehcode/config-rs)'s `config::Source` trait.
4//!
5//! ## Usage
6//!
7//! To derive `config::Source` for a struct, simply add the `#[derive(ConfigSource)]` attribute to
8//! the struct:
9//!
10//! ```
11//! #[derive(serde::Deserialize, config_source::ConfigSource, Clone, Debug)]
12//! pub struct MyConfig {
13//! pub question: String,
14//! pub answer: u64,
15//! }
16//!
17//! impl Default for MyConfig {
18//! fn default() -> Self {
19//! Self {
20//! question: String::from("The Ultimate Question of Life, the Universe, and Everything"),
21//! answer: 42,
22//! }
23//! }
24//! }
25//! ```
26//!
27//! Then, you can use the `config::Config` struct to load your configuration using a default value:
28//!
29//! ```
30//! # #[derive(serde::Deserialize, config_source::ConfigSource, Clone, Debug)]
31//! # pub struct MyConfig {
32//! # pub question: String,
33//! # pub answer: u64,
34//! # }
35//! #
36//! # impl Default for MyConfig {
37//! # fn default() -> Self {
38//! # Self {
39//! # question: String::from("The Ultimate Question of Life, the Universe, and Everything"),
40//! # answer: 42,
41//! # }
42//! # }
43//! # }
44//! #
45//! let config = config::Config::builder()
46//! .add_source(MyConfig::default()) // Default value as source!
47//! .add_source(config::File::with_name("my_config.toml").required(false))
48//! .add_source(config::Environment::with_prefix("MY_CONFIG").separator("__"))
49//! .build()
50//! .expect("Failed to build `MyConfig`");
51//! ```
52
53#![warn(missing_docs)]
54
55mod config;
56
57use proc_macro::TokenStream;
58
59/// Derives `config::Source` for a struct.
60#[proc_macro_derive(ConfigSource)]
61pub fn config_source(input: TokenStream) -> TokenStream {
62 config::derive(input)
63}