Last active
February 28, 2019 06:26
-
-
Save yuyawata/0a61e9fc116c1501e30c313683e9e34b to your computer and use it in GitHub Desktop.
Rust envy example3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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