6 releases (breaking)

✓ Uses Rust 2018 edition

0.5.0 Nov 28, 2019
0.4.0 Nov 5, 2019
0.3.0 Sep 8, 2019
0.2.0 Jul 19, 2019
0.1.1 Jul 18, 2019
Download history 3/week @ 2019-08-15 20/week @ 2019-08-22 16/week @ 2019-08-29 23/week @ 2019-09-05 20/week @ 2019-09-12 68/week @ 2019-09-19 12/week @ 2019-09-26 12/week @ 2019-10-03 17/week @ 2019-10-17 16/week @ 2019-10-24 24/week @ 2019-10-31 8/week @ 2019-11-07 6/week @ 2019-11-14 13/week @ 2019-11-21

76 downloads per month
Used in 1 crate

MIT license

45KB
1.5K SLoC

Evitable

Crate Documentation Build Status

Evitable is a library for easily creating and using custom error types in libraries. It's intended to make the creation of custom domain specific error types easier, as well as reduce the noise related to converting from underlying errors to domain specific errors, while keeping the underlying error as source(). This crate by default has a feature called derive enabled, which enables deriving ErrorContexts.

Quick example

This example showcases a typical usecase of calling some API that (pretends) to read a file, only to fail, and then converts the error into a domain specific error.

use evitable::*;

// Typically, this is in another file
mod error {
  use super::*;

  #[evitable]
  pub enum Context {
    #[evitable(description = "Io error", from = std::io::Error)]
    Io,

    #[evitable(description("Invalid token. Expected {}, was {}.", expected, actual))]
    InvalidToken {
       expected: String,
       actual: String,
    },
  }
}

use error::*;

// pretend token type
#[derive(Debug)]
pub enum Token {
  EndOfFile,
}

fn read_file() -> std::result::Result<String, std::io::Error> {
  // we're pretending to read a file here
  Err(std::io::Error::from(std::io::ErrorKind::NotFound))
}

// main function
fn parse_file() -> Result<Token> {
  let content = read_file()?;
  ensure!(content == "EOF", Context::InvalidToken {
    expected: "EOF".to_owned(),
    actual: content,
  });

  Ok(Token::EndOfFile)
}

let result = parse_file();
let err = result.unwrap_err();
assert_eq!(err.kind(), evitable_context::ErrorKind::Io);

Dependencies

~0.6–0.9MB
~21K SLoC