xcfg-rs

Crates.ioxcfg-rs
lib.rsxcfg-rs
version0.3.1
sourcesrc
created_at2024-02-27 13:29:02.967511
updated_at2024-10-25 15:22:32.833561
descriptionA simple configuration file parser for Rust
homepage
repositoryhttps://github.com/kumudiaorong/xcfg-rs.git
max_upload_size
id1155104
size31,254
kmdr (km0e)

documentation

README

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
Commit count: 33

cargo fmt