Replies: 1 comment
-
|
I think the idiomatic way here is to let You don’t need use serde::Deserialize;
fn default_port() -> u16 {
8000
}
#[derive(Debug, Deserialize)]
pub struct MySettings {
pub host: String,
#[serde(default = "default_port")]
pub port: u16,
}
#[derive(Debug, Deserialize)]
pub struct MyAppConfig {
pub name: String,
pub settings: MySettings,
}Then:
So the library only needs to expose a Example config: name = "my-app"
[settings]
host = "localhost"This should deserialize with So imo the answer is: no need for |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I will start by saying I really like how simple
config-rsis, in theory you don't depend on anything for configuration values so it can be included as part of a library easily, great!Now, there is one problem I found though, let's say I have a simple configuration value in one of my libraries:
MySettingsand this is defined as:And later in our app just include it as part of our app configuration:
And use our sources and all to fill our configuration, but then what happen with those required fields (like
host) and those with default values (likeport)? In things like Figment the guide for library developers just solves this exposing the configuration to your providers (and depending on Figment in the process, see https://docs.rs/figment/latest/figment/#for-library-authors), but what about here?How can I ensure that:
host) fail loudly if not provided by any sourceport) fall back to library-defined defaultsI am aware I can use
ConfigBuilder::set_defaultbut that will happen on the main caller (in this case, the app usingMyAppConfig), and I cannot useDefaultforMySettingsbecause whileporthas a default value,hostdoesn't. Do you see my dilemma?For really big configurations (let's say, very deep hierarchies and a lot of config values with default or required values) this is sort of crucial.
Is there an out-of-the-box way to handle this in config-rs? I have a few ideas for workarounds, but I want to know if there's an idiomatic approach before implementing something custom.
Beta Was this translation helpful? Give feedback.
All reactions