toobad 0.1.0

A simple, intuitive error handling library
Documentation
<div align="center">

# toobad

[![Crates.io Version](https://img.shields.io/crates/v/toobad)](https://crates.io/crates/toobad)
[![docs.rs](https://img.shields.io/docsrs/toobad)](https://docs.rs/toobad)
[![License MIT](https://img.shields.io/crates/l/toobad)](https://codeberg.org/razkar/toobad/src/branch/main/LICENSE-MIT)
[![License Apache-2.0](https://img.shields.io/crates/l/toobad)](https://codeberg.org/razkar/toobad/src/branch/main/LICENSE-APACHE)
[![Crates.io Downloads](https://img.shields.io/crates/d/toobad)](https://crates.io/crates/toobad)
[![Codeberg Stars](https://img.shields.io/badge/dynamic/json?url=https://codeberg.org/api/v1/repos/razkar/toobad&query=$.stars_count&label=stars)](https://codeberg.org/razkar/toobad)
[![Codeberg Issues](https://img.shields.io/badge/dynamic/json?url=https://codeberg.org/api/v1/repos/razkar/toobad&query=$.open_issues_count&label=issues)](https://codeberg.org/razkar/toobad/issues)
[![Rust Edition](https://img.shields.io/badge/rust%20edition-2024-orange)](https://doc.rust-lang.org/edition-guide/rust-2024/)
[![Maintenance](https://img.shields.io/badge/maintenance-actively--developed-brightgreen)](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