You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I will start by saying I really like how simple config-rs is, 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: MySettings and this is defined as:
pubstructMySettings{host:String,// this prop is requiredport:u16,// this can have a default value, let's say 8000}
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 (like port)? 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:
Required fields (like host) fail loudly if not provided by any source
Optional fields (like port) fall back to library-defined defaults
This works when MySettings is nested in larger configurations
I am aware I can use ConfigBuilder::set_default but that will happen on the main caller (in this case, the app using MyAppConfig), and I cannot use Default for MySettings because while port has a default value, host doesn'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.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
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