Skip to content

Instantly share code, notes, and snippets.

@dflemstr
Last active July 19, 2016 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dflemstr/c8bd48a63ffa5c5f4cb454ae07f3eb52 to your computer and use it in GitHub Desktop.
Save dflemstr/c8bd48a63ffa5c5f4cb454ae07f3eb52 to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate quick_error;
quick_error! {
#[derive(Debug)]
pub enum ForeignA {
Foo {
description("foo")
display("foo")
}
}
}
quick_error! {
#[derive(Debug)]
pub enum ForeignB {
Foo {
description("foo")
display("foo")
}
}
}
error_chain! {
types {
Error, ErrorKind, ChainErr, Result;
}
links {}
foreign_links {
ForeignA, A, "a";
ForeignB, B, "b";
}
errors {}
}
fn main() {
let e: Error = Error::from(ForeignA::Foo);
// Let's assume we don't know the cause of e
match *e.kind() {
ErrorKind::Msg(_) => println!("it's a Msg"),
ErrorKind::A => {
if let Error(_, (Some(ref cause), _)) = e {
match cause.downcast_ref::<ForeignA>() {
Some(b) => {
match *b {
ForeignA::Foo => println!("it's a ForeignA::Foo")
}
},
None => unreachable!()
}
} else {
unreachable!()
}
}
ErrorKind::B => {
if let Error(_, (Some(ref cause), _)) = e {
match cause.downcast_ref::<ForeignB>() {
Some(b) => {
match *b {
ForeignB::Foo => println!("it's a ForeignB::Foo")
}
},
None => unreachable!()
}
} else {
unreachable!()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment