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 own an internal library crate and an application that uses that library, and I'm trying to move both to use figment. I checked out the For Library Authors section of the doc, but I'm a bit confused how to use it in practice.
This works great. I can call LibConfig::new() and it extracts the values out of the environment and creates the config struct.
What I want
When I start integrating this with my app, I want to have a config file like this:
---
app_value: "a"my_lib:
first_value: "b"# second_value set using env vars.
And have my rust code set up like this:
#[derive(Deserialize)]pubstructAppConfig{pubapp_value:String,pubmy_lib:LibConfig,}implAppConfig{pubfnnew() -> figment::Result<Self>{Figment::new().merge(Yaml::file("app_config.yaml")).merge(MyLib::figment())// <-- Is this right??.extract()}}
This doesn't work as expected. first_value in the lib config is set correctly, but second_value isn't. From what I can tell, this is because the LibConfig env keys aren't nested correctly. So it's trying to set the value at the top level instead of the nested object.
but that returns a new figment that can't be "unfocused"—so that works to extract the LibConfig struct but not the wrapping AppConfig struct.
It looks like figment::util::nest() exists, but that works on values, not figments.
What I think I need is something like a merge_under(&self, prefix: &str, provider: impl Provider) that does merge(), but nests the keys in provider under prefix.
Questions
Now that I've set the context, my actual questions are:
Is there any way to do what I want here? I think the library is pushing me towards having multiple different config structs and getting them out via extract_inner or focus. This works, but it's not quite as nice as being able to deserialize the whole thing.
Is it possible to get a more complete example in the "For Library Authors" section of the docs? Something that shows not just how the library should be set up, but how it's expected to be used by a dependent.
Thanks!
The text was updated successfully, but these errors were encountered:
I own an internal library crate and an application that uses that library, and I'm trying to move both to use figment. I checked out the For Library Authors section of the doc, but I'm a bit confused how to use it in practice.
What I've got
I've set up the library config roughly like this:
This works great. I can call
LibConfig::new()
and it extracts the values out of the environment and creates the config struct.What I want
When I start integrating this with my app, I want to have a config file like this:
And have my rust code set up like this:
This doesn't work as expected.
first_value
in the lib config is set correctly, butsecond_value
isn't. From what I can tell, this is because the LibConfig env keys aren't nested correctly. So it's trying to set the value at the top level instead of the nested object.I thought maybe
focus()
could this—something likebut that returns a new figment that can't be "unfocused"—so that works to extract the
LibConfig
struct but not the wrappingAppConfig
struct.It looks like
figment::util::nest()
exists, but that works on values, not figments.What I think I need is something like a
merge_under(&self, prefix: &str, provider: impl Provider)
that doesmerge()
, but nests the keys inprovider
underprefix
.Questions
Now that I've set the context, my actual questions are:
extract_inner
orfocus
. This works, but it's not quite as nice as being able to deserialize the whole thing.Thanks!
The text was updated successfully, but these errors were encountered: