Crates.io | penguin-config |
lib.rs | penguin-config |
version | 0.1.1 |
source | src |
created_at | 2021-12-22 16:47:27.99506 |
updated_at | 2021-12-22 16:59:23.482366 |
description | Read json files as Rust structs with a simple derive macro. |
homepage | |
repository | https://github.com/Henrik-N/penguin-config |
max_upload_size | |
id | 501777 |
size | 7,511 |
Penguin-config simplifies the creation of config files using serde and serde_json.
[dependencies]
serde = "1.0"
penguin-config = "0.1.1"
{ "width": 640, "height": 400 }
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();
}
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);
}
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);
}
}
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
}
}
Licensed under MIT. Do whatever you want with it :)