rust-cli/confy

Newly added values result in BadTomlData even though a default was implemented?

mrusme opened this issue · 5 comments

This is more a question/suggestion than an issue/bug report, since I cannot seem to find an explanation on this behaviour.

Assuming there is an AppConfig struct in place that contains of a handful values and there is a default implementation for those values in place as well. Then this config is being loaded on the very first launch and presumably stored on exit.

When the AppConfig struct changes, however, and for example a new value was introduced, it looks like confy simply refuses to load the configuration altogether, even though a default for that new value was specified:

Error: BadTomlData(Error { inner: ErrorInner { kind: Custom, line: Some(0), col: 0, at: Some(0), message: "missing field `api_endpoint`", key: [] } })

Would it be possible/make sense to "merge" the existing config and the defaults for value that does not exist in the .toml yet, in order to allow loading the configuration?

I had the same issue. The solution was to add #[serde(default)] attribute to AppConfig struct.

@martinsp thanks for the advice! It works

@charlie632 @mrusme i'm assuming we can close this now?

@Dylan-DPC for now I think it is a good workaround

I thought about it but did not found a way to implement that.
Thanks for the confirmation that the workaround is working, and for serde to implement this on their side.