filipegoncalves / rust-config

Configuration file library in Rust similar to C/C++'s libconfig
MIT License
20 stars 10 forks source link

Better error handling in env var parsers #14

Open workanator opened 9 years ago

workanator commented 9 years ago

Hello,

per our conversation in the mean pull request I started to think more on error handling in env parsers. Probably you are right and that will be good to return an error saying "Environment variable A is undefined" but with some rules. We can divide parsers to permissive and restrictive. Permissive parsers return the default value if the env var is empty. Restrictive parsers return the error if the env var is empty or if the env var cannot be correctly parsed. Dividing the existing parsers in those groups we have:

Permissive:

Restrictive:

But it's good to have say permissive boolean parser so we need an additional syntax, for example we can add question after the type - $"SOME_VAR_NAME"::bool?.

Those are just thoughts. The idea of env var parsers becomes more complex and maybe the idea on permissive and restrictive parsers is excessive.

filipegoncalves commented 9 years ago

I agree that this is something we can work on and the division between restrictive and permissive makes sense.

From my perspective there are mainly 4 different scenarios:

Of course, with ::auto there isn't a distinction between permissive strongly typed and permissive weakly typed (we just infer the correct type in the first place); ditto for the restrictive versions.

The challenge is to make it easy to have any of this kind of parser in configurations without making the syntax a pain (and also, we should think if all 4 of them make sense). I was also thinking that it could be useful to add configuration-scoped variables, and that these could override environment variables (so something like, first search for $"FOO" as a local configuration variable, if not defined, search as an environment variable), but this is a topic for another discussion - but it certainly plays a role in how we do error handling with non-existent / invalid environment variables.

More to come in the following days (probably something to work on over the weekend), but this looks reasonable.

Thoughts?

workanator commented 9 years ago

Regarding to strongly/weekly typed I'm thinking of two options:

About the local config variables I'm not sure. Everything here depends on how they will be implemented. I mean syntax and other rules. But local variables may be good if we have conditional configuration when we can do some test right in the config and then assign the value to the local variable depending on results. Other option is to implement simple functions and when we need $"FOO"::int or a default value if env var is empty then we can do say COALESCE($"FOO"::int, 99).