ostrya-core 0.19.0

ostree object model, checksums, on-disk format primitives, and typed config
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
485
486
487
488
489
490
491
492
493
//! The macro that defines a group of `config` and its keys.

/// Defines one group of `config` and a type for each key of the group.
///
/// # Grammar
///
/// ```text
/// config_group! {
///     #[attributes of the module]
///     VISIBILITY group MODULE = [named] "NAME" [, closed] {
///         /// The doc comment of the key type.
///         KeyType: TYPE = "key-name" [, required | , default EXPR] [, overridable] [, fixed];
///         ...
///     }
/// }
/// ```
///
/// One call defines one group. The call writes the module `MODULE` with the
/// marker type `Group` of the group and one unit struct for each key. Each
/// key type implements [`Key<Group>`](crate::config::Key).
///
/// The module takes each attribute before `group`. A key line takes doc
/// comments alone. Another attribute on a key line, for example
/// `#[cfg(...)]` or `#[allow(...)]`, is a compile error.
///
/// The words:
///
/// - `named`: the group is a named group, `[NAME "SECTION"]`. Without the
///   word, the group is a single group, `[NAME]`.
/// - `closed`: [`Group::CLOSED`](crate::config::Group::CLOSED) is `true`.
///   The port refuses an unknown key of a closed group of its own. A
///   library user checks the key list of its own group with
///   [`Section::refuse_unknown_keys`](crate::config::Section::refuse_unknown_keys),
///   [`ConfigView::refuse_unknown_keys`](crate::config::ConfigView::refuse_unknown_keys),
///   or `RepoConfig::refuse_unknown_keys`.
/// - `required`: the key has no default, and a read of an absent key fails
///   with
///   [`ConfigError::InvalidFormat`](crate::config::ConfigError::InvalidFormat).
/// - `default EXPR`: the built-in default of the key. `EXPR` is a value of
///   `TYPE`.
/// - `overridable`: the key implements
///   [`Overridable`](crate::config::Overridable), and
///   [`Group::OVERRIDABLE`](crate::config::Group::OVERRIDABLE) lists its
///   name.
/// - `fixed`: the key does not implement
///   [`Settable`](crate::config::Settable), and
///   [`Group::SETTABLE`](crate::config::Group::SETTABLE) does not list its
///   name.
///
/// The words come in the order of the grammar. A key with `required` or
/// `default` has the value type `TYPE`, and a read gives `TYPE`. Each other
/// key is written with the type `Option<V>`. Its value type is `V`, and a
/// read gives `Option<V>`. The macro reads the written form `Option<...>`.
///
/// Each key of a single group implements
/// [`SingleKey`](crate::config::SingleKey), and also
/// [`Settable`](crate::config::Settable) unless it has the word `fixed`. A
/// key of a named group takes no `overridable` and no `fixed`.
///
/// The value type implements [`Value`](crate::config::Value). Inside the
/// module, a glob import of the parent module makes the names of the
/// enclosing module available. A type that a function body defines is not
/// an item of the enclosing module, so the module of the group cannot name
/// it. A key type hides a value type of the same name, so write such a
/// value type as a path. The group name follows the rules of
/// [`UserGroup`](crate::config::UserGroup).
///
/// The macro reads one key line for each step of its recursion. A group of
/// about 100 keys or more needs a higher `#![recursion_limit]` in the crate
/// of the caller.
///
/// # Examples
///
/// ```
/// use ostrya_core::config::{Group, Key, Overridable, Settable};
///
/// ostrya_core::config_group! {
///     /// The keys of the `debles` tool.
///     pub group debles = "ex-debles" {
///         /// The target of a build.
///         Target: Option<String> = "target";
///         /// The number of parallel jobs.
///         Parallel: u32 = "parallel", default 4, overridable;
///     }
/// }
///
/// ostrya_core::config_group! {
///     /// The images of the `debles` tool.
///     pub group image = named "ex-debles image", closed {
///         /// The architecture of an image.
///         Arch: String = "arch", required;
///     }
/// }
///
/// assert_eq!(<debles::Group as Group>::NAME, "ex-debles");
/// assert_eq!(<debles::Group as Group>::KEYS, ["target", "parallel"]);
/// assert_eq!(<debles::Group as Group>::OVERRIDABLE, ["parallel"]);
/// assert_eq!(<debles::Group as Group>::SETTABLE, ["target", "parallel"]);
/// assert_eq!(<debles::Parallel as Key<debles::Group>>::default_value(), Some(4));
/// assert!(<image::Group as Group>::NAMED);
/// assert!(<image::Group as Group>::CLOSED);
/// assert!(<image::Group as Group>::SETTABLE.is_empty());
///
/// fn overridable<K: Overridable>() {}
/// fn settable<K: Settable>() {}
/// overridable::<debles::Parallel>();
/// settable::<debles::Target>();
/// ```
///
/// A key line with a doc comment compiles:
///
/// ```
/// ostrya_core::config_group! {
///     pub group tool = "ex-tool" {
///         /// The number of parallel jobs.
///         Jobs: u32 = "jobs", default 4;
///     }
/// }
/// ```
///
/// A key line with another attribute does not compile:
///
/// ```compile_fail
/// ostrya_core::config_group! {
///     pub group tool = "ex-tool" {
///         #[allow(dead_code)]
///         Jobs: u32 = "jobs", default 4;
///     }
/// }
/// ```
///
/// ```compile_fail
/// ostrya_core::config_group! {
///     pub group tool = "ex-tool" {
///         /// The number of parallel jobs.
///         #[cfg(any())]
///         Jobs: u32 = "jobs", default 4;
///     }
/// }
/// ```
#[macro_export]
macro_rules! config_group {
    ($($tokens:tt)*) => {
        $crate::__config_group! { @user [] $($tokens)* }
    };
}

