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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! # Environment Variable Utilities
//!
//! This module provides comprehensive environment variable handling with type-safe parsing,
//! JSON configuration support via our serde module, and parallel processing capabilities.
//!
//! ## Overview
//!
//! The `env` module offers:
//! - **Basic Environment Access**: Reading variables with defaults and type parsing
//! - **JSON Configuration**: Parse complex JSON configs from environment variables
//! - **Batch Processing**: Parallel reading of multiple environment variables
//! - **Validation**: Check for required environment variables
//!
//! ## Usage Patterns
//!
//! ### Basic Environment Variable Access
//! ```rust
//! use trash_analyzer::sys::env::*;
//!
//! let home = read_env_var("HOME").unwrap();
//! let port: u16 = read_env_var_parse("PORT").unwrap_or(8080);
//! let debug = read_env_var_or("DEBUG", "false");
//! ```
//!
//! ### JSON Configuration from Environment
//! ```rust
//! use trash_analyzer::sys::env::read_env_var_json;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Deserialize, Serialize)]
//! struct Config { host: String, port: u16 }
//!
//! let config: Config = read_env_var_json("APP_CONFIG").unwrap();
//! ```
//!
//! ### Batch Processing
//! ```rust
//! use trash_analyzer::sys::env::read_env_vars_parallel;
//!
//! let keys = vec!["HOME", "USER", "PATH"];
//! let values = read_env_vars_parallel(&keys).unwrap();
//! ```
use crateparallel;
use crateserde;
use DeserializeOwned;
use HashMap;
/// Type-safe environment variable handling with advanced parsing capabilities.
///
/// This module provides comprehensive environment variable utilities with
/// type-safe parsing, JSON configuration support, parallel processing, and
/// validation. Designed for robust application configuration management.
///
/// ## Features
///
/// - **Type-Safe Parsing**: Automatic conversion to Rust types with error handling
/// - **JSON Configuration**: Parse complex nested configurations from env vars
/// - **Default Values**: Graceful fallback to defaults when variables are missing
/// - **Parallel Processing**: Concurrent reading of multiple environment variables
/// - **Validation**: Check for required environment variables with detailed errors
/// - **Error Propagation**: Comprehensive error reporting with context
///
/// ## Examples
///
/// ### Application Configuration
/// ```rust
/// use trash_utilities::sys::env::*;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Deserialize, Serialize)]
/// struct DatabaseConfig {
/// host: String,
/// port: u16,
/// database: String,
/// max_connections: u32,
/// }
///
/// #[derive(Deserialize, Serialize)]
/// struct AppConfig {
/// debug: bool,
/// log_level: String,
/// database: DatabaseConfig,
/// }
///
/// fn load_config() -> Result<AppConfig, Box<dyn std::error::Error>> {
/// let debug: bool = read_env_var_parse("DEBUG").unwrap_or(false);
/// let log_level = read_env_var_or("LOG_LEVEL", "info");
///
/// // Load complex JSON config from environment
/// let db_config: DatabaseConfig = read_env_var_json("DATABASE_CONFIG")?;
///
/// Ok(AppConfig {
/// debug,
/// log_level: log_level.to_string(),
/// database: db_config,
/// })
/// }
/// ```
///
/// ### Docker Environment Variables
/// ```rust,no_run
/// use trash_utilities::sys::env::*;
///
/// // Common Docker environment variables
/// let port: u16 = read_env_var_parse("PORT").unwrap_or(8080);
/// let host = read_env_var_or("HOST", "0.0.0.0");
/// let workers: usize = read_env_var_parse("WEB_CONCURRENCY").unwrap_or(4);
///
/// println!("Starting server on {}:{} with {} workers", host, port, workers);
/// ```
///
/// ### Configuration Validation
/// ```rust
/// use trash_utilities::sys::env::*;
///
/// fn validate_environment() -> Result<(), Vec<String>> {
/// let required_vars = vec![
/// "DATABASE_URL",
/// "REDIS_URL",
/// "JWT_SECRET"
/// ];
///
/// validate_required_env_vars(&required_vars)?;
///
/// // Additional custom validation
/// if let Ok(port_str) = read_env_var("PORT") {
/// if port_str.parse::<u16>().is_err() {
/// return Err(vec!["PORT must be a valid port number".to_string()]);
/// }
/// }
///
/// Ok(())
/// }
/// ```
///
/// ### Parallel Environment Loading
/// ```rust
/// use trash_utilities::sys::env::*;
/// use std::collections::HashMap;
///
/// fn load_service_configs() -> HashMap<String, String> {
/// let service_vars = vec![
/// "AUTH_SERVICE_URL",
/// "USER_SERVICE_URL",
/// "PAYMENT_SERVICE_URL",
/// "NOTIFICATION_SERVICE_URL",
/// ];
///
/// read_env_vars_parallel(&service_vars)
/// }
///
/// // Usage
/// let services = load_service_configs();
/// for (service, url) in services {
/// if !url.is_empty() {
/// println!("{} configured: {}", service, url);
/// }
/// }
/// ```
///
/// ### Error Handling Patterns
/// ```rust
/// use trash_utilities::sys::env::*;
///
/// fn robust_config_loading() {
/// // Handle missing variables gracefully
/// match read_env_var("REQUIRED_VAR") {
/// Ok(value) => println!("Required var: {}", value),
/// Err(e) => eprintln!("Missing required variable: {}", e),
/// }
///
/// // Type conversion with fallbacks
/// let timeout: u64 = read_env_var_parse("TIMEOUT_SECONDS")
/// .unwrap_or_else(|_| {
/// eprintln!("Invalid TIMEOUT_SECONDS, using default");
/// 30
/// });
///
/// // JSON parsing with detailed errors
/// match read_env_var_json::<serde_json::Value>("COMPLEX_CONFIG") {
/// Ok(config) => println!("Config loaded: {:?}", config),
/// Err(e) => eprintln!("Failed to parse config: {}", e),
/// }
/// }
/// ```
/// Reads an environment variable as a string.
///
/// # Parameters
/// - `key`: The name of the environment variable.
///
/// # Returns
/// - `Ok(String)` containing the value if the variable exists.
/// - `Err(std::env::VarError)` if the variable is not set or invalid.
///
/// # Errors
/// Returns a `std::env::VarError` if the environment variable is not set or contains invalid Unicode.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::env::read_env_var;
///
/// let home = read_env_var("HOME").unwrap();
/// println!("Home directory: {}", home);
/// ```
/// Reads an environment variable with a default value.
///
/// # Parameters
/// - `key`: The name of the environment variable.
/// - `default`: The default value to return if the variable is not set.
///
/// # Returns
/// The value of the environment variable or the default.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::env::read_env_var_or;
///
/// let port = read_env_var_or("PORT", "8080");
/// println!("Port: {}", port);
/// ```
/// Reads an environment variable and parses it as a type that implements `FromStr`.
///
/// # Type Parameters
/// - `T`: The type to parse the value into, must implement `FromStr`.
///
/// # Parameters
/// - `key`: The name of the environment variable.
///
/// # Returns
/// - `Ok(T)` if parsing succeeds.
/// - `Err` if the variable is not set or parsing fails.
///
/// # Errors
/// Returns an error if the environment variable is not set, or if parsing the value fails.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::env::read_env_var_parse;
///
/// let port: u16 = read_env_var_parse("PORT").unwrap_or(8080);
/// println!("Port: {}", port);
/// ```
/// Reads an environment variable containing JSON and deserializes it to a type using our serde module.
///
/// # Type Parameters
/// - `T`: The type to deserialize into, must implement `Deserialize`.
///
/// # Parameters
/// - `key`: The name of the environment variable containing JSON.
///
/// # Returns
/// - `Ok(T)` if the variable exists and JSON parsing succeeds.
/// - `Err` if the variable is not set or parsing fails.
///
/// # Errors
/// - Returns `std::env::VarError` if the environment variable is not set or contains invalid Unicode.
/// - Returns `serde_json::Error` if the JSON parsing fails.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::env::read_env_var_json;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Config { host: String, port: u16 }
///
/// let config: Config = read_env_var_json("APP_CONFIG").unwrap();
/// ```
/// Reads multiple environment variables in parallel using our parallel module.
///
/// This function processes a collection of environment variable keys concurrently,
/// which can improve performance when reading many variables.
///
/// # Parameters
/// - `keys`: A slice of environment variable names to read.
///
/// # Returns
/// A `HashMap` where keys are the variable names and values are the variable values.
/// Variables that are not set or contain errors are omitted from the result.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::env::read_env_vars_parallel;
///
/// let keys = vec!["HOME", "USER", "PATH"];
/// let vars = read_env_vars_parallel(&keys);
/// for (key, value) in vars {
/// println!("{}: {}", key, value);
/// }
/// ```
/// Validates that all required environment variables are set.
///
/// # Parameters
/// - `required_keys`: A slice of environment variable names that must be set.
///
/// # Returns
/// - `Ok(())` if all required variables are set.
/// - `Err(Vec<String>)` containing the names of missing variables.
///
/// # Errors
/// Returns a `Vec<String>` containing the names of environment variables that are not set.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::env::validate_required_env_vars;
///
/// let required = vec!["HOME", "USER"];
/// match validate_required_env_vars(&required) {
/// Ok(()) => println!("All required env vars are set"),
/// Err(missing) => println!("Missing: {:?}", missing),
/// }
/// ```