parse_result 0.1.0

Add parse to Result
Documentation
  • Coverage
  • 100%
    6 out of 6 items documented1 out of 4 items with examples
  • Size
  • Source code size: 18.39 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.5 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Ryman/parse-result
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Ryman

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),
    }
}