yuyawata
2/28/2019 - 5:43 AM

Rust envy example1

Rust envy example1

[package]
name = "rust-envy-sample"
version = "0.1.0"
authors = ["Yutaka Yawata"]
edition = "2018"

[dependencies]
envy = "0.3.3"
serde = "1.0.87"
serde_derive = "1.0.87"
use serde_derive::Deserialize;
use envy;
use std::error::Error;

#[derive(Deserialize, Debug)]
struct Config {
    host: String,
    port: Option<u16>,
    numbers: Vec<u64>,
}

fn main() {
    // HOST=myserver
    // PORT=8080
    // NUMBERS=1,2,3

    let config = match envy::from_env::<Config>() {
        Ok(val) => val,
        Err(err) => {
            println!("{}", err);
            process::exit(1);
        }
    };

    println!("{:#?}", config);

    assert_eq!(config.host, "myserver");
    assert_eq!(config.port, Some(8080));
    assert_eq!(config.numbers, vec![1,2,3]);
}