cfg_rs/
lib.rs

1#![doc = include_str!("../README.md")]
2#![doc(issue_tracker_base_url = "https://github.com/leptonyu/cfg-rs/issues/")]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4#![warn(
5    anonymous_parameters,
6    missing_copy_implementations,
7    missing_debug_implementations,
8    missing_docs,
9    nonstandard_style,
10    rust_2018_idioms,
11    single_use_lifetimes,
12    trivial_casts,
13    trivial_numeric_casts,
14    unreachable_pub,
15    unused_extern_crates,
16    unused_qualifications,
17    variant_size_differences
18)]
19
20#[cfg(test)]
21mod test;
22#[cfg(test)]
23#[macro_use(quickcheck)]
24extern crate quickcheck_macros;
25
26mod cache;
27mod configuration;
28mod derive;
29mod err;
30mod key;
31
32pub mod source;
33mod value;
34mod value_ref;
35
36use key::PartialKeyCollector;
37
38/// Automatic derive [`FromConfig`] instance.
39///
40/// We use annotation attributes to customize the derived instances' behavior.
41/// All attributes in `cfg-rs` have format `#[config(key = value, key2 = value2)]`.
42///
43/// # Struct Annotation Attribute
44///
45/// * `#[config(prefix = "cfg.app")]`
46///
47/// This attr will lead to implement trait [`FromConfigWithPrefix`].
48///
49/// ```ignore,rust
50/// #[derive(FromConfig)]
51/// #[config(prefix = "cfg.test")]
52/// struct Test {
53///   //fields...   
54/// }
55/// ```
56///
57/// # Field Annotation Attribute
58///
59/// * `#[config(name = "val")]`
60///
61/// This attr will replace the default config partial key, which is name of field.
62///
63/// ```ignore,rust
64/// #[derive(FromConfig)]
65/// struct Test {
66///   val: u8,
67///   #[config(name = "val")]
68///   other: u8, // This field `other` will use the same partial key as `val`.
69/// }
70/// ```
71///
72/// * `#[config(default = true)]`
73///
74/// This attr provides default value for underlying field.
75///
76/// ```ignore,rust
77/// #[derive(FromConfig)]
78/// struct Test {
79///   enabled: bool, // User must provide value for this field.
80///   #[config(default = true)]
81///   enabled_with_default: bool, // This field has default value `true`.
82/// }
83/// ```
84pub use cfg_derive::FromConfig;
85pub use configuration::{ConfigContext, Configuration, PredefinedConfigurationBuilder};
86pub use derive::FromConfigWithPrefix;
87pub use err::ConfigError;
88pub(crate) use err::ConfigLock;
89pub use key::ConfigKey;
90#[allow(unused_imports)]
91#[cfg(feature = "log")]
92pub use value::log as _;
93#[allow(unused_imports)]
94#[cfg(feature = "coarsetime")]
95pub use value::time as _;
96pub use value::{ConfigValue, FromStrHolder, FromStringValue, FromValue};
97pub use value_ref::RefValue;
98
99#[doc(hidden)]
100pub use source::cargo::Cargo;
101#[doc(hidden)]
102pub use source::file::inline_source_config;
103
104use std::sync::*;
105
106pub(crate) mod macros {
107    macro_rules! cfg_log {
108    ($b:expr => $lvl:expr,$($arg:tt)+) => {
109        #[cfg(feature = "log")]
110        {
111            if  $b {
112                log::log!($lvl, $($arg)+);
113            }
114        }
115    };
116    ($lvl:expr,$($arg:tt)+) => {
117        #[cfg(feature = "log")]
118        {
119            log::log!($lvl, $($arg)+);
120        }
121    };
122    }
123
124    macro_rules! impl_default {
125        ($x:ident) => {
126            impl Default for $x {
127                fn default() -> Self {
128                    Self::new()
129                }
130            }
131        };
132    }
133
134    pub(crate) use cfg_log;
135    pub(crate) use impl_default;
136}
137
138/// Generate config instance from configuration.
139///
140/// The most power of this crate is automatically deriving this trait.
141/// Please refer to [Derive FromConfig](./derive.FromConfig.html) for details.
142pub trait FromConfig: Sized {
143    /// Generate config from [`ConfigValue`] under context.
144    fn from_config(
145        context: &mut ConfigContext<'_>,
146        value: Option<ConfigValue<'_>>,
147    ) -> Result<Self, ConfigError>;
148}