1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! # Derive macro examples
//!
//! Various examples how to use [`DescribeConfig`](macro@crate::DescribeConfig) and other derive macros
//! from the library.
//!
//! # Basic usage
//!
//! Shows how to use the macros with nested and flattened sub-configs, enum configs etc.
//!
//! ```
//! # use std::{collections::HashSet, num::NonZeroUsize, time::Duration};
//! # use smart_config::{DescribeConfig, DeserializeConfig};
//! use smart_config::metadata::TimeUnit;
//!
//! #[derive(DescribeConfig, DeserializeConfig)]
//! struct TestConfig {
//! /// Doc comments are parsed as a description.
//! #[config(default_t = 3)]
//! int: u32,
//! #[config(default)] // multiple `config` attrs are supported
//! #[config(rename = "str", alias = "string")]
//! renamed: String,
//! /// Nested sub-config. E.g., the tag will be read from path `nested.version`.
//! #[config(nest)]
//! nested: NestedConfig,
//! /// Flattened sub-config. E.g., `array` param will be read from `array`, not `flat.array`.
//! #[config(flatten)]
//! flat: FlattenedConfig,
//! }
//!
//! #[derive(DescribeConfig, DeserializeConfig)]
//! #[config(tag = "version", rename_all = "snake_case", derive(Default))]
//! enum NestedConfig {
//! #[config(default)]
//! V0,
//! #[config(alias = "latest")]
//! V1 {
//! /// Param with a custom deserializer. In this case, it will deserialize
//! /// a duration from a number with milliseconds unit of measurement.
//! #[config(default_t = Duration::from_millis(50), with = TimeUnit::Millis)]
//! latency_ms: Duration,
//! /// `Vec`s, sets and other containers are supported out of the box.
//! set: HashSet<NonZeroUsize>,
//! },
//! }
//!
//! #[derive(DescribeConfig, DeserializeConfig)]
//! struct FlattenedConfig {
//! #[config(default = FlattenedConfig::default_array)]
//! array: [f32; 2],
//! }
//!
//! impl FlattenedConfig {
//! const fn default_array() -> [f32; 2] { [1.0, 2.0] }
//! }
//! ```
//!
//! # Deriving `ExampleConfig`
//!
//! ```
//! # use std::collections::HashSet;
//! # use smart_config::{DescribeConfig, ExampleConfig, SerializerOptions};
//! #[derive(DescribeConfig, ExampleConfig)]
//! struct TestConfig {
//! /// Required param that still has an example value.
//! #[config(example = 42)]
//! required: u32,
//! optional: Option<String>,
//! #[config(default_t = true)]
//! with_default: bool,
//! #[config(default, example = vec![5, 8])]
//! values: Vec<u32>,
//! #[config(nest)]
//! nested: NestedConfig,
//! }
//!
//! #[derive(DescribeConfig, ExampleConfig)]
//! struct NestedConfig {
//! #[config(default, example = ["eth_call".into()].into())]
//! methods: HashSet<String>,
//! }
//!
//! let example: TestConfig = TestConfig::example_config();
//! let json = SerializerOptions::default().serialize(&example);
//! assert_eq!(
//! serde_json::Value::from(json),
//! serde_json::json!({
//! "required": 42,
//! "optional": null,
//! "with_default": true,
//! "values": [5, 8],
//! "nested": {
//! "methods": ["eth_call"],
//! },
//! })
//! );
//! ```
//!
//! # Advanced features
//!
//! Demonstrates some advanced library features:
//!
//! - [Parameter validation](crate::validation)
//! - [Fallback values](crate::fallback)
//! - Complex deserializers: [`Delimited`](crate::de::Delimited), [`NamedEntries`](crate::de::NamedEntries)
//! and [`DelimitedEntries`](crate::de::DelimitedEntries)
//! - Use or [regexes](crate::pat) as delimiters and validations
//! - Deserialization of values [with units](crate::de::WithUnit), and the fixed-unit alternative
//! - [Deprecated aliases](super::derive_ref#deprecated) and the use of paths
//! - [Secret values](super::derive_ref#secret).
//!
//! ```
//! ```
//!
//! ## Matching YAML configuration
//!
//! ```yaml
//! ```
//!
//! ## Matching env variables
//!
//! Assumes the `APP_` prefix for env vars.
//!
//! ```shell
//! ```
//!
//! # See also
//!
//! - [Derive macro reference](super::derive_ref)
//! - [Combining config sources](super::sources)