envy icon indicating copy to clipboard operation
envy copied to clipboard

Continue parsing for more missing required values

Open hbobenicio opened this issue 4 years ago • 4 comments

💡 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
    ]
)

hbobenicio avatar Aug 16 '19 11:08 hbobenicio

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 avatar Aug 16 '19 13:08 softprops

@softprops Any progress?

That would be super handy! :)

hbobenicio avatar Nov 29 '19 20:11 hbobenicio

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)

hbobenicio avatar Dec 04 '19 12:12 hbobenicio

These serde docs may be helpful:

  • https://serde.rs/impl-deserialize.html
  • https://serde.rs/deserialize-struct.html

hbobenicio avatar Dec 16 '19 11:12 hbobenicio