Skip to main content

Crate configfs

Crate configfs 

Source
Expand description

§configfs

A small lightweight filesystem config manager configfs provides an api to load, deserialize and write config files for your application. This currently only supports the TOML format, however I shall introduce more formats in the future.

§Usage

use configfs::{Config, ConfigDirectory};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct AppSettings {
    port: u16,
    verbose: bool
}
impl Default for AppSettings {
    fn default() -> Self {
        Self {
            port: 9000,
            verbose: true
        }
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let dir = tempfile::tempdir()?;
    let config = Config::new(ConfigDirectory::Custom(dir.path().to_path_buf()))?;
    let settings = config.read_or_default::<AppSettings>()?;

    if settings.verbose {
        println!("using port: {}", settings.port);
    }

    Ok(())
}

§Writing Config

use configfs::{Config, ConfigDirectory};
use serde::Serialize;

#[derive(Serialize)]
struct AppSettings {
    username: String
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let dir = tempfile::tempdir()?;
    let config = Config::new(ConfigDirectory::Custom(dir.path().to_path_buf()))?;
    let settings = AppSettings {
        username: "jimmy".into()
    };

    config.write(&settings)?;

    Ok(())
}

Structs§

Config
SharedConfig

Enums§

ConfigDirectory
Target directory of the configuration files
ConfigError

Traits§

Deserialize
A data structure that can be deserialized from any data format supported by Serde.
DeserializeOwned
A data structure that can be deserialized without borrowing any data from the deserializer.
Serialize
A data structure that can be serialized into any data format supported by Serde.

Derive Macros§

Deserialize
Serialize