suwrap 0.1.2

Contextual and explicit replacement for unwrap()
Documentation
  • Coverage
  • 50%
    2 out of 4 items documented2 out of 3 items with examples
  • Size
  • Source code size: 17.96 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.1 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 13s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • SHENGUOWU

suwrap

Explicit, contextual replacement for unwrap().

suwrap is a tiny utility crate that provides a disciplined alternative to unwrap() and expect().

It makes invariant failures explicit and consistently contextual.


✨ Why?

unwrap() and expect() panic without structured meaning.

They:

  • Hide the invariant being enforced
  • Lose domain context
  • Create inconsistent panic messages
  • Encourage casual failure handling

suwrap enforces explicit invariant naming and consistent failure style.


📦 Installation

Add to your Cargo.toml:

[dependencies]

suwrap = "0.1"


🔥 Basic Usage

Instead of this:

let user = maybe_user.unwrap();

Write this:

use suwrap::Suwrap;

let user = maybe_user.sunwrap("user must exist");

If the invariant fails, it panics with a structured message:

[INVARIANT VIOLATION]
Context: user must exist

🧠 Option Example

use suwrap::Suwrap;

let config = std::env::var("DATABASE_URL")
    .ok()
    .sunwrap("DATABASE_URL must be set");

🧠 Result Example

use suwrap::Suwrap;

let file = std::fs::read_to_string("config.toml")
    .sunwrap("config file must be readable");

🎯 Design Principles

  • No silent unwrap
  • Explicit invariant description
  • Zero runtime cost beyond panic formatting
  • Minimal API surface
  • No dependencies

🛡 Philosophy

sunwrap() is not error handling.

It is invariant enforcement.

Use it when:

  • Failure means programmer bug
  • The program should stop
  • The condition must always hold

Do not use it for recoverable errors.


📄 License

MIT OR Apache-2.0