Trait lsio::config::ConfigFile [] [src]

pub trait ConfigFile: Sized {
    type Error: Error + From<Error>;
    fn from_toml(toml: Value) -> Result<Self, Self::Error>;

    fn from_file<T: AsRef<Path>>(filepath: T) -> Result<Self, Self::Error> { ... }
}

Defines the default ConfigFile operation of from_file and from_toml

from_toml should be implemented in the calling project for a Config struct that you define. For example:

[derive(Clone, Debug, PartialEq, Eq)]

pub struct Config { pub endpoint: Option, pub proxy: Option, }

impl ConfigFile for Config { type Error = Error;

fn from_toml(toml: toml::Value) -> Result<Self> {
    let mut cfg = Config::default();

    let endpoint = match toml.lookup("options.endpoint") {
        Some(ep) => Some(ep.as_str().unwrap().to_string()),
        None => None,
    };

    let proxy = match toml.lookup("options.proxy") {
        Some(p) => Some(p.as_str().unwrap().to_string()),
        None => None,
    };

    cfg.endpoint = endpoint;
    cfg.proxy = proxy;

    Ok(cfg)
}Run

}

impl Default for Config { fn default() -> Self { Config { endpoint: None, proxy: None, } } }

Associated Types

Required Methods

Provided Methods

Implementors