<div align="center">
# toobad
[](https://crates.io/crates/toobad)
[](https://docs.rs/toobad)
[](https://codeberg.org/razkar/toobad/src/branch/main/LICENSE-MIT)
[](https://codeberg.org/razkar/toobad/src/branch/main/LICENSE-APACHE)
[](https://crates.io/crates/toobad)
[](https://codeberg.org/razkar/toobad)
[](https://codeberg.org/razkar/toobad/issues)
[](https://doc.rust-lang.org/edition-guide/rust-2024/)
[](https://codeberg.org/razkar/toobad)
Want better error handling? Well, toobad.
`toobad` is a simple, unbloated error handling library for Rust.
</div>
```sh
cargo add toobad
```
```toml
[dependencies]
toobad = "0.1"
```
---
## The Basics
Any error type automatically converts into `toobad::Error` via `?`:
```rust
use toobad::Result;
fn read_config() -> Result<String> {
let content = std::fs::read_to_string("config.toml")?;
Ok(content)
}
```
---
## Adding Context
Wrap any `Result` or `Option` with a human-readable message:
```rust
use toobad::{Context, Result};
fn read_config() -> Result<String> {
let content = std::fs::read_to_string("config.toml")
.context("failed to read config file")?;
Ok(content)
}
```
Prints as:
```
failed to read config file: No such file or directory (os error 2)
```
Works on `Option` too:
```rust
let value = some_map.get("key").context("key not found")?;
```
---
## Creating Errors from Scratch
Use the `toobad!()` macro with full `format!()` support:
```rust
use toobad::toobad;
return Err(toobad!("expected {} items but got {}", expected, actual));
```
Or bail early with `bail!()`:
```rust
use toobad::bail;
bail!("expected {} items but got {}", expected, actual);
// equivalent to: return Err(toobad!(...))
```
Or assert conditions with `ensure!()`:
```rust
use toobad::ensure;
ensure!(x > 0, "x must be positive, got {}", x);
// equivalent to: if !condition { bail!(...) }
```
---
## Downcasting
Recover the original error type when you need it:
```rust
if let Some(io_err) = error.downcast::<std::io::Error>() {
match io_err.kind() {
std::io::ErrorKind::NotFound => { /* handle missing file */ }
_ => { /* handle other io errors */ }
}
}
```
---
## License
This project is licensed under either of [MIT License](https://codeberg.org/razkar/toobad/src/branch/main/LICENSE-MIT) or [Apache License, Version 2.0]([LICENSE-APACHE](https://codeberg.org/razkar/toobad/src/branch/main/LICENSE-APACHE) at your option.
Cheers, RazkarStudio
© 2026 RazkarStudio. All rights reserved