Fluent_Result
A compact crate offering a suite of extensions providing helpers for manipulating and transforming Result and Option types fluently.
This crate is no_std compatible and contains no unsafe code. Some features are gated by alloc (enabled by default).
Provided Traits
IntoOption
Wrap any value in a Option. This is equivalent to Some(value) and None, but may be more readable in long chains.
use IntoOption;
let some = 42.into_some;
assert_eq!;
// Typically less useful, but included for completeness.
let none = 42.;
assert_eq!;
IntoResult
Wrap any value in a Result. This is equivalent to Ok(value) and Err(value), but may be more readable in long chains.
use IntoResult;
// if necessary, the error type can be specified
let ok = 42.;
assert_eq!;
// likewise the ok type can be specified
let err = 42.;
assert_eq!;
Sink
Handle a variant of a Result or Option by sinking it into a Sink.
This is useful for handling a variant by sinking it into a side-effecting function, for example logging. Especially useful for methods that return Result<(), E> for example.
See the documentation for brief examples.
bool::Then
Transforms bool values into Option or Result types for easier control flow with the ? operator, or to replace simple if statements.
Convert to Option:
then_none()- ReturnsNoneon true,Some(())on false (useful for guard clauses)
Convert to Result:
then_err(err)- ReturnsErr(err)on true,Ok(())on falsethen_err_with(|| err)- Lazy version ofthen_errto_result(on_true, on_false)- ReturnsOk(on_true)on true,Err(on_false)on falseto_result_with(|| on_true, || on_false)- Lazy version ofto_result
use Then;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
bool::expect
Provides debug-only (bool::dbg) and release-mode (bool::rls) assertions for bool values. Each mode offers both assert_*() methods with fixed panic messages and expect_*() methods with custom messages.
Debug-only assertions (no-op in release):
use Expect;
true.assert_true; // Fixed panic message
true.expect_true; // Custom panic message
Always-on assertions:
use Expect;
true.assert_true; // Fixed panic message
true.expect_true; // Custom panic message
expect_none
Provides debug-only (expect::dbg) and release-mode (expect::rls) assertions for unwrapping the None variant of an Option<T>. This is useful for validating methods that should return None but may return Some in some cases. For example, when inserting a key value pair that should be unique into a hashmap. Each mode offers both assert_none() with a fixed panic message and expect_none() with a custom message.
Debug-only assertions (no-op in release):
use HashMap;
use ExpectNone;
let mut map = new;
map.insert.assert_none; // Fixed panic message
map.insert.expect_none; // Custom panic message
Always-on assertions:
use HashMap;
use ExpectNone;
let mut map = new;
map.insert.assert_none; // Fixed panic message
map.insert.expect_none; // Custom panic message
FlattenErr
Flattens a Result<Result<T, EInner>, EOuter> into a Result<T, NestedError<EInner, EOuter>>. This is useful when working with nested Result types where you want to preserve both the inner and outer error types.
use ;
let result: = Ok;
let ok = result.flatten_err.expect;
assert_eq!;
let result: = Ok;
let err = result.flatten_err.expect_err;
assert_eq!;
let result: = Err;
let err = result.flatten_err.expect_err;
assert_eq!;
BoxErr
Handles nested Result types by boxing errors into a Box<dyn Error>. This trait works with Results with up to four layers of nesting, so long as all error types implement std::error::Error. This is useful when working with operations that produce nested Results and erasing the error type is acceptable.
If all the error types are the same, consider using Result::flatten instead. For results with only two layers of nesting, consider using FlattenErr::flatten_err.
This trait requires the alloc feature, which is enabled by default.
use Error;
use BoxErr;
let result: = Ok;
let boxed: = result.box_err;
assert_eq!;
let err_io = from;
let result: = Ok;
let boxed: = result.box_err;
assert!;