#[macro_export]
macro_rules! config_group {
($($tokens:tt)*) => {
$crate::__config_group! { @user $($tokens)* }
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __config_group {
(@$mode:ident $(#[$attr:meta])* $vis:vis group $module:ident = named $name:literal, closed
{ $($body:tt)* }) => {
$crate::__config_group! { @module $mode named closed $name
[$(#[$attr])*] $vis $module { $($body)* } }
};
(@$mode:ident $(#[$attr:meta])* $vis:vis group $module:ident = named $name:literal
{ $($body:tt)* }) => {
$crate::__config_group! { @module $mode named open $name
[$(#[$attr])*] $vis $module { $($body)* } }
};
(@$mode:ident $(#[$attr:meta])* $vis:vis group $module:ident = $name:literal, closed
{ $($body:tt)* }) => {
$crate::__config_group! { @module $mode single closed $name
[$(#[$attr])*] $vis $module { $($body)* } }
};
(@$mode:ident $(#[$attr:meta])* $vis:vis group $module:ident = $name:literal
{ $($body:tt)* }) => {
$crate::__config_group! { @module $mode single open $name
[$(#[$attr])*] $vis $module { $($body)* } }
};
(@module $mode:ident $kind:ident $closed:ident $name:literal
[$(#[$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] [] $($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;
};
(@lines [builtin named $closed:ident $name:literal] [$($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]
[$($keys)* ($key_type $key [])] $($rest)* }
};
(@lines [builtin named $closed:ident $name:literal] [$($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]
[$($keys)* ($key_type $key [])] $($rest)* }
};
(@lines [builtin $kind:ident $closed:ident $name:literal] [$($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] [$($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]
[$($keys)* ($key_type $key [$($flag)*])] $($rest)* }
};
(@lines [$mode:ident $kind:ident $closed:ident $name:literal] [$($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]
[$($keys)* ($key_type $key [$($flag)*])] $($rest)* }
};
(@lines [user $kind:ident $closed:ident $name:literal]
[$(($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))*]);
}
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]
[$(($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))*]);
}
pub(crate) fn check_file(
keyfile: &::ostrya_core::KeyFile,
header: &str,
) -> $crate::Result<()> {
$crate::config::refuse_unknown_keys::<Group>(keyfile, header)?;
$($crate::config::read_file::<Group, $key_type>(keyfile, header)?;)*
::core::result::Result::Ok(())
}
pub(crate) fn text_in_effect(
keyfile: &::ostrya_core::KeyFile,
header: &str,
overrides: ::core::option::Option<&$crate::config::Overrides>,
key: &str,
) -> ::core::option::Option<
$crate::Result<::core::option::Option<::std::string::String>>,
> {
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)+ }
};
(@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];`"
));
};
(@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 };
(@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"
));
};
(@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) => {};
}