struct_error 0.0.1

Modern, flat, zero-cost error flow based on pure struct errors.
Documentation
  • Coverage
  • 23.53%
    4 out of 17 items documented1 out of 11 items with examples
  • Size
  • Source code size: 16.28 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.44 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 45s Average build duration of successful builds.
  • all releases: 45s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Alex6357/struct_error
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Alex6357

Modern, flat, zero-cost error flow based on pure struct errors.

⚠️ WARNING: UNAUDITED CODE

This crate was AI-generated and has not undergone human review. It likely contains bugs and should NOT be used in production. The API is subject to change after audit.

struct_error inverts the traditional Rust error model:

  • Errors are first-class structs, not enum variants.
  • No manual Ok/Err wrapping inside #[throws] functions.
  • Pattern match by type name without destructuring nested Result/Enum layers.
  • Zero runtime cost: everything is resolved at compile time via procedural macros.

Core Concepts

Item Purpose
#[error] Define an atomic error struct with auto-derived Debug, Display, and Error.
#[united_error] Create a compile-time alias for a set of errors.
#[throws] Rewrite a function to implicitly return Result<T, Unt<...>> and intercept ?.
match_error! Blind, type-driven pattern matching on errors.
[throw!] Explicitly throw an error inside a #[throws] function.
[Unt] Runtime heterogeneous list (nested enum) representing the implicit union.
[End] Uninhabited terminator for the Unt HList.

Examples

A complete end-to-end example:

use struct_error::{error, united_error, throws, match_error, throw};

#[error("resource not found: {}", self.id)]
pub struct NotFound {
    pub id: u64,
}

#[error("connection timed out after {}ms", self.ms)]
pub struct Timeout {
    pub ms: u64,
}

#[united_error(NotFound, Timeout)]
pub struct AppError;

#[throws(NotFound, Timeout)]
pub fn fetch_resource(id: u64) -> String {
    if id == 0 {
        throw!(NotFound { id });
    }
    if id > 100 {
        throw!(Timeout { ms: 5000 });
    }
    format!("resource-{}", id)
}

#[throws(NotFound, Timeout)]
pub fn process(id: u64) -> String {
    let res = fetch_resource(id)?;
    res.to_uppercase()
}

fn main() {
    let result = process(0);
    match_error!(result {
        Ok(v) => println!("success: {}", v),
        NotFound { id } => println!("not found: {}", id),
        Timeout { ms } => println!("timeout: {}ms", ms),
    });
}