a simple tool to adapt different configuration file format
- intergrate with toml, yaml, json. ...
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