#toml-config #derive #macro-derive #turn #struct

macro toml_config_derive

Rust macro to turn a Rust struct into a TOML config

5 releases

new 0.1.4 Feb 20, 2025
0.1.3 Feb 20, 2025
0.1.2 Feb 20, 2025
0.1.1 Feb 20, 2025
0.1.0 Feb 19, 2025

#344 in Procedural macros

Download history

108 downloads per month
Used in toml_config_trait

MIT/Apache

5KB

toml_config_derive

Basic rust derive macro and trait to turn a struct into a readable and writable TOML file.

crates.io Docs

Installation & Usage

cargo add toml_config_derive

use toml_config_derive::TomlConfig;
use toml_config_derive::TomlConfigTrait;

Both the trait and the macro must be in scope to use the provided methods.

Structs using TomlConfig MUST also implement Default, Serialize, and Deserialize.

Example Usage

use toml_config_derive::{TomlConfig, TomlConfigTrait};
use serde::{Serialize, Deserialze};

#[derive(TomlConfig, Serialize, Deserialize, Default)]
TestStruct {
	first: String,
	second: usize,
}

fn main() {
	let test_struct = TestStruct::default();
	test_struct.write_to_path("test_config.toml".into()).unwrap();
	let test_struct_read = TestStruct::read_from_path("test_config.toml".into()).unwrap();
	assert_eq!(test_struct, test_struct_read);
}

Features

config_map is used to convert a TOML config to a flat map that can be used for a Kubernetes config-map.

Example

cargo add toml_config_derive -F config_map

use toml_config_derive::{TomlConfig, TomlConfigTrait};
use serde::{Serialize, Deserialze};

#[derive(TomlConfig, Serialize, Deserialize, Default)]
TestStruct {
	first: String,
	second: usize,
}

#[tokio::main]
async fn main() {
	let test_struct = TestStruct::read_from_path("test_config.toml".into()).unwrap();
	test_struct.update_config_map("test_config_map", "test_namespace").await.unwrap();
}

Under the hood this uses

	let config = kube::Config::infer().await?;
	let client = kube::Client::try_from(config)?;
	let maps: kube::Api<k8s_openapi::api::core::v1::ConfigMap> = kube::Api::namespaced(client, namespace);

k8s_openapi is pinned to v1_32.

Note: this is pretty niche, but necessary for my use case. I'm open to ideas to make this more adaptable if there's a need for it.

Dependencies

~0.3–0.9MB
~20K SLoC