Skip to main content

Crate struct_error

Crate struct_error 

Source
Expand description

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

ItemPurpose
#[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.
UntRuntime heterogeneous list (nested enum) representing the implicit union.
EndUninhabited 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),
    });
}

Re-exports§

pub extern crate macro_magic;

Macros§

__throws_impl
Internal function-like proc macro called by __throws_cps at the end of the token forwarding chain.
match_error
Blind, type-driven pattern matching on errors.
throw
Explicitly throw an error inside a #[throws] function.

Enums§

End
Uninhabited terminator for the Unt HList.
Unt
Heterogeneous list (nested enum) — runtime representation of the implicit error union.

Attribute Macros§

__struct_error_members
Internal attribute macro that encodes the member list on structs generated by #[united_error].
__throws_cps
Internal attribute macro used as the CPS callback for macro_magic::forward_tokens!.
error
Defines an atomic error struct, automatically deriving Debug, Display, and Error.
throws
Replaces the function’s return type with an implicit error union and rewrites control flow.
united_error
Defines a compile-time alias for a set of errors (a united error).