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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/// Generates a configuration loader for a struct with file and environment variable support.
///
/// This macro creates a complete configuration loading system including:
/// - A `load()` method that reads from config files and environment variables
/// - A thread-safe static singleton instance
/// - A getter function to access the configuration from anywhere in your app
///
/// # Usage
///
/// ## Basic Usage (with defaults)
///
/// ```rust,no_run
/// use dyson_boot::settings_struct;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Debug, Clone, Deserialize, Serialize)]
/// struct AppConfig {
/// pub host: String,
/// pub port: u16,
/// }
///
/// // Uses default values:
/// // - Config dir: APP__CONFIG_DIR env var (defaults to ".")
/// // - Config file: app_config.json
/// // - Env prefix: APP
/// // - Env separator: __
/// // - List separator: ,
/// settings_struct!(AppConfig);
///
/// fn main() {
/// let config = get_app_config();
/// println!("Host: {}", config.host);
/// }
/// ```
///
/// ## Custom Configuration
///
/// ```rust,no_run
/// use dyson_boot::settings_struct;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Debug, Clone, Deserialize, Serialize)]
/// struct DatabaseConfig {
/// pub url: String,
/// pub max_connections: u32,
/// }
///
/// settings_struct!(
/// DatabaseConfig,
/// "DB_CONFIG_DIR", // Environment variable for config directory
/// "database.json", // Config file name
/// load_env = (
/// "DATABASE", // Environment variable prefix
/// "__", // Environment variable separator
/// ","
/// ) // List separator for array values
/// )
///
/// fn main() {
/// let db_config = get_database_config();
/// println!("DB URL: {}", db_config.url);
/// }
/// ```
///
/// # Generated Code
///
/// For a struct named `AppConfig`, the macro generates:
/// - `impl AppConfig { fn load(config_path: PathBuf) -> anyhow::Result<Self> }`
/// - A static `APP_CONFIG: Lazy<Arc<AppConfig>>`
/// - A function `get_app_config() -> Arc<AppConfig>`
///
/// # Environment Variables
///
/// The macro supports overriding config values via environment variables:
///
/// ```bash
/// # Override top-level fields
/// export APP__host=0.0.0.0
/// export APP__port=3000
///
/// # Override nested fields (if using nested structs)
/// export APP__database__url=postgres://localhost/mydb
/// ```
///
/// # Configuration File Location
///
/// The config file path is determined by:
/// 1. Reading the directory from the specified environment variable (e.g., `APP__CONFIG_DIR`)
/// 2. If not set, defaults to current directory (`.`)
/// 3. Joins the directory with the config file name
///
/// # Panics
///
/// The generated code will panic and exit the process if:
/// - The configuration file cannot be found
/// - The configuration file contains invalid data
/// - Required fields are missing
///
/// # Requirements
///
/// Your configuration struct must:
/// - Implement `serde::Deserialize` and `serde::Serialize`
/// - Have all fields that can be deserialized from the config file format
///
/// # Examples
///
/// ## With Environment Variable Override
///
/// ```rust,no_run
/// use dyson_boot::settings_struct;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Debug, Clone, Deserialize, Serialize)]
/// struct ServerConfig {
/// pub bind_address: String,
/// pub port: u16,
/// pub workers: usize,
/// }
///
/// settings_struct!(
/// ServerConfig,
/// "SERVER_CONFIG_DIR",
/// "server.json",
/// load_env = ("SERVER",
/// "__",
/// ",")
/// );
///
/// fn main() {
/// // Set environment: export SERVER__port=9000
/// let config = get_server_config();
/// println!("Server will bind to {}:{}", config.bind_address, config.port);
/// }
/// ```
///
/// ## Multiple Configurations
///
/// You can use this macro multiple times for different config structs:
///
/// ```rust,no_run
/// use dyson_boot::settings_struct;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Debug, Clone, Deserialize, Serialize)]
/// struct AppConfig { pub name: String }
///
/// #[derive(Debug, Clone, Deserialize, Serialize)]
/// struct DbConfig { pub url: String }
///
/// settings_struct!(AppConfig);
/// settings_struct!(DbConfig, "DB_CONFIG_DIR", "db.json",load_env = ("DB", "__", ","));
///
/// fn main() {
/// let app = get_app_config();
/// let db = get_db_config();
/// println!("App: {}, DB: {}", app.name, db.url);
/// }
/// ```
}
paste!
paste!
};
}