axconfig-gen-macros

Crates.ioaxconfig-gen-macros
lib.rsaxconfig-gen-macros
version0.1.0
created_at2024-12-28 14:22:28.26852+00
updated_at2024-12-28 14:22:28.26852+00
descriptionProcedural macros for converting TOML format configurations to Rust constant definitions.
homepagehttps://github.com/arceos-org/arceos
repositoryhttps://github.com/arceos-org/axconfig-gen
max_upload_size
id1497531
size8,514
core (github:arceos-org:core)

documentation

https://docs.rs/axconfig-gen-macros

README

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");
Commit count: 13

cargo fmt