tiny_bail
Bailing is an error-handling pattern that takes the middle path between unwrap and ?:
- Compared to
unwrap: Bail willreturn,continue, orbreakinstead of panicking. - Compared to
?: Bail will log or quietly discard the error instead of propagating it.
The middle path avoids unwanted panics without the ergonomic challenges of propagating errors with ?.
This crate provides six macro variants:
Along with their tiny aliases:
r!,
rq!,
c!,
cq!,
b!, and
bq!.
The macros support Result, Option, and bool types out of the box. You can implement
IntoResult to extend this to other types.
Example
use *;
// With `tiny_bail`:
// Without `tiny_bail`:
Getting started
To use this crate, add it to your Cargo.toml:
cargo add tiny_bail
You can set features to customize the logging behavior on failure:
# Log with `println!` instead of `tracing::warn!`.
cargo add tiny_bail --no-default-features
# Log with `log::info!` instead of `tracing::warn!`.
cargo add tiny_bail --no-default-features --features log,info
This crate has zero dependencies other than the logging backend you choose (log, tracing, or nothing).
License
This crate is available under either of MIT or Apache-2.0 at your choice.