Last active
July 19, 2016 19:53
-
-
Save dflemstr/c8bd48a63ffa5c5f4cb454ae07f3eb52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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