#io #error-handling #value #option-t #str

oops

Lightweight error-handling. Provides Option<T> -> std::io::Result<T>.

2 unstable releases

0.2.0 Jan 3, 2021
0.1.0 Feb 15, 2020

#2 in #option-t

Download history 14/week @ 2024-07-22 19/week @ 2024-07-29 16/week @ 2024-08-05 20/week @ 2024-08-12 10/week @ 2024-08-19 8/week @ 2024-08-26 19/week @ 2024-09-02 22/week @ 2024-09-09 19/week @ 2024-09-16 63/week @ 2024-09-23 33/week @ 2024-09-30 18/week @ 2024-10-14 10/week @ 2024-10-21 28/week @ 2024-10-28 20/week @ 2024-11-04

76 downloads per month
Used in 5 crates

MIT/Apache

6KB
90 lines

Lightweight error-handling for transforming values into std::io::Result. Provides:

  • Option<T>::oops(self, &str) -> std::io::Result<T>
  • Result<T, E>::oops(self, &str) -> std::io::Result<T>
  • Option<T>::lazy_oops(self, FnOnce() -> String) -> std::io::Result<T>
  • Resukt<T, E>::lazy_oops(self, FnOnce() -> String) -> std::io::Result<T>

Examples

use std::io::Result;

fn third_element(slice: &[usize]) -> Result<&usize> {
    // Using oops to add context to a None
    slice.iter().nth(3).oops("No third element")
}

fn parse_batch(slice: &[&str]) -> Result<Vec<usize>> {
    slice
        .iter()
        .map(|v| {
            v
                .parse::<usize>()

                // Using lazy_oops to add debug messages
                .lazy_oops(|| format!("Failed to parse {} from {:?}", v, slice))
        })
        .collect()
}

assert_eq!(
    // No third element
    third_element(&[1, 2, 3]).err().unwrap().kind(),
    std::io::ErrorKind::Other
);

assert_eq!(
    // Failed to parse lo from ["2", "3", "7", "lo", "11"]
    parse_batch(&["2", "3", "7", "lo", "11"]).err().unwrap().kind(),
    std::io::ErrorKind::Other
);

No runtime deps