config

Attribute Macro config 

Source
#[config]
Expand description

Declares a config module.

#[config]
<VIS> mod <IDENT> {
    <FORMAT>!(<SRC>);
    <FORMAT>!(<SRC>);
    <FORMAT>!(<SRC>);
}

When there are multiple sources, they got merged recursively per field, with latter ones overwriting former ones. All null values need to be overwritten eventually.

Every <SRC> shall be a literal string, or a macro invocation expanding into a literal string. The full support of eager expansion is impossible without nightly-only feature proc_macro_expand. A subset of eager expansion for built-in macros is handled by macro_string crate, which identifies both the following as valid sources:

  • r#"name = "Tom""#
  • include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/examples/example_config.toml"))

After expansion, a type Type and a static variable EXPR holding config data will become available inside the module, i.e.

<VIS> mod <IDENT> {
    pub struct Type;
    pub static EXPR: Type = Type;
    // Other internal implementation details...
}

Users now can freely use <IDENT>::Type and <IDENT>::EXPR. Optionally, for convenience, you can export these items directly into the scope via export arguments:

#[config(export(type = <TYPE_IDENT>, const = <CONST_IDENT>, static = <STATIC_IDENT>))]

Each will yield a corresponding item:

ArgumentGenerated item
type = <TYPE_IDENT>pub type <TYPE_IDENT> = <IDENT>::Type;
const = <CONST_IDENT>pub const <CONST_IDENT>: <IDENT>::Type = <IDENT>::EXPR;
static = <STATIC_IDENT>pub static <STATIC_IDENT>: <IDENT>::Type = <IDENT>::EXPR;