Closed jalaziz closed 1 year ago
My pattern for doing this is to:
default
feature support, annoying but it works :)let figment = Figment::from(Serialized::defaults(MyConfig::default()))
and later merge with the other config providers If you don't want to use serde's default attribute, nor create a secondary struct, the simplest option, albeit unsatisfying, is to merge the default values into a Figment
manually:
Figment::new()
.merge(Serialized::default("host", ...))
.merge(Serialized::default("port", ...))
Extracting your type from this Figment (with the desired providers added) would then only require the required field(s).
You could also apply this idea in reserve by serializing your default struct with all of its field into a figment, setting the required fields to None, and then extracting from there.
Unfortunately I don't know of a better solution in Rust. There is unfortunately no way to say "give me a struct with the same fields as this struct expect exclude these," which is what we'd need here. You could theoretically write a procedural derive to accomplish this, though I'm not aware of a crate that does this, and a quick search turned up empty, but perhaps you'd have more luck.
You could also apply this idea in reserve by serializing your default struct with all of its field into a figment, setting the required fields to None, and then extracting from there.
For this approach, do you mean have default that sets all fields (potentially with some dummy values), then use Serialized::default("port", ...)
to set the override and set required fields to None
?
Unfortunately I don't know of a better solution in Rust.
Yeah, I tried searching myself and it's not an easy thing to search for. I'm also not convinced it would be any cleaner.
I ultimately went with serde's default
feature. Even though it means I have to define functions for every default, it's a scalable pattern. I didn't use Serialized::defaults(...)
though, since that would require all the fields to be set.
Thanks everyone for the input. Always good to confirm I'm not missing the obvious 😄.
I apologize if this should be trivial as I'm relatively new to Rust.
Is there a recommended pattern for defining defaults for a config struct where some fields should be optional with defaults but others must be passed in by the user?
For example, given the struct:
I would like to provide defaults for everything except
required_field
, e.g.:Unfortunately, this won't work due to having to define all fields on a struct. Using
Option
s doesn't solve the problem because thenfigment
treats them as optional fields.I was looking into
serdes
's defaults as an option, but the lack of literals support makes things pretty annoying.I assume another option would be to merge in a
Defaults
struct that defines all but the optional fields.Is there a recommended pattern? Is there another, simpler option that I'm missing?