Crates.io | xcfg-rs |
lib.rs | xcfg-rs |
version | 0.3.1 |
source | src |
created_at | 2024-02-27 13:29:02.967511 |
updated_at | 2024-10-25 15:22:32.833561 |
description | A simple configuration file parser for Rust |
homepage | |
repository | https://github.com/kumudiaorong/xcfg-rs.git |
max_upload_size | |
id | 1155104 |
size | 31,254 |
a simple tool to adapt different configuration file format
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