Crate config_struct [−] [src]
This crate is a library for generating structs based on a config file at build time.
It is intended for use in a build.rs
file so should be included in your
[build-dependencies]
.
The core library is agnostic to the markup language of the config, but there are a few features included to make it easier to use with a few markup languages:
-
json-parsing
-
ron-parsing
-
toml-parsing
-
yaml-parsing
None of these are included by default, so be sure to include one in your Cargo.toml
.
Examples
// build.rs extern crate config_struct; use config_struct::toml_parsing; fn main() { let toml_config = toml_parsing::parse_config_from_file("config.toml").unwrap(); config_struct::write_config_module( "src/config.rs", &toml_config, &Default::default()).unwrap(); }
The above build script will take the following config.toml
file and generate config.rs
:
// config.toml
name = "Application"
version = 5
features = [
"one",
"two",
"three"
]
// config.rs use std::borrow::Cow; #[derive(Debug, Clone, Serialize, Deserialize)] #[allow(non_camel_case_types)] pub struct Config { pub features: Cow<'static, [Cow<'static, str>]>, pub name: Cow<'static, str>, pub version: i64, } pub const CONFIG: Config = Config { features: Cow::Borrowed(&[Cow::Borrowed("one"), Cow::Borrowed("two"), Cow::Borrowed("three")]), name: Cow::Borrowed("Application"), version: 5, };
Strings and arrays are represented by Cow
types, which allows the entire Config struct to
be either heap allocated at runtime, or a compile time constant, as shown above.
Modules
json_parsing |
Parsing utilities for JSON config files. (Requires the |
ron_parsing |
Parsing utilities for RON config files. (Requires the |
toml_parsing |
Parsing utilities for TOML config files. (Requires the |
yaml_parsing |
Parsing utilities for YAML config files. (Requires the |
Structs
Options |
The set of options for generating a config struct. |
RawStructValue |
Represents a Rust struct. |
Enums
RawValue |
Represents a typed Rust value. |
StructGenerationError |
This type represents possible errors when generating a config struct. |
Functions
create_config_module |
Generate Rust code for a RawStructValue. |
write_config_module |
Generate Rust code for a RawStructValue and write it to a file. |