toml_const
TOML compile-time constants
Getting started
use ;
// workspace root
// ├── example.toml
// ├── normalize.toml
// ├── toml_const <---- you are here
// │ ├── Cargo.toml
// │ └── src
// └── toml_const_macros
// ├── Cargo.toml
// └── src
// include a TOML file in your project relative to your manifest directory
toml_const!
// include a file relative to your workspace root
toml_const_ws!
// table keys are capitalized struct fields
const TITLE: &str = EXAMPLE_TOML.title;
assert_eq!;
Table substitution
File substitution is supported. The first path that exists and satisfies the following conditions will be used. These conditions are, in order of precedence:
- if a substitute path has the
usekeyword prefixed - iif a toml file contains
use = trueat the root level
Multiple substitute files can be specified in the macro expression.
The first file containing a use = true key will be merged into the parent file.
These files may contain secrets or other sensitive information that you don't want to check into version control.
use toml_const;
toml_const!
Normalization
A TOML file is normalized before it is generated as code. This step does not modify the original config file.
Tables within arrays will have their keys propagated across all elements. Missing keys will be filled with default values. This means that keys can be omitted from parts of your config as long as it is defined in at least one element.
Empty arrays will be inferred to be &'static [&'static str].
# this table will normalize to ...
[]
= "my_library"
= [
{ = "0.1.0", = "Initial release" },
{ = "0.2.0" }, # description is omitted
{ = "0.3.0", = "Added support for arrays of tables", = [
{ = "1", = "Fixed a bug with arrays of tables" },
{ = "2", = "support nested arrays" },
] },
]
# ... this
[]
= "my_library"
= [
{ = "0.1.0", = "Initial release", = [] },
{ = "0.2.0", = "", = [] },
{ = "0.3.0", = "Added support for arrays of tables", = [
{ = "1", = "Fixed a bug with arrays of tables" },
{ = "2", = "support nested arrays" },
] },
]
Hashmaps
A table that contains identical keys will implement a const map() method that returns &phf::OrderedMap.
This feature is included by default under the feature flag "phf". You can opt to disable it by adding default-features = false to this dependency.
use toml_const;
toml_const!
// keys can be accessed through struct fields as usual
let first_value = NORMALIZE_TOML.identical_values.first;
let second_value = NORMALIZE_TOML.identical_values.second;
let map = NORMALIZE_TOML.identical_values.map;
for in map.into_iter
Unwrapping datetime
toml::Datetime contains fields that point to Options, which need const/runtime checks.
As the toml spec defines 4 datetime formats,
non-option types can be used to unwrap datetime values at compile time.
Datetime values are also normalized to support multiple formats defined for one key. The union of all formats will be used to generate the final datetime format.
Attributes
Docstrings and derive attributes are supported.
Clone, Copy, and Debug are automatically derived for all types.
use toml_const;
toml_const!
Limitations
This library does not support the full TOML specification.
It will fail to:
- generate arrays with distinct types (arrays containing different types, arrays of tables with conflicting key types)
- create a struct from a table with a blank key
"" = true - parse reserved keys (
__map__is reserved cannot be used as a key)
It will modify:
- table keys that begin with numbers
- table keys that contain invalid characters for identifiers
TOML data types
All TOML data types are supported. Datetime related structs are re-exported from toml.
| data type | rust type |
|---|---|
| boolean | bool |
| integer | i64 |
| float | f64 |
| string | &'static str |
| date | toml_const::Datetime |
| array | &'static [T] |
| table | auto-generated struct |