envy
envy copied to clipboard
Continue parsing for more missing required values
💡 Feature description
Continue parsing for more missing required values (instead stopping at the first one).
😍 Motivation
This can provide more information to the user/devops/developer in order to setup all missing values at once (instead of one by one).
💻 Basic example
Today, this:
use serde::Deserialize;
#[derive(Deserialize, Debug)]
struct Config {
host: String,
port: String,
}
fn main() {
match envy::prefixed("APP_").from_env::<Config>() {
Ok(config) => println!("{:#?}", config),
Err(err) => {
eprintln!("error: {:#?}", err);
std::process::exit(1);
}
}
}
running with:
cargo run
outputs this:
error: MissingValue(
"host",
)
The desired behaviour would output this instead:
error: MissingValues(
[
"host", // Or even better: "APP_HOST", considering prefix
"port" // Or even better: "APP_PORT", considering prefix
]
)
Oh that's interesting. This crate relies heavily on serde and I'm not sure that serde provides that hook but I can do some investigation.
@softprops Any progress?
That would be super handy! :)
Is it possible to create a custom serde deserializer that, when trying to deserialize a given struct property, checks for failure then save an error context (like a "names of the missing required fields" vector) and then continue the deserialization process somehow ? (for the other missing required fields)
When I mean "required" here is some deserializable type that is not an Option<T>
(which is ok already)
These serde docs may be helpful:
- https://serde.rs/impl-deserialize.html
- https://serde.rs/deserialize-struct.html