mkenv 1.0.2

Lightweight yet useful library to help you define the environment your app needs
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#[doc(hidden)]
#[macro_export]
macro_rules! make_config_impl {
    // ---------------
    // --------------- field config -> type
    // ---------------

    (@__field_config_ty $Config:ty) => {
        $Config
    };

    (@__field_config_ty
        var_name: $_var_name:literal
        $(, $($rest:tt)*)?
    ) => {
        $crate::__private::make_config_impl!(@__field_config_ty_layers $($($rest)*)?)
    };

    (@__field_config_ty_layers layers: [] $($_rest:tt)*) => {
        $crate::layers::TextVar
    };

    (@__field_config_ty_layers layers: [$($layers_tt:tt)+] $($_rest:tt)*) => {
        $crate::__private::make_config_impl!(@__field_config_ty_layers_content $($layers_tt)+)
    };

    // Case where the `layers` key wasn't provided
    (@__field_config_ty_layers $($_rest:tt)*) => {
        $crate::layers::TextVar
    };

    (@__field_config_ty_layers_content
        $($func_ident:ident $(<$($func_gen:ty),* $(,)?>)? ($($func_content:tt)*)),* $(,)?
    ) => {
        $crate::__private::make_config_impl!(@__field_config_ty_layer
            [$([ $func_ident $(<$($func_gen),*>)? ($($func_content)*) ])*]
            $crate::layers::TextVar
        )
    };

    // ---------------
    // --------------- layer kind -> type
    // ---------------

    // End case
    (@__field_config_ty_layer [] $($wrapped:tt)* ) => {
        $($wrapped)*
    };

    (@__field_config_ty_layer [[cached()] $([$($rest:tt)*])*] $($wrapped:tt)* ) => {
        $crate::__private::make_config_impl!(@__field_config_ty_layer
            [$([$($rest)*])*]
            $crate::layers::Cached<$($wrapped)*>
        )
    };

    (@__field_config_ty_layer [[file_read()] $([$($rest:tt)*])*] $($wrapped:tt)* ) => {
        $crate::__private::make_config_impl!(@__field_config_ty_layer
            [$([$($rest)*])*]
            $crate::layers::FileRead<$($wrapped)*>
        )
    };

    (@__field_config_ty_layer [[parsed<$parse_ty:ty>($($_content:tt)*)] $([$($rest:tt)*])*] $($wrapped:tt)* ) => {
        $crate::__private::make_config_impl!(@__field_config_ty_layer
            [$([$($rest)*])*]
            $crate::layers::Parsed<$parse_ty, $($wrapped)*>
        )
    };

    (@__field_config_ty_layer [[parsed_from_str<$parse_ty:ty>()] $([$($rest:tt)*])*] $($wrapped:tt)* ) => {
        $crate::__private::make_config_impl!(@__field_config_ty_layer
            [$([$($rest)*])*]
            $crate::layers::Parsed<$parse_ty, $($wrapped)*>
        )
    };

    (@__field_config_ty_layer [[or_default_val($($_content:tt)*)] $([$($rest:tt)*])*] $($wrapped:tt)* ) => {
        $crate::__private::make_config_impl!(@__field_config_ty_layer
            [$([$($rest)*])*]
            $crate::layers::OrDefault<$($wrapped)*>
        )
    };

    (@__field_config_ty_layer [[or_default()] $([$($rest:tt)*])*] $($wrapped:tt)* ) => {
        $crate::__private::make_config_impl!(@__field_config_ty_layer
            [$([$($rest)*])*]
            $crate::layers::OrDefault<$($wrapped)*>
        )
    };

    // ---------------
    // --------------- field config -> construction
    // ---------------

    (@__field_config_def $Config:ty) => {{
        <$Config as $crate::ConfigDescriptor>::define()
    }};

    (@__field_config_def
        var_name: $var_name:literal
        $(, layers: [$($layers:tt)*])?
        $(, description: $description:literal)?
        $(, default_val_fmt: $default_val_fmt:literal)?
        $(,)?
    ) => {{
        let __config = $crate::layers::TextVar::from_var_name($var_name)
            $(.description($description))?
            $(.default_fmt_val($default_val_fmt))?;
        $crate::__private::make_config_impl!(@__field_config_def_layers __config $($($layers)*)?)
    }};

    (@__field_config_def_layers $binding:ident) => {
        $binding
    };

    // ---------------
    // --------------- layers -> method calls
    // ---------------

    (@__field_config_def_layers $binding:ident
        $head:ident $(<$($head_gen_content:ty)*>)? ($($head_content:tt)*)
        $(, $tail:ident $(<$($tail_gen_content:ty)*>)? ($($tail_content:tt)*))* $(,)?
    ) => {{
        let $binding = $crate::__private::make_config_impl!(@__field_config_def_layer $binding
            $head $(<$($head_gen_content)*>)? ($($head_content)*)
        );
        $crate::__private::make_config_impl!(@__field_config_def_layers $binding
            $($tail $(<$($tail_gen_content)*>)? ($($tail_content)*)),*
        )
    }};

    (@__field_config_def_layer $binding:ident cached()) => {
        $binding.cached()
    };

    (@__field_config_def_layer $binding:ident file_read()) => {
        $binding.file_read()
    };

    (@__field_config_def_layer $binding:ident parsed<$parse_ty:ty>($($parse_content:tt)*)) => {
        $binding.parsed::<$parse_ty>($($parse_content)*)
    };

    (@__field_config_def_layer $binding:ident parsed_from_str<$parse_ty:ty>()) => {
        $binding.parsed_from_str::<$parse_ty>()
    };

    (@__field_config_def_layer $binding:ident or_default_val($($or_default_val_content:tt)*)) => {
        $binding.or_default_val($($or_default_val_content)*)
    };

    (@__field_config_def_layer $binding:ident or_default()) => {
        $binding.or_default()
    };

    // ---------------
    // --------------- field kinds -> iter type
    // ---------------

    (@__field_kind $lt:lifetime [$($head_config:tt)*] $([$($tail_config:tt)*])*) => {
        $crate::__private::iter::Chain<
            $crate::__private::make_config_impl!(@__field_kind_ty $lt $($head_config)*),
            $crate::__private::make_config_impl!(@__field_kind $lt $([$($tail_config)*])*),
        >
    };


    (@__field_kind $lt:lifetime) => {
        $crate::__private::iter::Empty<$crate::exec::ExecResult<$lt>>
    };

    // ---------------
    // --------------- field kind -> iter type
    // ---------------

    (@__field_kind_ty $lt:lifetime $Config:ty) => {
        <<$Config as $crate::exec::ConfigInitializer>::Iter<$lt> as
            $crate::__private::iter::IntoIterator>::IntoIter
    };

    (@__field_kind_ty $lt:lifetime var_name $($_rest:tt)*) => {
        $crate::__private::iter::Once<$crate::exec::ExecResult<$lt>>
    };

    // ---------------
    // --------------- field kind -> iter calls
    // ---------------

    (@__field_kind_calls $self:ident [$head_field:ident: $($head_config:tt)*] $([$($tail:tt)*])*) => {
        $crate::__private::make_config_impl!(@__field_kind_call $self $head_field $($head_config)*)
            .chain($crate::__private::make_config_impl!(@__field_kind_calls $self $([$($tail)*])*))
    };

    (@__field_kind_calls $_self:ident) => {
        $crate::__private::iter::empty::<$crate::exec::ExecResult<'_>>()
    };

    // ---------------
    // --------------- field kind -> iter call
    // ---------------

    (@__field_kind_call $self:ident $field:ident $Config:ty) => {
        <$Config as $crate::exec::ConfigInitializer>::init_raw(&$self.$field)
            .into_iter()
    };

    (@__field_kind_call $self:ident $field:ident var_name $($_rest:tt)*) => {
        $crate::__private::iter::once(
            $crate::exec::ExecResult {
                config: $self.$field.get_descriptor(),
                error: $self.$field.try_get().err().map(From::from),
            }
        )
    };
}

