Function itconfig::maybe_get_vec_env[][src]

pub fn maybe_get_vec_env<T>(env_name: &str, sep: &'static str) -> Option<Vec<T>> where
    T: FromEnvString
Expand description

Same as get_vec_env but returns Option enum instead Result

Example

use std::env;

#[derive(Debug, PartialEq, Eq)]
enum PaymentPlatform {
    PayPal,
    Stripe,
    SomethingElse,
}

impl FromEnvString for PaymentPlatform {
    type Err = &'static str;

    fn from_env_string(envstr: &EnvString) -> Result<Self, Self::Err> {
        match envstr.to_lowercase().as_str() {
            "paypal" => Ok(Self::PayPal),
            "stripe" => Ok(Self::Stripe),
            "smth" => Ok(Self::SomethingElse),
            _ => Err("Unsupported payment platform"),
        }
    }
}


fn main () {
    env::set_var("PAYMENT_PLATFORMS", "paypal,stripe");

    let payment_platforms: Option<Vec<PaymentPlatform>> = maybe_get_vec_env("PAYMENT_PLATFORMS", ",");
    assert_eq!(
        payment_platforms,
        Some(vec![PaymentPlatform::PayPal, PaymentPlatform::Stripe])
    );
}