1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
use std::error; use std::fmt; use std::result; pub type Result<T> = result::Result<T, Box<error::Error>>; macro_rules! make_err { ($name:ident) => { #[derive(Debug, Clone)] pub struct $name { msg: String, } impl $name { pub fn new(msg: &str) -> $name { $name { msg: msg.to_string(), } } } impl fmt::Display for $name { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.msg) } } impl error::Error for $name { fn description(&self) -> &str { &self.msg } fn cause(&self) -> Option<&error::Error> { None } } }; } make_err!(InvalidMagicNumberError); make_err!(OutOfRangeError);