derive_from_env

Crates.ioderive_from_env
lib.rsderive_from_env
version0.1.2
sourcesrc
created_at2024-07-29 04:29:00.311069
updated_at2024-07-31 08:32:28.26514
descriptionExtract type safe structured data from environment variables with procedural derive macros
homepagehttps://github.com/AntoineRenaud91/derive_from_env
repositoryhttps://github.com/AntoineRenaud91/derive_from_env
max_upload_size
id1318482
size46,017
Tonio Rd (AntoineRenaud91)

documentation

https://docs.rs/derive_from_env

README

derive_from_env

Extract type safe structured data from environment variables with procedural derive macros. Currently based on the FromStr trait.

Usage

  • Add derive_from_env to your project's dependencies:
[dependencies]
derive_from_env = "0.1.1"
  • Usage example:
use std::net::{IpAddr, Ipv4Addr};
use std::str::FromStr;
use derive_from_env::FromEnv;

#[derive(Debug, PartialEq, FromEnv)]
struct ServiceConfig {
    api_key: String,
    #[from_env(var = "EXT_SERVICE_URL")]
    base_url: String,
}

#[derive(Debug, PartialEq)]
enum AuthMethod {
    Bearer,
    XAPIKey,
}

impl FromStr for AuthMethod {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Bearer" => Ok(AuthMethod::Bearer),
            "X-API-Key" => Ok(AuthMethod::XAPIKey),
            _ => Err("Invalid auth method".into()),
        }
    }
}

#[derive(Debug, PartialEq)]
#[derive(FromEnv)]
struct AuthConfig {
    #[from_env(from_str)]
    auth_method: AuthMethod,
    api_key: String,
}

#[derive(Debug, PartialEq, FromEnv)]
struct AppConfig {
    #[from_env(default = "0.0.0.0")]
    addr: IpAddr,
    port: Option<u16>,
    external_service: ServiceConfig,
    #[from_env(no_prefix)]
    auth: AuthConfig 
}

fn main() {
    std::env::set_var("EXTERNAL_SERVICE_API_KEY", "api-key");
    std::env::set_var("EXT_SERVICE_URL", "http://external.service/api");
    std::env::set_var("PORT","8080");
    std::env::set_var("AUTH_METHOD","Bearer");
    std::env::set_var("API_KEY","api-key");
    let app_config = AppConfig::from_env().unwrap();
    assert_eq!(app_config, AppConfig {
        port: Some(8080),
        addr: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
        external_service: ServiceConfig {
            api_key: "api-key".into(),
            base_url: "http://external.service/api".into() 
        },
        auth: AuthConfig {
            auth_method: AuthMethod::Bearer,
            api_key: "api-key".into()
        }
    });
}

In the future...

  • serde support via a new attribute from_serde (usefull for chrono's types)
Commit count: 0

cargo fmt