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
/// Generates static getter methods for each config field defined in a struct.
///
/// This macro is used internally by [`config_generator!`] to expose typed access
/// to configuration fields.
///
/// Each generated method returns a `&'static` reference to its corresponding field,
/// so you'll typically want to use `.clone()`, `.as_str()`, or `*value` when needed.
)*
};
}
/// Extracts a typed field from a `HashMap<String, String>` with a fallback default.
///
/// This macro is used internally by [`config_generator!`] during the `.from_hashmap()`
/// process to parse and assign values to each field based on its type.
///
/// # Behavior
/// - For `String` types, it clones the value from the map.
/// - For other types, it attempts to parse the value using `.parse()`.
/// Generates a reusable static configuration struct with default values and typed getters.
///
/// This macro creates a global, lazily-initialized config object stored inside a `OnceLock`,
/// backed by a struct with typed fields and convenient static accessors.
///
/// # Parameters
/// - `$object`: Name of the struct (e.g., `AppConfig`)
/// - `$static_name`: Name of the `OnceLock` holding the config (e.g., `CONFIG`)
/// - Field list: A list of `(field_name, field_type, default_value)` tuples
///
/// # Features
/// - Strong typing for each config key
/// - Global access via static getters
/// - One-time initialization from `HashMap<String, String>`
/// - Safe fallback to defaults if a key is missing
///
/// # Notes
/// - Only the **first** call to `.from_hashmap()` initializes the config.
/// - Getter methods return `&'static` references. Use `.clone()`, `.as_str()`, or deref `*value` when needed.
///
/// # Example
/// ```rust
/// use std::collections::HashMap;
/// use macro_keeper::config_generator;
///
/// #[derive(PartialEq, Clone, Debug)]
/// enum LogLevel {
/// Debug,
/// Info,
/// }
///
/// impl std::str::FromStr for LogLevel {
/// type Err = String;
/// fn from_str(s: &str) -> Result<Self, Self::Err> {
/// match s {
/// "Debug" => Ok(LogLevel::Debug),
/// "Info" => Ok(LogLevel::Info),
/// _ => Err("Invalid log level".to_string()),
/// }
/// }
/// }
///
/// config_generator!(
/// AppConfig,
/// CONFIG,
/// [
/// (log_level, LogLevel, LogLevel::Info),
/// (buffer_capacity, usize, 1024),
/// (environment, String, "production".to_string())
/// ]
/// );
///
/// let mut map = HashMap::new();
/// map.insert("environment".to_string(), "development".to_string());
/// AppConfig::from_hashmap(Some(map));
///
/// // Accessing values
/// let level = AppConfig::log_level(); // &'static LogLevel
/// let capacity = AppConfig::buffer_capacity(); // &'static usize
/// let env = AppConfig::environment(); // &'static String
///
/// // Interacting with values
/// let double = *capacity * 2; // 2048
/// let is_prod = env.as_str() == "production"; // false
/// ```
};
}