[][src]Trait configurable::Configurable

pub trait Configurable: Default + Serialize + DeserializeOwned {
const QUALIFIER: &'static str;
const ORGANIZATION: &'static str;
const APPLICATION: &'static str;
const NAME: &'static str;

    fn ensure_dir() -> Result<PathBuf, Error>;

    fn load_or_default() -> Result<LoadState<Self>, Error> { ... }
fn load() -> Result<Self, Error> { ... }
fn save(&self) -> Result<(), Error> { ... }
fn dump(&self, out: impl Write) -> Result<(), Error> { ... }
fn dir() -> Result<PathBuf, Error> { ... }
fn path() -> Result<PathBuf, Error> { ... } }

Trait to provide easier loaded/saving of a config type

Provide static strs for QUALIFIER, ORGANIZATION, APPLICATION and NAME

Will which will produce $CONFIG_PATH/qualifier.organization.application/name

Configuration-style configs (e.g. stuff that should be human editable)

use serde::{Serialize, Deserialize};
use std::path::PathBuf;
use configurable::{Config, Data, Configurable, Error};

// Default is required
#[derive(Default, Serialize, Deserialize)]
struct MyConfig;

// For configurations (e.g. foo.toml)
impl Config for MyConfig {};
impl Configurable for MyConfig {
    const ORGANIZATION: &'static str = "museun";
    const APPLICATION: &'static str = "foobar";
    const NAME: &'static str = "config.toml";

    fn ensure_dir() -> Result<PathBuf, Error> {
        // Config `configs`
        <Self as Config>::ensure_dir()
    }
}
// will place it here:
// -> "~/.config/com.github/museun/foobar/config.toml

Data-style configurations (e.g. formats outside of toml)

use serde::{Serialize, Deserialize};
use std::path::PathBuf;
use configurable::{Config, Data, Configurable, Error};

// Default is required
#[derive(Default, Serialize, Deserialize)]
struct MyMap { map: std::collections::HashMap<String,i32> }

// For configurations (e.g. foo.toml)
impl Data for MyMap {};
impl Configurable for MyMap {
    const ORGANIZATION: &'static str = "museun";
    const APPLICATION: &'static str = "foobar";
    const NAME: &'static str = "mapping.json";

    fn ensure_dir() -> Result<PathBuf, Error> {
        // Data `configs`
        <Self as Data>::ensure_dir()
    }
}
// will place it here:
// -> "~/.local/share/com.github/museun/foobar/mapping.json

Associated Constants

const QUALIFIER: &'static str

Qualifier (e.g. "com.github")

Defaults to com.github

const ORGANIZATION: &'static str

Organization (e.g. "museun" (in github.com/museun))

You must provide this

const APPLICATION: &'static str

Application (e.g. "foo" (in github.com/museun/foo))

You must provide this

const NAME: &'static str

The name of the toml file, with extension

ex: config.toml

Loading content...

Required methods

fn ensure_dir() -> Result<PathBuf, Error>

Ensures the directory exists

Implement either Config or Data then delegate to it

// Config or Data
impl Config for Foo {};
impl Configurable for Foo {
    const ORGANIZATION: &'static str = "some_org";
    const APPLICATION: &'static str = "foobar";
    const NAME: &'static str = "config.toml";

    fn ensure_dir() -> Result<PathBuf, Error> {
        // Config or Data
        <Self as Config>::ensure_dir()
    }
}
Loading content...

Provided methods

fn load_or_default() -> Result<LoadState<Self>, Error>

Loads, or defaults the configuration

Returns a LoadState

  • Default meant it created a default instance
  • Loaded meant it created the instance from the file

fn load() -> Result<Self, Error>

Tries to load the configuration

fn save(&self) -> Result<(), Error>

Tries to save the configuration

fn dump(&self, out: impl Write) -> Result<(), Error>

Tries to dump the config to the writer

fn dir() -> Result<PathBuf, Error>

Ensures the directory exists and returns a PathBuf to it

fn path() -> Result<PathBuf, Error>

Ensures the directory exists and returns a PathBuf to the configuration file inside of the directory

Loading content...

Implementors

Loading content...