ConfigFactory

Struct ConfigFactory 

Source
pub struct ConfigFactory;
Expand description

Implements helper functions for loading TOML files into a structure

§Examples

To load a file into a Config struct, use ConfigFactory.

Either rustc_serialize or serde can be used for serialization.

## Example using rustc_serialize

extern crate rustc_serialize;
extern crate toml_config;

use rustc_serialize::{Encodable, Decodable};
use std::path::Path;
use toml_config::ConfigFactory;

#[derive(RustcEncodable, RustcDecodable)]
struct Config  {
    nested: NestedConfig
}

// Defaults will be used for missing/invalid configurations in the TOML config file
impl Default for Config {
    fn default() -> Config {
        Config {
            nested: NestedConfig::default()
        }
    }
}

#[derive(RustcEncodable, RustcDecodable)]
struct NestedConfig  {
    value: String,
    values: Vec<u16>
}

impl Default for NestedConfig {
    fn default() -> NestedConfig {
        NestedConfig {
            value: "default".to_owned(),
            values: vec![0, 0, 0]
        }
    }
}

/* config.toml:
 * [nested]
 * value = "test"
 * values = [1, 2, 3]
 */

let config: Config = ConfigFactory::load(Path::new("config.toml"));
assert_eq!(config.nested.value, "test");
assert_eq!(config.nested.values, vec![1, 2, 3]);

§Example using serde

extern crate serde;
extern crate toml_config;

use serde::{Serialize, Deserialize};
use std::path::Path;
use toml_config::ConfigFactory;

#[derive(Serialize, Deserialize)]
struct Config  {
    nested: NestedConfig
}

// Defaults will be used for missing/invalid configurations in the TOML config file
impl Default for Config {
    fn default() -> Config {
        Config {
            nested: NestedConfig::default()
        }
    }
}

#[derive(Serialize, Deserialize)]
struct NestedConfig  {
    value: String,
    values: Vec<u16>
}

impl Default for NestedConfig {
    fn default() -> NestedConfig {
        NestedConfig {
            value: "default".to_owned(),
            values: vec![0, 0, 0]
        }
    }
}

/* config.toml:
 * [nested]
 * value = "test"
 * values = [1, 2, 3]
 */

let config: Config = ConfigFactory::load(Path::new("config.toml"));
assert_eq!(config.nested.value, "test");
assert_eq!(config.nested.values, vec![1, 2, 3]);

Implementations§

Source§

impl ConfigFactory

Source

pub fn load<T>(path: &Path) -> T

Loads a TOML file and decodes it into a target structure, using default values for missing or invalid file configurations

Source

pub fn decode<T>(toml_table: Table) -> T

Decodes a TOML table into a target structure, using default values for missing or invalid configurations

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.