#[doc(hidden)]
pub use make_config_impl;

/// Generates the definition of a configuration value set.
#[macro_export]
macro_rules! make_config {
    {
        $(#[$main_attr:meta])*
        $vis:vis struct $Name:ident {$(
            $(#[$field_attr:meta])*
            $field_vis:vis $field:ident: { $($field_config:tt)* }
        ),* $(,)?}
    } => {
        $(#[$main_attr])*
        $vis struct $Name {$(
            $(#[$field_attr])*
            $field_vis $field: $crate::__private::make_config_impl!(@__field_config_ty $($field_config)*),
        )*}

        const _: () = {
            #[automatically_derived]
            impl $crate::ConfigDescriptor for $Name {
                fn define() -> Self {
                    #[allow(unused_imports)]
                    use $crate::prelude::*;
                    Self {$(
                        $field: $crate::__private::make_config_impl!(@__field_config_def $($field_config)*)
                    ),*}
                }
            }

            #[automatically_derived]
            impl $crate::exec::ConfigInitializer for $Name {
                type Iter<'a> = $crate::__private::make_config_impl!(@__field_kind
                    'a $([$($field_config)*])*
                );

                fn init_raw(&self) -> Self::Iter<'_> {
                    #[allow(unused_imports)]
                    use $crate::prelude::*;

                    $crate::__private::make_config_impl!(
                        @__field_kind_calls self $([ $field: $($field_config)* ])*
                    )
                }
            }
        };
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;

    #[test]
    fn assert_result_iter_coherent_no_flattening() {
        make_config! {
            struct TestConfig {
                var_a: {
                    var_name: "VAR_A",
                },
                var_b: {
                    var_name: "VAR_B",
                },
                var_c: {
                    var_name: "VAR_C",
                    layers: [
                        parsed_from_str<i32>(),
                        or_default(),
                        cached(),
                    ],
                    description: "hello",
                },
            }
        }

        let config = TestConfig::define();
        let res = config.init_raw();

        itertools::assert_equal(
            res.map(|res| res.config.var_name),
            ["VAR_A", "VAR_B", "VAR_C"],
        );
    }

    #[test]
    fn assert_result_iter_coherent_with_flattening() {
        make_config! {
            struct Foo {
                var_a: {
                    var_name: "VAR_A",
                },
                var_b: {
                    var_name: "VAR_B",
                },
            }
        }

        make_config! {
            struct Bar {
                foo: { Foo },
                var_c: {
                    var_name: "VAR_C",
                },
                var_d: {
                    var_name: "VAR_D",
                },
            }
        }

        let config = Bar::define();
        let res = config.init_raw();

        itertools::assert_equal(
            res.map(|res| res.config.var_name),
            ["VAR_A", "VAR_B", "VAR_C", "VAR_D"],
        );
    }

    /// Contains declarations made with the macro, to make sure the code still compiles
    /// with some tweaks.
    #[cfg(debug_assertions)]
    #[allow(unexpected_cfgs, unused)]
    mod __assert_compiles {
        // empty
        make_config! {
            struct Foo0 {}
        }

        // pub struct
        make_config! {
            pub struct Foo1 {}
        }

        // pub field
        make_config! {
            pub struct Foo2 {
                pub bar: { Foo1 },
            }
        }

        // doc comment on struct
        make_config! {
            /// Hello
            struct Foo3 {
            }
        }

        // doc comment on field
        make_config! {
            struct Foo4 {
                /// Hello
                bar: { Foo1 },
            }
        }

        // attribute on struct
        make_config! {
            #[derive(Debug)]
            struct Foo5 {
            }
        }

        // cfg on macro
        #[cfg(not(feature = "foo"))]
        make_config! {
            struct Foo6 {
                bar: { Foo1 },
            }
        }
        // neg cfg on macro
        #[cfg(feature = "foo")]
        make_config! {
            struct Foo6 {
                bar: { Foo2 },
            }
        }

        // only specify var name
        make_config! {
            struct Foo7 {
                foo: {
                    var_name: "HI",
                }
            }
        }

        // layers in whatever order
        make_config! {
            struct Foo8 {
                foo: {
                    var_name: "HI",
                    layers: [
                        file_read(),
                        parsed<std::time::Duration>(|input| todo!()),
                        or_default(),
                        cached(),
                    ],
                }
            }
        }

        // flattened config in whatever field order
        make_config! {
            struct Foo9 {
                foo: { Foo8 },
                bar: {
                    var_name: "HEY",
                },
                foobar: { Foo8 },
            }
        }

        // provide description and default_fmt_val - without layers
        make_config! {
            struct Foo10 {
                foo: {
                    var_name: "HEY",
                    description: "hey",
                    default_val_fmt: "no default :(",
                }
            }
        }

        // provide description and default_fmt_val - with layers
        make_config! {
            struct Foo11 {
                foo: {
                    var_name: "HEY",
                    layers: [cached()],
                    description: "hey",
                    default_val_fmt: "no default :(",
                }
            }
        }

        // provide description alone
        make_config! {
            struct Foo12 {
                foo: {
                    var_name: "HEY",
                    description: "hey",
                }
            }
        }

        // provide default_val_fmt alone
        make_config! {
            struct Foo13 {
                foo: {
                    var_name: "HEY",
                    default_val_fmt: "no default :(",
                }
            }
        }

        // empty layers
        make_config! {
            struct Foo14 {
                foo: {
                    var_name: "HEY",
                    layers: []
                }
            }
        }
    }
}