opzioni

Crates.ioopzioni
lib.rsopzioni
version3.0.1
sourcesrc
created_at2023-05-21 20:07:30.190291
updated_at2024-02-05 21:49:55.135782
descriptionA slim and fast configuration library for Rust
homepage
repositoryhttps://github.com/auribuo/opzioni
max_upload_size
id870128
size28,752
Aurelio Buonomo (auribuo)

documentation

README

⚙️ opzioni

A simple and fast configuration library for Rust.

Installation

Add the library to your project via cargo:

cargo add opzioni

Features

By default all features are enabled. This allows to work with JSON, TOML and YAML configs.

If you want to only use a subset run:

cargo add opzioni --no-default-features --features json

Replace json with the features you want to enable. The available features are

  • json
  • yaml
  • toml

You can also enable logs via the tracing crate using the tracing feature. This feature is disabled by default

Usage

First create a struct implementing Serialize, Deserialize and Default

use serde::{Serialize, Deserialize}

#[derive(Serialize, Deserialize, Default)]
struct MyConfig {
    name: String,
    age: u8
}

Then just load the config file using opzioni:

let config = opzioni::Config::<MyConfig>::configure().load(std::path::Path::new("myconfig.yml")).unwrap();

opzioni exposes a RwLock which can be used to modify the config data:

let lock = config.get();
let data = lock.read().unwrap();
// or
let mut data = lock.write().unwrap();

Once you are done working with the config you can save the changes to disk by calling save:

config.save().unwrap();
Commit count: 8

cargo fmt