/// The rules of [`config_group!`].
///
/// The macro is not a stable API.
#[doc(hidden)]
#[macro_export]
macro_rules! __config_group {
    // ---- the group -------------------------------------------------------
    // The bracket after the mode holds the visibility of `check_file` and
    // `text_in_effect` of a built-in group. It is empty for a group of a
    // library user.
    (@$mode:ident $fvis:tt $(#[$attr:meta])* $vis:vis group $module:ident = named $name:literal,
        closed { $($body:tt)* }) => {
        $crate::__config_group! { @module $mode named closed $name $fvis
            [$(#[$attr])*] $vis $module { $($body)* } }
    };
    (@$mode:ident $fvis:tt $(#[$attr:meta])* $vis:vis group $module:ident = named $name:literal
        { $($body:tt)* }) => {
        $crate::__config_group! { @module $mode named open $name $fvis
            [$(#[$attr])*] $vis $module { $($body)* } }
    };
    (@$mode:ident $fvis:tt $(#[$attr:meta])* $vis:vis group $module:ident = $name:literal, closed
        { $($body:tt)* }) => {
        $crate::__config_group! { @module $mode single closed $name $fvis
            [$(#[$attr])*] $vis $module { $($body)* } }
    };
    (@$mode:ident $fvis:tt $(#[$attr:meta])* $vis:vis group $module:ident = $name:literal
        { $($body:tt)* }) => {
        $crate::__config_group! { @module $mode single open $name $fvis
            [$(#[$attr])*] $vis $module { $($body)* } }
    };

    (@module $mode:ident $kind:ident $closed:ident $name:literal $fvis:tt
        [$(#[$attr:meta])*] $vis:vis $module:ident { $($body:tt)* }) => {
        $(#[$attr])*
        $vis mod $module {
            #[allow(unused_imports)]
            use super::*;

            $crate::__config_group! { @marker $kind $name }

            $crate::__config_group! { @lines [$mode $kind $closed $name $fvis] [] $($body)* }
        }
    };

    (@marker single $name:literal) => {
        #[doc = concat!("The marker type of the group `[", $name, "]`.")]
        #[derive(Debug, Clone, Copy)]
        pub struct Group;
    };
    (@marker named $name:literal) => {
        #[doc = concat!("The marker type of the group `[", $name, " \"NAME\"]`.")]
        #[derive(Debug, Clone, Copy)]
        pub struct Group;
    };

    // ---- the key lines ---------------------------------------------------
    // Each entry of the key list is `(KeyType "key-name" [WORDS])`.
    //
    // A built-in named group also takes the lines
    // `use MODULE::KeyType: Option<TYPE> = "key-name";` and
    // `use MODULE::KeyType: TYPE = "key-name", default EXPR;`. Such a line
    // re-exports the key type of the group `MODULE` and implements `Key` of
    // this group for it, with its own default. The line takes no doc
    // comment: the key type keeps the doc of its own group.
    (@lines [builtin named $closed:ident $name:literal $fvis:tt] [$($keys:tt)*]
        use $source:ident :: $key_type:ident : Option<$value:ty> = $key:literal ;
        $($rest:tt)*) => {
        pub use super::$source::$key_type;
        $crate::__config_group! { @key_impl $key_type [$value] $key opt [none] }
        $crate::__config_group! { @lines [builtin named $closed $name $fvis]
            [$($keys)* ($key_type $key [])] $($rest)* }
    };
    (@lines [builtin named $closed:ident $name:literal $fvis:tt] [$($keys:tt)*]
        use $source:ident :: $key_type:ident : $value:ty = $key:literal, default $default:expr ;
        $($rest:tt)*) => {
        pub use super::$source::$key_type;
        $crate::__config_group! { @key_impl $key_type [$value] $key val [default $default] }
        $crate::__config_group! { @lines [builtin named $closed $name $fvis]
            [$($keys)* ($key_type $key [])] $($rest)* }
    };
    (@lines [builtin $kind:ident $closed:ident $name:literal $fvis:tt] [$($keys:tt)*]
        use $($rest:tt)*) => {
        ::core::compile_error!(concat!(
            "a use line has the form `use MODULE::KeyType: Option<TYPE> = \"key-name\";` ",
            "or `use MODULE::KeyType: TYPE = \"key-name\", default EXPR;`, and a named group ",
            "alone takes it"
        ));
    };
    (@lines [$mode:ident $kind:ident $closed:ident $name:literal $fvis:tt] [$($keys:tt)*]
        $(#[doc = $kdoc:expr])* $key_type:ident : Option<$value:ty> = $key:literal
        $(, $flag:ident $($flag_expr:expr)?)* ; $($rest:tt)*) => {
        $crate::__config_group! { @flags [$kind $name [$(#[doc = $kdoc])*] $key_type [$value] $key opt]
            [$($flag $($flag_expr)?),*] }
        $crate::__config_group! { @lines [$mode $kind $closed $name $fvis]
            [$($keys)* ($key_type $key [$($flag)*])] $($rest)* }
    };
    (@lines [$mode:ident $kind:ident $closed:ident $name:literal $fvis:tt] [$($keys:tt)*]
        $(#[doc = $kdoc:expr])* $key_type:ident : $value:ty = $key:literal
        $(, $flag:ident $($flag_expr:expr)?)* ; $($rest:tt)*) => {
        $crate::__config_group! { @flags [$kind $name [$(#[doc = $kdoc])*] $key_type [$value] $key val]
            [$($flag $($flag_expr)?),*] }
        $crate::__config_group! { @lines [$mode $kind $closed $name $fvis]
            [$($keys)* ($key_type $key [$($flag)*])] $($rest)* }
    };
    (@lines [user $kind:ident $closed:ident $name:literal $fvis:tt]
        [$(($key_type:ident $key:literal $flags:tt))*]) => {
        impl $crate::config::UserGroup for Group {
            const NAME: &'static str = $name;
            const NAMED: bool = $crate::__config_group!(@bool $kind);
            const CLOSED: bool = $crate::__config_group!(@bool $closed);
            const KEYS: &'static [&'static str] = &[$($key,)*];
            const OVERRIDABLE: &'static [&'static str] =
                $crate::__config_group!(@list $kind overridable [$(($key $flags))*]);
            const SETTABLE: &'static [&'static str] =
                $crate::__config_group!(@list $kind settable [$(($key $flags))*]);
        }
        // A free const item is evaluated where it is defined, so the checks
        // of the name and the key lists run also for a group that no code
        // uses.
        const _: &str = <Group as $crate::config::Group>::NAME;
        const _: &[&str] = <Group as $crate::config::Group>::KEYS;
        const _: &[&str] = <Group as $crate::config::Group>::OVERRIDABLE;
        const _: &[&str] = <Group as $crate::config::Group>::SETTABLE;
    };
    (@lines [builtin $kind:ident $closed:ident $name:literal [$($fvis:tt)*]]
        [$(($key_type:ident $key:literal $flags:tt))*]) => {
        impl $crate::config::sealed::Sealed for Group {}
        impl $crate::config::Group for Group {
            const NAME: &'static str = $name;
            const NAMED: bool = $crate::__config_group!(@bool $kind);
            const CLOSED: bool = $crate::__config_group!(@bool $closed);
            const KEYS: &'static [&'static str] = &[$($key,)*];
            const OVERRIDABLE: &'static [&'static str] =
                $crate::__config_group!(@list $kind overridable [$(($key $flags))*]);
            const SETTABLE: &'static [&'static str] =
                $crate::__config_group!(@list $kind settable [$(($key $flags))*]);
        }
        /// Checks the group `header` of `keyfile` and returns the first
        /// error. A closed group first refuses a key that it does not list.
        /// The call then parses the value of each key of the group that the
        /// file sets, in the order of the definition.
        $($fvis)* fn check_file(
            keyfile: &$crate::KeyFile,
            header: &str,
        ) -> ::core::result::Result<(), $crate::config::ConfigError> {
            $crate::config::refuse_unknown_keys::<Group>(keyfile, header)?;
            $($crate::config::read_file::<Group, $key_type>(keyfile, header)?;)*
            ::core::result::Result::Ok(())
        }
        /// Returns the value in effect of the key `key` of the group in the
        /// group `header` of `keyfile`, with the overrides `overrides`, as the
        /// text of `Value::format`. The inner `None` is a key with no value.
        /// The outer `None` is a key that the group does not list.
        $($fvis)* fn text_in_effect(
            keyfile: &$crate::KeyFile,
            header: &str,
            overrides: ::core::option::Option<&$crate::config::Overrides>,
            key: &str,
        ) -> ::core::option::Option<
            ::core::result::Result<
                ::core::option::Option<::std::string::String>,
                $crate::config::ConfigError,
            >,
        > {
            match key {
                $($key => ::core::option::Option::Some(
                    $crate::config::lookup_text::<Group, $key_type>(keyfile, header, overrides),
                ),)*
                _ => ::core::option::Option::None,
            }
        }
    };
    (@lines $ctx:tt [$($keys:tt)*] $($rest:tt)+) => {
        $crate::__config_group! { @bad_line $($rest)+ }
    };

    // ---- the error of a key line -----------------------------------------
    (@bad_line #[doc = $kdoc:expr] $($rest:tt)*) => {
        $crate::__config_group! { @bad_line $($rest)* }
    };
    (@bad_line # $attr:tt $($rest:tt)*) => {
        ::core::compile_error!("a key line takes doc comments alone, and no other attribute");
    };
    (@bad_line $($rest:tt)*) => {
        ::core::compile_error!(concat!(
            "a key line has the form `KeyType: TYPE = \"key-name\" [, required | , default EXPR] ",
            "[, overridable] [, fixed];`"
        ));
    };

    // ---- the lists of the marker keys ------------------------------------
    // A named group has no override and no create value. The list of a
    // single group holds each key name whose words select it. The checks of
    // the words run in `@flags`.
    (@list named $which:ident $keys:tt) => { &[] };
    (@list single $which:ident [$(($key:literal [$($flag:ident)*]))*]) => {{
        const LEN: usize = 0 $(+ $crate::__config_group!(@if $which [$($flag)*] { 1 } { 0 }))*;
        #[allow(unused_mut, unused_variables, unused_assignments)]
        const LIST: [&str; LEN] = {
            let mut list = [""; LEN];
            let mut len = 0;
            $($crate::__config_group! { @if $which [$($flag)*]
                { list[len] = $key; len += 1; } {} })*
            list
        };
        &LIST
    }};
    (@if overridable [overridable $($rest:ident)*] { $($yes:tt)* } $no:tt) => { $($yes)* };
    (@if overridable [$first:ident overridable $($rest:ident)*] { $($yes:tt)* } $no:tt) => {
        $($yes)*
    };
    (@if overridable [$($flag:ident)*] $yes:tt { $($no:tt)* }) => { $($no)* };
    (@if settable [fixed] $yes:tt { $($no:tt)* }) => { $($no)* };
    (@if settable [$first:ident fixed] $yes:tt { $($no:tt)* }) => { $($no)* };
    (@if settable [$first:ident $second:ident fixed] $yes:tt { $($no:tt)* }) => { $($no)* };
    (@if settable [$($flag:ident)*] { $($yes:tt)* } $no:tt) => { $($yes)* };

    (@bool named) => { true };
    (@bool single) => { false };
    (@bool closed) => { true };
    (@bool open) => { false };

    // ---- the words of a key ----------------------------------------------
    (@flags $key:tt [required $(, $($rest:tt)*)?]) => {
        $crate::__config_group! { @flags_marker $key [required] [$($($rest)*)?] }
    };
    (@flags $key:tt [default $default:expr $(, $($rest:tt)*)?]) => {
        $crate::__config_group! { @flags_marker $key [default $default] [$($($rest)*)?] }
    };
    (@flags $key:tt [$($rest:tt)*]) => {
        $crate::__config_group! { @flags_marker $key [none] [$($rest)*] }
    };
    (@flags_marker $key:tt $default:tt [overridable $(, $($rest:tt)*)?]) => {
        $crate::__config_group! { @flags_fixed $key $default yes [$($($rest)*)?] }
    };
    (@flags_marker $key:tt $default:tt [$($rest:tt)*]) => {
        $crate::__config_group! { @flags_fixed $key $default no [$($rest)*] }
    };
    (@flags_fixed $key:tt $default:tt $overridable:ident [fixed]) => {
        $crate::__config_group! { @key $key $default $overridable yes }
    };
    (@flags_fixed $key:tt $default:tt $overridable:ident []) => {
        $crate::__config_group! { @key $key $default $overridable no }
    };
    (@flags_fixed $key:tt $default:tt $overridable:ident [$($rest:tt)*]) => {
        ::core::compile_error!(concat!(
            "the words of a key are `required` or `default EXPR`, then `overridable`, ",
            "then `fixed`, in this order"
        ));
    };

    // ---- the key type ----------------------------------------------------
    (@key [$kind:ident $name:literal [$(#[doc = $kdoc:expr])*] $key_type:ident [$value:ty] $key:literal
        $form:ident] $default:tt $overridable:ident $fixed:ident) => {
        $crate::__config_group! { @key_struct $kind $name [$(#[doc = $kdoc])*] $key_type $key }
        $crate::__config_group! { @key_impl $key_type [$value] $key $form $default }
        $crate::__config_group! { @key_markers $kind $key_type $overridable $fixed }
    };

    (@key_struct single $name:literal [$(#[doc = $kdoc:expr])*] $key_type:ident $key:literal) => {
        #[doc = concat!("The key `", $key, "` of `[", $name, "]`.")]
        #[doc = ""]
        $(#[doc = $kdoc])*
        #[derive(Debug, Clone, Copy)]
        pub struct $key_type;
    };
    (@key_struct named $name:literal [$(#[doc = $kdoc:expr])*] $key_type:ident $key:literal) => {
        #[doc = concat!("The key `", $key, "` of `[", $name, " \"NAME\"]`.")]
        #[doc = ""]
        $(#[doc = $kdoc])*
        #[derive(Debug, Clone, Copy)]
        pub struct $key_type;
    };

    (@key_impl $key_type:ident [$value:ty] $key:literal opt [none]) => {
        impl $crate::config::Key<Group> for $key_type {
            const NAME: &'static str = $key;
            type Value = $value;
            type Output = ::core::option::Option<$value>;
            fn default_value() -> ::core::option::Option<$value> {
                ::core::option::Option::None
            }
            fn output(
                value: ::core::option::Option<$value>,
            ) -> ::core::option::Option<::core::option::Option<$value>> {
                ::core::option::Option::Some(value)
            }
        }
    };
    (@key_impl $key_type:ident [$value:ty] $key:literal val [required]) => {
        impl $crate::config::Key<Group> for $key_type {
            const NAME: &'static str = $key;
            type Value = $value;
            type Output = $value;
            fn default_value() -> ::core::option::Option<$value> {
                ::core::option::Option::None
            }
            fn output(value: ::core::option::Option<$value>) -> ::core::option::Option<$value> {
                value
            }
        }
    };
    (@key_impl $key_type:ident [$value:ty] $key:literal val [default $default:expr]) => {
        impl $crate::config::Key<Group> for $key_type {
            const NAME: &'static str = $key;
            type Value = $value;
            type Output = $value;
            fn default_value() -> ::core::option::Option<$value> {
                ::core::option::Option::Some($default)
            }
            fn output(value: ::core::option::Option<$value>) -> ::core::option::Option<$value> {
                value
            }
        }
    };
    (@key_impl $key_type:ident [$value:ty] $key:literal opt $default:tt) => {
        ::core::compile_error!(concat!(
            "the key `", $key, "` of type `Option<...>` takes no `required` and no `default`"
        ));
    };
    (@key_impl $key_type:ident [$value:ty] $key:literal val [none]) => {
        ::core::compile_error!(concat!(
            "the key `", $key, "` takes `required` or `default`, or has the type `Option<...>`"
        ));
    };

    (@key_markers single $key_type:ident $overridable:ident $fixed:ident) => {
        impl $crate::config::SingleKey for $key_type {
            type Group = Group;
        }
        $crate::__config_group! { @settable $key_type $fixed }
        $crate::__config_group! { @overridable $key_type $overridable }
    };
    (@key_markers named $key_type:ident no no) => {};
    (@key_markers named $key_type:ident $overridable:ident $fixed:ident) => {
        ::core::compile_error!("a key of a named group takes no `overridable` and no `fixed`");
    };
    (@settable $key_type:ident no) => {
        impl $crate::config::Settable for $key_type {}
    };
    (@settable $key_type:ident yes) => {};
    (@overridable $key_type:ident yes) => {
        impl $crate::config::Overridable for $key_type {}
    };
    (@overridable $key_type:ident no) => {};
}