toml_const
TOML compile-time constants
Getting started
Add these in your Cargo.toml file:
= "build.rs"
[[]]
= "toml_const_gen"
= "src/toml_const_gen.rs"
[]
= "0.8"
= "1.4"
= { = "https://github.com/cruzerngz/toml_const.git" }
[]
= { = "https://github.com/cruzerngz/toml_const.git" }
# crate not published on crates.io yet!
# toml_const = "0.1"
Create a new binary file src/toml_const_gen.rs with the following code:
use ExitCode;
use cli;
Run the binary. MANIFEST_PATH is the path to your Cargo.toml package manifest:
# run "cargo run... -- init --help" to see more arguments
The binary will create and configure some files.
Create a build script, build.rs, in the same directory as Cargo.toml:
// build.rs code
Your package should now look like this:
The generated file generated.rs can now be included into your code:
include! // include in main
It is recommended include this file in a separate module:
// inside main.rs / lib.rs
// inside consts.rs
include!
And then use it:
let this = USE; // the USE const must always be defined
Usage
All valid TOML datatypes are generated as compile-time constants, except for arrays and tables.
Arrays and tables are defined inside a lazy_static! wrapper.
All tables are destructured, when possible, to keys that point to their TOML values. Namespaces are separated with an underscore "_".
For example, this:
[]
= "string"
= 11:20:00
= 3
[]
= "another string"
= 1979-05-27 07:32:00Z
= 1e09
Turns to this:
// ...imports excluded
/// type: i64
pub const TABLE_INTEGER: i64 = ;
/// type: f64
pub const TABLE_INNER_ONE_BILLION: f64 = ;
/// type: &'static str
pub const TABLE_INNER_FIELD: &'static str = "another string";
/// type: Datetime
pub const TABLE_TIME: Datetime = Datetime ;
/// type: &'static str
pub const TABLE_FIELD: &'static str = "string";
/// type: Datetime
pub const TABLE_INNER_DATETIME: Datetime = Datetime ;
Toml key table.time is is converted to TABLE_TIME.
A nested table value table.inner.one_billion is destructured to TABLE_INNER_ONE_BILLION.
Additionally, last-level tables, or tables that do not contain inner tables or arrays, also have their key-value pairs stored as a HashMap<&'static str, String>:
// `table` contains the `inner` table
// so it is not a last-level table.
//
// HashMaps and arrays are wrapped in lazy_static!{...}
/// type: HashMap<&'static str, String>
pub static ref TABLE_INNER: = from;
This might be useful when iterating over unknown key-value pairs during runtime. You do lose type information, though.
Template, debug, deploy
toml_const generates 3 toml files into your root project directory.
The contents from *.template.toml is used as a base, matching keys from
*.debug.toml or *.deploy.toml will override the template values.
Setting the top-level key use=true will cause toml_const
to generate code from that particular config file.
| debug use | deploy use | file(s) used |
|---|---|---|
false |
false |
template: compilation warning |
false |
true |
template + deploy |
true |
false |
template + debug |
true |
true |
template + deploy |