From b6d52b2616ab0a512524ad885751b129d8ad82a2 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 30 Oct 2015 15:22:11 +0100 Subject: [PATCH] Implement: Error, Debug, Display for ParserError --- src/storage/parser.rs | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/storage/parser.rs b/src/storage/parser.rs index 4a345636..c4f009c6 100644 --- a/src/storage/parser.rs +++ b/src/storage/parser.rs @@ -1,4 +1,7 @@ use regex::Regex; +use std::error::Error; +use std::fmt::{Debug, Display, Formatter}; +use std::fmt; use super::file::*; @@ -29,6 +32,48 @@ impl ParserError { } } +impl Error for ParserError { + + fn description(&self) -> &str { + &self.summary[..] + } + + fn cause(&self) -> Option<&Error> { + None + } + +} + +impl Debug for ParserError { + + fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + write!(fmt, "ParserError: {}\n\n", self.summary); + + if let Some(ref e) = self.explanation { + write!(fmt, "{}\n\n", e); + } + + write!(fmt, "On position {}\nin\n{}", self.index, self.parsertext); + Ok(()) + } + +} + +impl Display for ParserError { + + fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + write!(fmt, "ParserError: {}", self.summary); + + if let Some(ref e) = self.explanation { + write!(fmt, "\n\n{}", e); + } + + Ok(()) + } + +} + + pub trait FileHeaderParser : Sized { fn new(spec: &FileHeaderSpec) -> Self; fn read(&self, string: Option) -> Result;