| Crates.io | toml_config_trait |
| lib.rs | toml_config_trait |
| version | 0.1.5 |
| created_at | 2025-02-19 21:58:43.596849+00 |
| updated_at | 2025-02-26 05:55:27.080759+00 |
| description | Rust trait to turn a Rust struct into a TOML config. |
| homepage | |
| repository | https://github.com/simp4t7/toml_config |
| max_upload_size | |
| id | 1561941 |
| size | 45,817 |
Basic rust derive macro and trait to turn a struct into a readable and writable TOML file.
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.
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);
}
config_map is used to convert a TOML config to a flat map that can be used for a Kubernetes config-map.
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.