Skip to content

Latest commit

 

History

28 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

configfs

Build Status crates.io Documentation License

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.

Installation

Add configfs and serde to your Cargo.toml

[dependencies]
configfs = "0.1.2"
serde = { version = "1", features = ["derive"] }

Usage

Reading Config

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 config = Config::new(ConfigDirectory::Custom("./appconf".into()))?;
    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 config = Config::new(ConfigDirectory::Custom("./appconf".into()))?;
    let settings = AppSettings {
        username: "jimmy".into()
    };

    config.write(&settings)?;

    Ok(())
}

Features

system-dirs

Enables support to access the users local config directory, this is fetched using the dirs crate.

[dependencies]
configfs = { version = "0.1", features = ["system-dirs"] }

An example that will save the config folder app in ~/.config/.

let config = Config::new(ConfigDirectory::System("app"))?;

watcher

Enables support to watch files as they reload.

An example of this to see file reloading can be seen in examples/watcher.rs.

License

Licensed under either of:

  • MIT License
  • Apache License, Version 2.0

About

Small filesystem configuration manager for rust applications

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages