3 unstable releases
Uses old Rust 2015
0.2.0 | Jul 29, 2016 |
---|---|
0.1.1 | Jul 29, 2016 |
0.1.0 | Jul 29, 2016 |
#8 in #custom-derive
9KB
149 lines
error_derive
This crate allows to automatically derive Error
implementations
through custom_derive!
.
License
Licensed under MIT license (see LICENSE or http://opensource.org/licenses/MIT).
lib.rs
:
This crate provides macros for deriving some useful methods and traits for Error enums.
All of these macros are designed to be used with the
custom_derive
crate, though they can be used independent of it.
Example
Derive implementation of the standard Error
trait.
This also provides the requisite Display
implementation.
#[macro_use] extern crate custom_derive;
#[macro_use] extern crate error_derive;
custom_derive! {
#[derive(Debug,
ErrorFrom, ErrorDisplay, Error("very bad error"))]
pub enum Error {
Io(io::Error),
Utf8(Utf8Error),
}
}
let io_error = Error::Io(io::Error::last_os_error());
let utf8_error = Error::Utf8(from_utf8(&[0, 142]).unwrap_err());
assert_eq!("very bad error", io_error.description());
assert_eq!("very bad error", utf8_error.description());
assert!(io_error.cause().is_some());
assert!(utf8_error.cause().is_some());
Overview
This crate allows to derive the following traits:
ErrorFrom
, which createsFrom
trait implementations from each enum variants that wraps an innerError
ErrorDisplay
, which creates aDisplay
trait implementation showing the entire causal chain for an errorError
, which implements the standardError
trait with a given description
Error
and ErrorDisplay
are typically derived together, though either can of course be implemented separately.
Usage without custom_derive!
Although designed to be used with custom_derive!
, all of the macros in this crate can be used without it.
The following:
custom_derive! {
#[derive(Debug, ErrorDisplay, Error("just I/O error"))]
pub enum JustIoError { ThisOne(io::Error) }
}
Can also be writtten as:
#[derive(Debug)]
pub enum JustIoError { ThisOne(io::Error) }
ErrorDisplay! { () pub enum JustIoError { ThisOne(io::Error) } }
Error! { ("just I/O error") pub enum JustIoError { ThisOne(io::Error) } }