diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-03-30 16:42:52 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-03-30 16:42:52 +0100 |
| commit | f683bba4004cc07f9ac91d5d99a6bab76f335dba (patch) | |
| tree | c67ca3d00849838c4de56a9ffa13813f640f9707 /src/lib.rs | |
| parent | 10994ebac34d027fb1937f72183859142b1b6180 (diff) | |
Create error_type! macro ✔
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 52 |
1 files changed, 15 insertions, 37 deletions
@@ -38,20 +38,21 @@ //! exporter.export(&document, &mut file).unwrap(); //! ``` -use std::fmt::{self, Display, Debug, Formatter}; use crate::syntax::SyntaxTree; use crate::parsing::{Tokens, Parser, ParseError}; use crate::doc::{Document, Style}; use crate::font::FontProvider; use crate::engine::{Engine, TypesetError}; +#[macro_use] +mod error; +mod utility; pub mod doc; pub mod engine; pub mod export; pub mod font; pub mod parsing; pub mod syntax; -mod utility; /// Transforms source code into typesetted documents. @@ -120,43 +121,20 @@ pub enum Error { Typeset(TypesetError), } -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - match self { - Error::Parse(err) => Some(err), - Error::Typeset(err) => Some(err), - } - } -} - -impl Display for Error { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - match self { - Error::Parse(err) => write!(f, "parse error: {}", err), - Error::Typeset(err) => write!(f, "typeset error: {}", err), - } - } -} - -impl Debug for Error { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - Display::fmt(self, f) - } -} - -impl From<ParseError> for Error { - fn from(err: ParseError) -> Error { - Error::Parse(err) - } +error_type! { + err: Error, + show: f => match err { + Error::Parse(e) => write!(f, "parse error: {}", e), + Error::Typeset(e) => write!(f, "typeset error: {}", e), + }, + source: match err { + Error::Parse(e) => Some(e), + Error::Typeset(e) => Some(e), + }, + from: (ParseError, Error::Parse(err)), + from: (TypesetError, Error::Typeset(err)), } -impl From<TypesetError> for Error { - fn from(err: TypesetError) -> Error { - Error::Typeset(err) - } -} - - #[cfg(test)] mod test { use std::fs::File; |
