Skip to content

Instantly share code, notes, and snippets.

@yuyawata
Last active February 28, 2019 06:26
Show Gist options
  • Save yuyawata/0a61e9fc116c1501e30c313683e9e34b to your computer and use it in GitHub Desktop.
Save yuyawata/0a61e9fc116c1501e30c313683e9e34b to your computer and use it in GitHub Desktop.
Rust envy example3
use serde_derive::Deserialize;
use envy;
use std::error::Error;
#[derive(Deserialize, Debug)]
struct Config {
host: String,
#[serde(default="default_port")]
port: u16,
numbers: Vec<u64>,
}
fn default_port() -> u16 {
8080
}
fn main() {
// HOST=myserver
// 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, 8080);
assert_eq!(config.numbers, vec![1,2,3]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment