11 releases
Uses new Rust 2024
| 0.3.4 | Aug 8, 2025 |
|---|---|
| 0.3.3 | Jan 19, 2025 |
| 0.3.1 | Oct 25, 2024 |
| 0.2.4 | Apr 20, 2024 |
| 0.1.0 | Feb 27, 2024 |
#713 in Encoding
564 downloads per month
18KB
343 lines
xcfg_rs is a simple configuration file loader and saver.
Example
use serde::{Deserialize, Serialize};
use xcfg::XCfg;
#[derive(XCfg, Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Test {
a: i32,
b: Vec<i32>,
sub: SubTest,
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct SubTest {
c: Vec<String>,
}
let test = Test {
a: 1,
b: vec![0, 1, 2],
sub: SubTest {
c: vec!["ab".to_string(), "cd".to_string()],
},
};
let path = "./test.toml";
test.save(path).unwrap();
assert_eq!(Test::load(path).unwrap().into_inner(), test);
std::fs::remove_file(path).unwrap();
config-rs
a simple tool to adapt different configuration file format
plan
- intergrate with toml, yaml, json. ...
usage
First, we need to add serde and xcfg to our Cargo.toml:
cargo add serde -F derive
cargo add xcfg -F full
Then, we can use XCfg to load configuration from different file formats:
use serde::{Deserialize, Serialize};
use xcfg::XCfg;
#[derive(Debug, Serialize, Deserialize, XCfg)]
struct Config {
name: String,
age: u32,
}
fn main() {
let config = Config::load("config")
.expect("Failed to load config.[toml|yaml|yml|json]")
.into_inner();
println!("{:?}", config);
}
This example is also available in the example directory. You can clone this repo and run the example:
cd example && cargo r --example full --features full
Dependencies
~0.6–1.8MB
~39K SLoC