rust-cli / confy

🛋 Zero-boilerplate configuration management in Rust
Other
907 stars 61 forks source link

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

Closed mrusme closed 3 years ago

mrusme commented 4 years ago

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?

martinsp commented 4 years ago

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

charlie632 commented 3 years ago

@martinsp thanks for the advice! It works

Dylan-DPC-zz commented 3 years ago

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

charlie632 commented 3 years ago

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

Zykino commented 3 years ago

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.