Crates.io | struct-to-config |
lib.rs | struct-to-config |
version | 0.1.6 |
source | src |
created_at | 2024-10-02 06:15:28.247268 |
updated_at | 2024-10-11 06:52:59.089015 |
description | Add clap (or serde::Serializable) structs to config when adding sources to config builder. |
homepage | https://www.pickupleague.com/ |
repository | https://github.com/morphytron/clap-config |
max_upload_size | |
id | 1393752 |
size | 28,739 |
I ran into a problem with adding clap structs into a config file, easily, of which I figured I could use Serde to serialize and add it manually that way. This was tedious to do this for each use case, so I designed this crate to be simple to use.
Fails on compile time if any attribute of a struct (doesn't necessarily have to be a Clap arg struct) does not convert into a key-value pair given the current narrow implementation.
Example
use config::Config;
use config::ConfigBuilder;
use serde::Serialize;
use clap::Parser;
#[derive(Serialize)]
#[clap(
author = "Daniel Alexander Apatiga",
version = "1.0.0",
about = "Make service.")]
pub struct Make {
/// Name of person
#[clap(short, long)]
name: String,
good: Option<String>
}
pub fn test1() {
let make = Make {
name: "Joe".to_string(),
good: None
};
let mut cfg : Config = make_cfg!(make);
assert_eq!(cfg.get_string("name").unwrap(), "Joe".to_string());
let settings_builder = Config::builder()
.add_source(config::File::with_name("path_to_file/blah.cfg"))
.add_source(cfg);
cfg = settings_builder.build().unwrap();
assert_eq!(cfg.get_string("joe").ok(), None);
}