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].

[build-dependencies.config_struct]
version = "~0.2.0"
features = ["toml-parsing"]

By default, config_struct is markup-language-agnostic, so include the relevant feature for whatever language your config file is written in. Choices are:

  1. json-parsing
  2. ron-parsing
  3. toml-parsing
  4. yaml-parsing

Only toml-parsing is included by default, so be sure to specify the features you need in your Cargo.toml file.

Examples

This example is not tested
// build.rs
extern crate config_struct;

fn main() {
    config_struct::create_config("config.toml", "src/config.rs", &Default::default());
}

The above build script will take the following config.toml file and generate a config.rs like the following:

// config.toml
name = "Application"
version = 5
features = [
    "one",
    "two",
    "three"
]
This example is not tested
// 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.

Note: By default, config structs derive Serialize and Deserialize. This will only work in your application if you include the serde_derive and serde crates. You can disable deriving those traits in Options if you wish.

Similarly, the loading functions generated by default will require the same serialization crate used in generation. For example, extern crate toml will be required if you generate using "config.toml". These loading functions can also be disabled however.

Structs

Options

Options for configuring the generation of a config struct.

Enums

DynamicLoading

When to perform dynamic loading from the config file itself.

Error

An error type for errors while generating config struct modules.

FloatSize

Represents a floating-point type.

Format

Represents an input markup format for a config file.

GenerationError

An error occurring during code generation.

IntSize

Represents an integer type.

OptionsError

An error type for when an Options value failed validation.

Functions

create_config

Generate a Rust module containing struct definitions based on a given config file.

create_config_from_source

Generate a Rust module containing struct definitions from a config string in some specified format.

create_config_with_format

Generate a Rust module containing struct definitions based on a given config file with an explicitly specified format.

generate_config

Generate Rust source code defining structs based on a config file.

generate_config_from_source

Generate Rust source code defining structs from a config string in some specified format.

generate_config_with_format

Generate Rust source code defining structs based on a config file of an explicit format.