penguin-config

Crates.iopenguin-config
lib.rspenguin-config
version0.1.1
sourcesrc
created_at2021-12-22 16:47:27.99506
updated_at2021-12-22 16:59:23.482366
descriptionRead json files as Rust structs with a simple derive macro.
homepage
repositoryhttps://github.com/Henrik-N/penguin-config
max_upload_size
id501777
size7,511
Henrik Nilsson (Henrik-N)

documentation

README

Penguin Config

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

Dependencies

Cargo.toml

[dependencies]
serde = "1.0"
penguin-config = "0.1.1"

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 :)

Commit count: 29

cargo fmt