how.rs/README.md

41 lines
1.4 KiB
Markdown
Raw Permalink Normal View History

2022-07-21 20:40:44 -04:00
# *How*
2022-07-21 20:39:47 -04:00
A seriously contextual error library that focuses on how you got there. Designed to make debugging easier, *how* enables you to concisely capture any and all context that could possibly have contributed to an error.
2022-07-21 20:40:44 -04:00
## Getting started
A basic example takes just 2 imports:
2022-07-21 20:40:44 -04:00
```rust,should_panic
use how::{How, Result};
2022-07-21 20:40:44 -04:00
fn main() -> Result<()> {
Err(How::new("TODO: implement amazing new program"))
}
```
But usually you'll want to [attach some information](Explain::context) too:
```rust,should_panic
use how::{How, Explain, Result};
fn main() -> Result<()> {
Err(How::new("TODO: implement amazing new program")
.context("I plan to do it eventually™"))
}
```
And you'll probably want to get [nicer output](TerminationResult) when you return an error from `main` (this one requires `feature = "termination"`):
```rust,should_panic,compile_fail
use how::{How, Explain, Result, TerminationResult};
fn main() -> TerminationResult<()> {
Err(How::new("TODO: implement amazing new program")
.context("I plan to do it eventually™"))
.into()
}
```
[`How`] intentionally omits a [`From`] implementation for [`Error`](std::error::Error) to discourage the creation of [`How`]s with no caller context. Instead, the [`Explain`] trait is implemented for all [`Result`][^1] and [`Option`] and provides a convenient [`context`](Explain::context) function.
2022-07-21 20:40:44 -04:00
[^1]: Where `E` is either [`How`] or implements [`Error + 'static`](std::error::Error).