-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Automatic extraction based on cargo features #48
Comments
The idea is appealing, in general, but here are some reasons why we might want to avoid this:
In general, it seems that we're trading off a few lines of code, which make logic and intent explicit and clear, for an interface that has no obvious defaults and may not work for many cases. For instance, this interface would not work for Rocket, whose default figment looks like this: Figment::from(Config::default())
.merge(Toml::file(Env::var_or("ROCKET_CONFIG", "Rocket.toml")).nested())
.merge(Env::prefixed("ROCKET_").ignore(&["PROFILE"]).global())
.select(Profile::from_env_or("ROCKET_PROFILE", Self::DEFAULT_PROFILE)) I suspect it won't work for most libraries, in fact. What are your thoughts on writing this in an external library? |
I'm closing this out based on the above. |
Motivation
Currently Figment requires a couple of lines of code to get started. This includes the config structure (which is reasonable) as well as setting up the extractors. I suggest an alternative interface to generate the configuration by writing a one liner, based on cargo features
Example
A basic developer writing a binary crate called
foo
writes the following code to extract theConfig
struct from a toml and environment (import statements elided)If the developer later decides to enable
yaml
support, the following changes are requiredNotice how the developer is required to add both
.yaml
andyml
files, because the yaml spec allows for both file extensions. This could be improved using a little more rusty magic.Proposed api
Provide an
auto
method onFigment
that automatically sets up all Figment extractors based on currently enabled featuresAdding yaml support is as easy as editing the
Cargo.toml
. Just include theyaml
feature and it will automatically add all variants without modifyingmain.rs
This could be further enhanced by providing a macro that automatically uses the crate name as the config name, as well as offer some degree of configuration with varargs.
Caveats
json
files overyaml
.Advantages
cargo add
, write the scheme andlet config: Config = figment::auto()
.yaml
,.yml
, uppercase, lowercase, title...) reduces chances of debugging typosFigment::new()
can still be usedFigment::auto()
returns aFigment
struct, which can be further chained with more extractorsFinal thoughts
The text was updated successfully, but these errors were encountered: