#conversion #utilities #options #result #convert #cargo-toml

some-to-err

A set of traits for converting Option to Result, providing methods to transform Some values to Err while handling None values as Ok, either directly or using a closure to generate the Ok result

6 releases

0.2.1 Apr 8, 2023
0.2.0 Mar 24, 2023
0.1.1 Mar 20, 2023
0.0.2 Mar 17, 2023

#2598 in Rust patterns

Download history 10/week @ 2024-03-11 92/week @ 2024-03-18 49/week @ 2024-04-01 18/week @ 2024-04-08 27/week @ 2024-04-15 13/week @ 2024-04-22 1/week @ 2024-04-29 26/week @ 2024-05-06 7/week @ 2024-05-13 61/week @ 2024-05-20 27/week @ 2024-05-27 35/week @ 2024-06-03 28/week @ 2024-06-10 28/week @ 2024-06-24

92 downloads per month

MIT license

7KB
82 lines

Some To Err

Crates.io Crates.io License: MIT Build Status Documentation

This crate offers a pair of traits for effortlessly transforming Option into Result, elegantly converting Some values into Err while gracefully handling None values as Ok. Unleash the full potential of Rust's error handling capabilities with these versatile traits.

Usage

Add this to your crate by:

cargo add some-to-err

Or add this to your Cargo.toml:

[dependencies]
some-to-err = "0.2.0"

and then:

use some_to_err::ErrOr;

{
    let some: Option<&str> = Some("Error");
    let result = some.err_or(42);
    assert_eq!(result, Err("Error"));
}

{
    let none: Option<&str> = None;
    let result = none.err_or(42);
    assert_eq!(result, Ok(42));
}
use some_to_err::ErrOrElse;
{
    let input: Option<&str> = None;
    let result = input.err_or_else(|| "Ok");
    assert_eq!(result, Ok("Ok"));
}

{
    let input = Some("Error");
    let result = input.err_or_else(|| "Ok");
    assert_eq!(result, Err("Error"));
}

No runtime deps