#config-file #file-format #file-extension #format-file #configuration #deserialize #serialization

cfgfifo

(De)serialize common configuration file formats based on file extension

4 releases

new 0.2.2 Feb 10, 2025
0.2.1 Jan 14, 2025
0.2.0 Dec 22, 2023
0.1.0 Oct 30, 2023

#659 in Encoding

Download history 2/week @ 2024-10-26 10/week @ 2024-11-02 1/week @ 2024-11-09 7/week @ 2024-11-16 12/week @ 2024-11-23 7/week @ 2024-11-30 6/week @ 2024-12-07 7/week @ 2024-12-14 9/week @ 2024-12-28 20/week @ 2025-01-04 134/week @ 2025-01-11 28/week @ 2025-01-18 11/week @ 2025-01-25 55/week @ 2025-02-01 96/week @ 2025-02-08

196 downloads per month
Used in labelmaker

MIT license

64KB
892 lines

Project Status: Active – The project has reached a stable, usable state and is being actively developed. CI Status codecov.io Minimum Supported Rust Version MIT License

GitHub | crates.io | Documentation | Issues | Changelog

cfgfifo is a Rust library for serializing & deserializing various common configuration file formats (JSON, JSON5, RON, TOML, and YAML), including autodetecting the format of a file based on its file extension. It's good for application authors who want to support multiple configuration file formats but don't want to write out a bunch of boilerplate. cfgfifo has already written that boilerplate for you, so let it (de)serialize your files!

Example

use serde::Deserialize;

#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
struct AppConfig {
    #[serde(default)]
    enable_foo: bool,
    #[serde(default)]
    bar_type: BarType,
    #[serde(default)]
    flavor: Option<String>,
}

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
enum BarType {
    #[default]
    Open,
    Closed,
    Clopen,
}

fn main() -> anyhow::Result<()> {
    let Some(cfgpath) = std::env::args().nth(1) else {
        anyhow::bail!("No configuration file specified");
    };
    // cfgfifo identifies the format used by the file `cfgpath` based on its
    // file extension and deserializes it appropriately:
    let cfg: AppConfig = cfgfifo::load(cfgpath)?;
    println!("You specified the following configuration:");
    println!("{cfg:#?}");
    Ok(())
}

Dependencies

~3–4.5MB
~91K SLoC