2 releases

Uses new Rust 2021

0.1.1 Dec 22, 2021
0.1.0 Dec 22, 2021

#1484 in Procedural macros

46 downloads per month
Used in 2 crates (via penguin-config)

MIT license

3KB

Penguin Config

Penguin-config simplifies the creation of config files using serde and serde_json.

Dependencies

Cargo.toml

[dependencies]
serde = { version = "1.0", features = ["serde_derive"] }
serde_json = { version = "1.0" }
penguin-config = { git = "https://github.com/Henrik-N/penguin-config" }

Usage

window_config.json
{ "width": 640, "height": 400 }

Using derive macros

Generating a config file
use penguin_config::*;

// the PenguinConfigGenerate macro will use the std::ops::Default implementation of the struct
#[derive(Serialize, PenguinConfigGenerate, Default)]
#[penguin_config(path = "window_config.json")]
struct WindowConfig {
    width: u32,
    height: u32,
}

fn generate_config() {
    WindowConfig::generate_penguin_config_file();
}
Reading a config file
use penguin_config::*;

#[derive(Deserialize, PenguinConfigFile)]
#[penguin_config(path = "window_config.json")]
struct WindowConfig {
    width: u32,
    height: u32,
}

fn read_config() {
    let config = WindowConfig::read_config();

    assert_eq!(config.width, 640);
    assert_eq!(config.height, 400);
}

Using traits

Generating a config file
use penguin_config::*;

#[derive(Serialize)]
struct WindowConfig {
    width: u32,
    height: u32,
}
impl PenguinConfigGenerate for WindowConfig {
    fn generate_penguin_config_file() {
        let config = WindowConfig { width: 640, height: 400 };
        Serializer::file_path("window_config.json").serialize(&config);
    }
}
Reading a config file
use penguin_config::*;

#[derive(Deserialize)]
struct WindowConfig {
    width: u32,
    height: u32,
}
impl PenguinConfig for WindowConfig {
    fn read_config() -> Self {
        let config: Self = Deserializer::file_path("window_config.json").deserialize();
        config
    }
}

License

Licensed under MIT. Do whatever you want with it :)

Dependencies

~0.5–1MB
~22K SLoC