smart_config/source/
macros.rs

1/// Creates [`Json`](crate::Json) configuration input based on the provided list of path–value tuples.
2/// This is essentially a slightly fancier / more specialized version of [`json!`](serde_json::json!).
3///
4/// A path must be a string literal, with segments separated by dots `.`. A value can be anything
5/// implementing [`Serialize`](serde::Serialize). Paths in a macro cannot coincide or be embedded into each other.
6/// The value produced by macro has [`Json`](crate::Json) type.
7///
8/// # Examples
9///
10/// ## Basic usage
11///
12/// ```
13/// let json = smart_config::config!(
14///     "test.int": 1,
15///     "test.str": "example",
16///     "test.flag": true,
17///     "test.structured.array": [1, 2, 3],
18/// );
19/// // Will create JSON input with an object at `test` with `int`, `str`, `flag`,
20/// // and `structured` fields, where `structured` is an object with `array` field.
21/// ```
22///
23/// ## Compilation-time checks
24///
25/// ```compile_fail
26/// let json = smart_config::config!(
27///     "test": false,
28///     "test.int": 123,
29///     // Compilation error: Path #0 `test` is a prefix of path #1 `test.int`
30/// );
31/// ```
32#[macro_export]
33macro_rules! config {
34    ($($path:tt : $value:expr),* $(,)?) => {
35        {
36            const _:() = {
37                $crate::metadata::_private::assert_paths(&[$($path,)*]);
38            };
39
40            #[allow(unused_mut)]
41            let mut json = $crate::Json::empty(&::std::format!("inline config at {}:{}", file!(), line!()));
42            $(
43            json.merge($path, $value);
44            )*
45            json
46        }
47    };
48}