Crates.io | configgen-rs |
lib.rs | configgen-rs |
version | 0.1.3 |
source | src |
created_at | 2023-07-22 14:09:01.06584 |
updated_at | 2023-08-12 15:23:16.296895 |
description | An attempt to make a crate based on config that generates a default configuration file on the filesystem if it does not exist yet |
homepage | |
repository | https://github.com/Quessou/configgen-rs |
max_upload_size | |
id | 923212 |
size | 49,966 |
An attempt to make a small crate that generates a default configuration file on the filesystem if it does not exist yet.
Kinda thought as a very minimalistic but complementary crate for something like config
The crate exposes a few methods that allow to create safely a directory and a configuration file.
The one creating the configuration file takes an object implementing serde::Serializable
, that will be serialized into the created file.
For now, only a few formats are handled :
use config::Config;
use configgen_rs;
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, str::FromStr};
#[derive(Serialize, Deserialize)]
struct DummyConfig {
pub field1: i32,
}
fn main() {
let written_config = DummyConfig { field1: 2 };
let path = PathBuf::from_str("/tmp/test_config").unwrap();
configgen_rs::initialization::initialize_config_file(
&written_config,
&path,
configgen_rs::SerializationFormat::Toml,
)
.expect("Writing failed");
// From here, testing that the config crate can read the written file.
let f = config::File::new(path.to_str().unwrap(), config::FileFormat::Toml);
let config = Config::builder()
.add_source(config::File::from(f))
.build()
.unwrap();
let read_config = config.try_deserialize::<DummyConfig>().unwrap();
assert_eq!(read_config.field1, written_config.field1);
}
Do not hesitate to suggest improvements or report bugs on Github !