Crate borked

Source
Expand description

§About Borked

Borked Provides functionality such that nearly all error types can be passed seemlessly through a Result of almost any type.

This crate does simlar things to other crates, such as failure or error_chain, But although being lovely options for error handling, I was never quite satisfied with how they worked.

Although this crate coud be used to coerse Errors implementing BorkChain it is most useful when passing errors that implement the BorkChain Trait. Borkchain will coerse any error that implements std::error::Error into a BorkChain compatable type. There is also some provide methods for wrapping errors.

§Features

  • BorkChain Trait to allow for a single Rust error type signature to work with any implementation of an error (within bounds)
  • Easy chaining/wrapping of multiple errors with BorkChain
  • Easy creation of new Error Types: Errors need only implement BorkChain Trait, and this crate takes care of all the other implementations needed ie. std::error::Error, std::fmt::Display, …
  • Inter-operability with std::error::Error
  • Convienience macro borked!
  • Transparently wrapping result values with bork_with()
  • Convienience type Borkable for shorter Result types.

§Examples

#[macro_use]
extern crate borked;
use borked::{BaseBork, BorkWith, BorkWithMsg, Borkable};
 
fn borked_base()-> Borkable<()>{
    // Do something useful.
    // Encounter error.
    borked!("this is borked");
    // Never reached in this case.
    Ok(())
}

fn print_bork(){
    let e = borked_base().unwrap_err();
    let e_str = format!("{}", e);
    // Borks are automatically formatted to include 'Error: ' before name.
    assert!(e_str == "Error: this is borked");
}
 
fn borked_base_w_err()-> Borkable<()>{
    // Do something useful.
    // Encounter error.
    borked!(TY => BaseBork);
    // Never reached in this case.
    Ok(())
}
 
fn borked_std_err() -> Borkable<()>{
    // We can still use the '?' operator with std::error::Error's
    let e = u32::from_str_radix("ASDFA", 10)?;
    Ok(())
}
 
fn multiple_borked() -> Borkable<()>{
    let e0 = borked_base().unwrap_err();
    borked!("Borked Twice?", e0);
    Ok(())
}

fn print_multi_bork(){
    let e = multile_borked().unwrap_err();
    let e_str = format!("{}", e);
    // Borks are automatically formatted to display their causes.
    assert!(e_str == "Error: Borked Twice? => Cause: [Error: this is borked]");
}

fn multi_borked_q() ->Borkable<()>{
    // the '?' operator can also obviously be used with Bork errors.
    multiple_borked()?;
    Ok(())
}
 
fn bork_with() -> Borkable<()>{
    // Here if an error is encountered it is wrapped with a bork with the
    // BorkWith trait.
    let i = u32::from_str_radix("ASDFA", 10)
    .bork_with(BaseBork::msg("This is a message!"))?;
    Ok(())
}

Macros§

  • Macro for instantly returning a Result<_, Bork>.

Structs§

  • Basic bork implemntation. Used by borked! macro, But could also be useful for use as a basic error type. (or as a templeate for implementing custom Bork errors.

Traits§

  • Main Bork Trait. See BaseBork for an example implementation.
  • Trait for borks to be instantiated with no message
  • Trait to Wrap Errors with BorkChain Errors. This is implemented by this crate for most types that would concievably need it, and should not need to be implemented explicitly in most cases.
  • Trait for borks that implement a fn to make a new Bork with message dirrectly.

Type Aliases§