#toml #cfg #constant #configuring #values #across #procedural

toml-cfg

用于在crate间配置常量值的程序宏

5个版本

0.2.0 2024年2月21日
0.1.3 2022年1月11日
0.1.2 2022年1月11日
0.1.1 2022年1月11日
0.1.0 2022年1月11日

过程宏 中排名第 654

Download history 1558/week @ 2024-03-13 1436/week @ 2024-03-20 878/week @ 2024-03-27 876/week @ 2024-04-03 989/week @ 2024-04-10 960/week @ 2024-04-17 906/week @ 2024-04-24 994/week @ 2024-05-01 953/week @ 2024-05-08 787/week @ 2024-05-15 1726/week @ 2024-05-22 2027/week @ 2024-05-29 1768/week @ 2024-06-05 1406/week @ 2024-06-12 1580/week @ 2024-06-19 2103/week @ 2024-06-26

每月下载量 7,129
3 个crate 中使用

MIT/Apache

11KB
134

toml-cfg

简要概述

  • Crates可以声明可被覆盖的变量
    • 任何const类型,例如 usize、字符串等
  • (仅)"根crate"可以通过包含一个 cfg.toml 文件来覆盖这些变量

配置文件

# a toml-cfg file

[lib-one]
buffer_size = 4096

[lib-two]
greeting = "Guten tag!"

在库中查看

// lib-one
#[toml_cfg::toml_config]
pub struct Config {
    #[default(32)]
    buffer_size: usize,
}

// lib-two
#[toml_cfg::toml_config]
pub struct Config {
    #[default("hello")]
    greeting: &'static str,
}

看看我们得到了什么!

# Print the "buffer_size" value from the `lib-one` crate.
# Since it has no cfg.toml, we just get the default value.
$ cd pkg-example/lib-one
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/lib-one`
32

# Print the "greeting" value from the `lib-two` crate.
# Since it has no cfg.toml, we just get the default value.
$ cd ../lib-two
$ cargo run
   Compiling lib-two v0.1.0 (/home/james/personal/toml-cfg/pkg-example/lib-two)
    Finished dev [unoptimized + debuginfo] target(s) in 0.32s
     Running `target/debug/lib-two`
hello

# Print the "buffer_size" value from `lib-one`, and "greeting"
# from `lib-two`. Since we HAVE defined a `cfg.toml` file, the
# values defined there are used instead.
$ cd ../application
$ cargo run
   Compiling lib-two v0.1.0 (/home/james/personal/toml-cfg/pkg-example/lib-two)
   Compiling application v0.1.0 (/home/james/personal/toml-cfg/pkg-example/application)
    Finished dev [unoptimized + debuginfo] target(s) in 0.30s
     Running `target/debug/application`
4096
Guten tag!

依赖项

~0.6–1.2MB
~27K SLoC