From f683bba4004cc07f9ac91d5d99a6bab76f335dba Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 30 Mar 2019 16:42:52 +0100 Subject: =?UTF-8?q?Create=20error=5Ftype!=20macro=20=E2=9C=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/error.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/error.rs (limited to 'src/error.rs') diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 00000000..cfdb1f0e --- /dev/null +++ b/src/error.rs @@ -0,0 +1,38 @@ +/// Create an error type. +macro_rules! error_type { + ( + $var:ident: $err:ident, + $(res: $res:ident,)* + show: $f:ident => $show:expr, + $(source: $source:expr,)* + $(from: ($from:path, $conv:expr),)* + ) => { + $(type $res = std::result::Result;)* + + impl std::fmt::Display for $err { + fn fmt(&self, $f: &mut std::fmt::Formatter) -> std::fmt::Result { + let $var = self; + $show + } + } + + impl std::fmt::Debug for $err { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + std::fmt::Display::fmt(self, f) + } + } + + impl std::error::Error for $err { + $(fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + let $var = self; + $source + })* + } + + $(impl From<$from> for $err { + fn from($var: $from) -> $err { + $conv + } + })* + }; +} -- cgit v1.2.3