extern crate errer;
#[macro_use] extern crate errer_derive;
use errer::{res, main_res, IntoErrorContext, ErrorContext};
use std::fmt;
use std::io;
#[derive(Debug, Eq, PartialEq)]
struct Dog;
impl fmt::Display for Dog {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "bark")
}
}
trait Item: fmt::Display {}
#[derive(Debug, Eq, PartialEq)]
struct StringItem;
impl fmt::Display for StringItem {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "string")
}
}
impl Item for StringItem {}
#[derive(Debug, Eq, PartialEq, Errer)]
#[errer(from, display = "aaaaaaaaaaaaaaaaaahuehueh {} heheh")]
enum Heck<X: Item> {
#[errer(display = "Heckin error: is cat")]
Cat,
#[errer(display = "Heckin error: is cat with a {}", 0)]
CatWithDog(Dog),
#[errer(display = "Heckin heck")]
CatWithDogAndBool(Dog, bool),
#[errer(display = "Heckin error: is cat with \"{}\"", x)]
CatWithItem {x: X}
}
#[derive(Debug, Eq, PartialEq, Errer)]
#[errer(display = "{} hab ben hekked by {}!", 0, 1)]
struct Hecked<'a, H: fmt::Display>(Dog, &'a H);
#[derive(Debug, Eq, PartialEq, Errer)]
#[errer(from, context)]
enum CtxTest {
#[errer(display = "AAAAAAAAAAAAAAAA")]
NumWrong (bool, u32),
#[errer(context=NumWrongTwo)]
NumWrong2 (bool)
}
#[derive(Debug, Eq, PartialEq, Errer)]
#[errer(context=TestStructCtx)]
struct CtxTestStruct (#[errer(from)] bool, u32);
#[derive(Debug, Errer)]
struct StdTest (#[errer(std, from)] io::Error);
#[derive(Debug, Errer)]
enum StdEnumTest {
#[errer(std, from)]
X (io::Error),
#[errer(std, from)]
Y {from: std::ffi::NulError}
}
#[test]
fn enum_display() {
let hecks = vec![Heck::Cat, Heck::CatWithDog(Dog), Heck::CatWithDogAndBool(Dog, false), Heck::CatWithItem {x: StringItem}];
for x in hecks {
println!("{}", x);
}
println!("{}", CtxTest::NumWrong (false, 0));
}
#[test]
fn from_tests() {
assert_eq!(Heck::<StringItem>::CatWithDog(Dog), Dog.into());
}
#[test]
fn struct_display() {
println!("{}", Hecked::<Heck<StringItem>>(Dog, &Heck::Cat));
}
fn err<E>(e: E) -> Result<(), E> {
Err(e)
}
#[test]
fn context() {
assert_eq!(CtxTest::NumWrong (true, 5), NumWrong (5).into_target(true));
assert_eq!(err(CtxTest::NumWrong (true, 5)), err(true).context(NumWrong(5)));
assert_eq!(err(CtxTest::NumWrong2 (true)), err(true).context_with(|| NumWrongTwo));
assert_eq!(err(CtxTestStruct (true, 5)), err(true).context(TestStructCtx(5)));
}
mod macro_test {
use super::*;
res!(Dog);
fn main_res() -> Res<()> {
Err(Dog)
}
main_res!(main_res);
#[test]
fn main_test() { assert_eq!((), main()) }
}
mod macro_test3 {
use super::*;
res!(Hecked<'a, H>);
fn main_res<'a>() -> Res<'a, Heck<StringItem>, ()> {
Err(Hecked(Dog, &Heck::Cat))
}
main_res!(main_res);
#[test]
fn main_test() { assert_eq!((), main()) }
}