amethyst_config 0.3.0

Loading from .yml/.yaml files into Rust structures with defaults to prevent hard errors.
Documentation

Loads configuration files (.yaml/.yml/.toml) into a structure for easy/statically typed usage.

Basic usage:

#[macro_use]
extern crate amethyst_config;

use amethyst_config::Config;

config! {
    struct ExampleConfig {
        pub amount: i32 = 50,
    }
}

fn main() {
    let config = ExampleConfig::default();
    assert_eq!(config.amount, 50);
}

Config is the name of the rust struct that will be generated by the macro. It can be anything as long as it would be a valid struct in its context, e.g. no other structs by the same name.

The inner fields of Config can be summed up as:

name: type = default,

The field name will be looked up when attempting to load from a .yml/.yaml file. If it is found, then the value will be converted from the formats type to a Rust type and assigned to the field.

If a field is missing from the config file or has a value of a wrong type, the Rust field will fall back to the default value.

In addition to basic types, any struct created through the config! macro will automatically implement Serialize and Deserialize from the serde library, meaning you can nest configuration structs inside each other.

# #[macro_use] extern crate amethyst_config;
# use amethyst_config::Config;

config! {
    struct NestedConfig {
        pub some_field: [i64; 3] = [1, 2, 3],
    }
}

config! {
    struct ExampleConfig {
        pub nested: NestedConfig = NestedConfig::default(),
    }
}
# fn main() { }

Documentation and commenting

Normally when constructing a config, you might add a small description as to what the fields will be used for and possibly their default values, if they have any.

# #[macro_use]
# extern crate amethyst_config;
# use amethyst_config::Config;
config! {
    /// This is an example configuration!
    struct ExampleConfig {
        /// Width and height of the window on initialization. Defaults to
        /// 1024x768.
        pub dimensions: [u16; 2] = [1024, 768],
    }
}
# fn main() { }

If the macro has problems expanding, then you may want to check whether you have the documentation on the line before the field and that you have the pub identifier before the field name.