Add parse
to Result
The core purpose of this library is to give better ergonomics to parsing from
sources which could fail, e.g. reading stdin
.
extern crate parse_result;
use parse_result::*;
use std::env;
fn main() {
// It turns code like this:
env::var("PORT").map(|s| s.parse().unwrap_or(3000)).unwrap_or(3000);
// Into this:
env::var("PORT").parse().unwrap_or(3000);
// Matching to find the specific failure
match env::var("PORT").parse::<u32>() {
Ok(port) => println!("Parsed port {} successfully!", port),
Err(OriginalErr(e)) => panic!("Failed to get PORT from env: {}", e),
Err(ParseFailure(e)) => panic!("Failed to parse PORT: {}", e),
}
}