Skip to content

Overwrite a value from toml with env #369

@tekjar

Description

@tekjar

Hi! I'm trying to overwrite a variable in toml with env variable. Due to restrictions w.r.t underscores, I had to alias a field with serde.

 config::{Config, ConfigError, File, Environment};
use serde::{Serialize,  Deserialize};

#[derive(Debug, Deserialize, Clone)]
pub struct Settings {
    pub name: String,
    pub v4: TlsConfig,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TlsConfig {
    pub ca_path: String,
    #[serde(alias = "certpath")]
    pub cert_path: String,
    // #[serde(alias = "keypath")]
    pub key_path: String,
}

pub fn main() -> Result<(), ConfigError> {
    std::env::set_var("V4_CERTPATH", "Hello World");

    let s = Config::builder()
        .add_source(File::with_name("default.toml"))
        .add_source(Environment::default().separator("_"))
        .build()?;

    dbg!(&s);
    let v: Settings = s.try_deserialize()?;
    dbg!(v);
    Ok(())
}

This is leading go this error

Error: duplicate field `cert_path`

How do I fix this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions