SergioBenitez / Figment

A hierarchical configuration library so con-free, it's unreal.
Apache License 2.0
604 stars 35 forks source link

Write back to config file. #85

Closed uwejan closed 10 months ago

uwejan commented 10 months ago

been using configrs for multiple project, and now with a new project writing back to the config file is required. configs.toml Was wondering if this crate is able of doing so. Thank you.

SergioBenitez commented 10 months ago

Yes. Simply serialize your config structure, or the entire Figment itself, to your desired format and write it out.

uwejan commented 10 months ago

@SergioBenitez , Basically std::fs::write ..etc, after dealing with string struct/serialization. I thought the crate might have a way say state.set / state.override ('path', value).

SergioBenitez commented 10 months ago

It's really rather straightforward:

let toml_string = toml::to_string_pretty(&figment)?;
std::fs::write("path.toml", &toml_string)?;

I thought the crate might have a way say state.set / state.override ('path', value).

What is state? If you serialize/deserialize into a typed structure, as encouraged by Figment, then you just need foo.path = value. If you want to work with untyped values, then simply extract into something that gives you the API you want, like serde_json::Value which lets you use pointer and pointer_mut.