Skip to main content

ConfigValue

Derive Macro ConfigValue 

Source
#[derive(ConfigValue)]
Expand description

Derive confroid::FromEnv for a scalar configuration value.

ConfigValue is the type-definition alternative to confroid::from_str_scalar!. The type must implement FromStr, and its parser error must implement Error, Send, Sync, and 'static so it can be retained as the source of confroid::ConfroidError::EnvVarInvalid.

When confroid’s docs feature is enabled, the type must also implement Display. This explicit generated bound makes missing display support point back to the scalar type rather than to a config field that happens to use default or example.

§Example

use std::str::FromStr;

#[derive(Debug, confroid::ConfigValue)]
enum Mode {
    Standalone,
    Cluster,
}

impl FromStr for Mode {
    type Err = std::io::Error;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "standalone" => Ok(Self::Standalone),
            "cluster" => Ok(Self::Cluster),
            other => Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                format!("unknown mode `{other}`"),
            )),
        }
    }
}