Skip to content

Conversation

@rliang
Copy link

@rliang rliang commented May 12, 2025

Hi there, first of all thanks for this crate.

I'm opening a PR for supporting prefixing in nested structures. So it differs by #43 since that one supports prefixes on the structs themselves.

It solves a pain point in one of my codebases that we can't reuse structures:

#[derive(Envconfig)]
pub struct DbConfig {
    #[envconfig(from = "HOST")]
    pub host: String,
    #[envconfig(from = "PORT", default = "5432")]
    pub port: u16,
}

#[derive(Envconfig)]
pub struct Config {
    #[envconfig(nested)]
    pub foo: DbConfig,
    #[envconfig(nested)]
    pub bar: DbConfig,
}

In this example, I'd have to create another identical DbConfig struct for each database. But with this PR, we can reuse it:

#[derive(Envconfig)]
pub struct DbConfig {
    #[envconfig(from = "HOST")]
    pub host: String,
    #[envconfig(from = "PORT", default = "5432")]
    pub port: u16,
}

#[derive(Envconfig)]
pub struct Config {
    #[envconfig(nested, prefix = "FOO_")]
    pub foo: DbConfig,
    #[envconfig(nested, prefix = "BAR_")]
    pub bar: DbConfig,
}

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

Successfully merging this pull request may close these issues.

1 participant