Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable prefix #12

Closed
phrohdoh opened this issue May 31, 2017 · 3 comments
Closed

Configurable prefix #12

phrohdoh opened this issue May 31, 2017 · 3 comments

Comments

@phrohdoh
Copy link

For example if my app is named "barstool" I'll probably be looking for BARSTOOL_BAZ, BARSTOOL_FOO, etc.

@softprops
Copy link
Owner

Neat idea. That's very common. I'll take a look at this next weekend

@softprops
Copy link
Owner

A thought just occured to me that you can already accomplish this with a feature built right into serde. You can tell serde to rename a field on a field by field basis.

here is an example

$ FOO_BAR=test cargo run --example prefixed
FOO_BAR=test cargo run --example prefixed
   Compiling envy v0.3.1 (file:///Users/dougtangren/code/rust/envy)
    Finished dev [unoptimized + debuginfo] target(s) in 1.27 secs
     Running `/Users/dougtangren/code/rust/envy/target/debug/examples/prefixed`
provided config.bar Some("test")

I can see how this would get annoying and would remove some of the convenience of having things just work with a given struct.

Serde also has another neat feature where you can rename all fields with a formatter. Unfortunately this doesn't provide a user-defined hook though so it it doesn't really solve for the case you're looking for

Im thinking adding a prefix would actually be pretty easy but bit may make the surface area a little wider. Let me think about this a bit. In the mean time let me know how you feel about using the rename feature of serde. My thought is that it would be more inline with "the serde way" to use buildt in serde features to do renaming. Ideally you could so something like

#[serde(rename_all = "prefix(barstool_)")]
struct Config {
  // ...
}

@softprops
Copy link
Owner

published. this was simple to add check up the updated example you can know use a prefixed version of from_env for BARSTOOL vars using the following

envy::prefixed("BARSTOOL_").from_env<_>()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants