| Crates.io | konfig-rust |
| lib.rs | konfig-rust |
| version | 0.1.1 |
| created_at | 2025-02-27 23:39:29.959584+00 |
| updated_at | 2025-02-28 13:24:37.960015+00 |
| description | A library providing a simple way to cnteralized config management in your codebase |
| homepage | |
| repository | https://github.com/kociumba/konfig-rust |
| max_upload_size | |
| id | 1572291 |
| size | 29,299 |
A simple, configuration management library for Rust. konfig-rust lets you focus on app logic and design instead of config management.
This library is a Rust implementation of my konfig-go library.
cargo add konfig-rust
If you want to use the #[derive(KonfigSection)] macro, you also need the konfig-rust-derive crate
cargo add konfig-rust-derive
This is a very simple example, more complex ones are planned as examples in the github repo.
use serde::{Deserialize, Serialize};
use konfig_rust::*;
use konfig_rust::format::*;
use konfig_rust_derive::KonfigSection;
#[derive(Serialize, Deserialize, KonfigSection)] // Aside from KonfigSection, you also have to use the Serialize and Deserialize macros
struct Config {
name: String,
age: u32,
}
fn main() {
let mut c = Config { name: "Bob".to_string(), age: 32 };
let mut manager = KonfigManager::new(KonfigOptions {
format: Format::JSON,
auto_save: false,
use_callbacks: true,
config_path: "config.json".to_string(),
});
manager.register_section(&mut c).unwrap();
manager.load().unwrap();
println!("Name: {}, Age: {}", c.name, c.age); // Notice how you just access the struct like normal in memory state storage
c.age = c.age + 1;
manager.save().unwrap();
}
The KonfigOptions struct provides several options to customize the behavior of the configuration manager:
/// Options for the KonfigManager
pub struct KonfigOptions {
/// Format of the configuration file
pub format: Format,
/// Path to the configuration file
pub config_path: String,
/// Currently noop due to lifetime issues, meant to register callbacks for panic, SIGINT and SIGTERM to save the data
pub auto_save: bool,
/// Whether to use callbacks (on_load, validate)
pub use_callbacks: bool,
}
Define your configuration struct and derive KonfigSection, Serialize, and Deserialize:
use serde::{Deserialize, Serialize};
use konfig_rust_derive::KonfigSection;
#[derive(Serialize, Deserialize, KonfigSection)]
#[section_name = "my_section"] // Optional: Define section name, defaults to struct name in snake_case
struct MyConfig {
setting1: String,
setting2: i32,
timeout_ms: u64,
}
if you want to provide custom validate and on_load callbacks, you just have to implement
the KonfigSection trait manually instead of using the KonfigSection derive macro
konfig-rust supports three configuration file formats, mirroring the Go version:
Format::JSON):
{
"my_section": {
"setting1": "value",
"setting2": 42,
"timeout_ms": 30000
}
}
Format::YAML):
my_section:
setting1: value
setting2: 42
timeout_ms: 30000
Format::TOML):
[my_section]
setting1 = "value"
setting2 = 42
timeout_ms = 30000
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License
Created by @kociumba