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
//! type abstraction [`Stg`] and other related elements.
//!
//! [`Stg`] type abstraction
//!
//! [`Setting`] is the trait that needs to be implemented for types for them to be turned into `Stg`
//!
//! [`StgError`] Error on conversion from `Stg` or T<&Stg> into S: Setting
//!
//! [`StgTrait`] Trait implement
//!
//!
//! # Example use of `Stg` in an [`Account`](crate::account::Account):
//!
//! ```rust
//! use hashmap_settings::{account::Account,stg::{Setting,Stg,StgTrait,StgError}};
//! //creating an account
//! let mut account = Account::<(),&str,Stg>::default();
//!
//! //inserting values of distinct types
//! account.insert("Number of trees",5.stg());
//! account.insert("Grass color","green".to_string().stg());
//! account.insert("Today is good",true.stg());
//!
//! //getting values from the account
//! let today_bool: bool = account.get(&"Today is good").unstg()?;
//! let grass_color: String = account.get(&"Grass color").unstg()?;
//! let trees: i32 = account.get(&"Number of trees").unstg()?;
//!
//! //example of using the values
//! print!("It's {today_bool} that today is a wonderful day,
//! the grass is {grass_color} and I can see {trees} trees in the distance");
//!
//! Ok::<(),StgError>(())
//! ```
///module containing implementations of `Setting` for rust types
use Debug;
use Any;
use DynClone;
use DynEq;
use ;
/// Required trait for conversion to abstract type [Stg]
///
/// For a Type to be able to implement Setting it needs to implement the traits
/// [Clone], [Debug], [PartialEq] (as well as [Deserialize](https://docs.rs/serde/latest/serde/trait.Deserialize.html) and [Serialize](https://docs.rs/serde/latest/serde/trait.Serialize.html) if the "serde" feature is activated )
///
/// In the [future](https://github.com/OxidizedLoop/HashMapSettings/issues/1) you will be able to derive Setting,
/// but for now you can do it by adding the following lines:
/// ```
/// # use hashmap_settings::stg::Setting;
/// # #[cfg(feature = "serde")]
/// # use serde::{Deserialize, Serialize};
/// #
/// # #[allow(dead_code)]
/// # #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// # #[derive(Clone, Debug, PartialEq)]
/// # pub struct MyType{}
/// #
/// # #[cfg_attr(feature = "serde", typetag::serde)]
/// // add #[typetag::serde] if serde feature is activated
/// impl Setting for MyType{}
/// ```
clone_trait_object!;
/// type abstraction for types implementing [`Setting`]
///
/// Types implementing `Setting` can be turned into a `Stg` with [.stg()](Setting::stg).
///
/// ```
/// use hashmap_settings::stg::{Setting,Stg};
/// # #[allow(unused_variables)]
/// let bool_stg: Stg = true.stg();
/// ```
///
/// They can be turned back to a specific type with [.unstg()](Stg::unstg) or [.unstg_panic()](Stg::unstg_panic)
///
/// ```
/// # use hashmap_settings::stg::{Setting,Stg};
/// # let bool_stg: Stg = true.stg();
/// # #[allow(unused_variables)]
/// let bool: bool = bool_stg.unstg()?;
/// # Ok::<(),Box<dyn core::any::Any>>(())
/// ```
///
/// Additionally there is the [`StgTrait`] that can be implemented for types containing `Stg` to allow
/// `.unstg()` and `.unstg_panic()` to be called on them.
///
/// The main example would be [Option<&Stg>]
///
/// ```
/// use std::collections::HashMap;
/// use hashmap_settings::stg::{Setting,Stg,StgError,StgTrait};
/// let bool_stg: Stg = true.stg();
/// let mut hashmap = HashMap::new();
/// hashmap.insert("bool",bool_stg);
/// # #[allow(unused_variables)]
/// let bool: bool = hashmap.get("bool").unstg()?;
/// # Ok::<(),StgError>(())
/// ```
/// [`Stg`] container converter trait
///
/// This trait is implemented by types to facilitate the conversion from
/// `T`<`Stg`> to a concrete type `S`.
///
/// Main example, and the use case of this crate, would be `Option<&Stg>` as it is what gets
/// returned when calling `get` on an `HashMap`/`Account`
///
/// #Example
/// ```
/// # use hashmap_settings::{account::Account,stg::{Setting,Stg,StgTrait,StgError}};
///
/// //creating a Stg Account
/// let mut account = Account::<(), &str, Stg>::default();
///
/// //inserting values of distinct types
/// account.insert("Number of trees", 5.stg());
/// account.insert("Grass color", "green".to_string().stg());
/// account.insert("Today is good", true.stg());
///
/// //getting values from the account in 3 different ways
/// let today_bool: bool = account.get(&"Today is good").unstg()?;
/// let grass_color: String = account.get(&"Grass color").unstg_panic();
/// let trees: i32 = account.get(&"Number of trees").unwrap().clone().unstg().unwrap();
/// //in the i32 example the last unwrap could be swapped for a "?" but it still would be a
/// //more complicated method chain than the other two alternatives.
///
/// //example of using the values
/// print!("It's {today_bool} that today is a wonderful day, the grass
/// is {grass_color} and I see {trees} trees in the distance");
/// # Ok::<(), StgError>(())
/// ```
///
/// Errors for [Stg] and [StgTrait] methods