Crates.io | some-to-err |
lib.rs | some-to-err |
version | 0.2.1 |
source | src |
created_at | 2023-03-17 08:36:26.671948 |
updated_at | 2023-04-08 17:31:55.027199 |
description | 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. |
homepage | https://github.com/cyphersnake/some-to-err |
repository | https://github.com/cyphersnake/some-to-err |
max_upload_size | |
id | 812550 |
size | 8,389 |
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.
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"));
}