Crates.io | figure |
lib.rs | figure |
version | 0.1.1 |
source | src |
created_at | 2021-07-19 06:07:30.554581 |
updated_at | 2024-01-02 07:46:22.438301 |
description | Library for runtime configuration management |
homepage | https://github.com/vmalloc/figure |
repository | https://github.com/vmalloc/figure |
max_upload_size | |
id | 424621 |
size | 27,416 |
Note: figure is still in very early stages of development. Use at your own risk
figure
is a crate for managing configuration for Rust applications. It puts emphasis on ergonomics in realistic use cases:
Clone
, and centrally controlled - this allows you to pass it around your app to whichever components need it without worrying too muchfigure
Configuration objects are wrappers around an inner configuration value. By default this value is a serde_json::Value
, but this can be customized to fit any type that is Serialize
and Deserialize
.
Configuration objects maintain the "raw" state of the configuration they hold - i.e. the serde_json representation of the value before building it. Manipulating this data is done by set_raw
and get_raw
:
use figure::Config;
let cfg = Config::empty();
cfg.set_raw("x", 2u32).unwrap();
let value: u32 = cfg.get_raw("x").unwrap();
assert_eq!(value, 2);
More useful cases involve creating your own types for holding the "built" configuration values. In this case it makes more sense to get the "built" configuration value via get()
, and modify it via modify()
:
use figure::Config;
#[derive(serde::Deserialize, serde::Serialize, Default)]
struct MyConfig {
value: u32,
}
let cfg = Config::<MyConfig>::new_with_default().unwrap();
let value = cfg.get().value;
assert_eq!(value, 0);
cfg.set_raw("value", 2u32);
let value = cfg.get().value;
assert_eq!(value, 2);