#config-toml #toml #arceos #config #proc-macro #config-file

macro axconfig-gen-macros

Procedural macros for converting TOML format configurations to Rust constant definitions

1 unstable release

0.1.0 Dec 28, 2024

#496 in Procedural macros

Download history 170/week @ 2024-12-24 305/week @ 2024-12-31 341/week @ 2025-01-07

816 downloads per month

GPL-3.0-or-later OR Apache-2…

54KB
1K SLoC

axconfig-gen-macros

Procedural macros for converting TOML format configurations to equivalent Rust constant definitions.

Example

axconfig_gen_macros::parse_configs!(r#"
are-you-ok = true
one-two-three = 123

[hello]
"one-two-three" = "456"     # int
array = [1, 2, 3]           # [uint]
tuple = [1, "abc", 3]
"#);

assert_eq!(ARE_YOU_OK, true);
assert_eq!(ONE_TWO_THREE, 123usize);
assert_eq!(hello::ONE_TWO_THREE, 456isize);
assert_eq!(hello::ARRAY, [1, 2, 3]);
assert_eq!(hello::TUPLE, (1, "abc", 3));

Value types are necessary for generating Rust constant definitions. Types can be specified by the comment following the config item. Currently supported types are bool, int, uint, str, (type1, type2, ...) for tuples, and [type] for arrays. If no type is specified, it will try to infer the type from the value.

The above example will generate the following constants:

pub const ARE_YOU_OK: bool = true;
pub const ONE_TWO_THREE: usize = 123;

pub mod hello {
    pub const ARRAY: &[usize] = &[1, 2, 3];
    pub const ONE_TWO_THREE: isize = 456;
    pub const TUPLE: (usize, &str, usize) = (1, "abc", 3);
}

You can also include the configuration file directly:

axconfig_gen_macros::include_configs!("/path/to/config.toml");

Dependencies

~2.2–3MB
~55K SLoC