Skip to content

Commit

Permalink
Add std::error::Error implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
drahnr committed Feb 16, 2019
1 parent bf9561e commit fffd2d7
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ pub enum ConversionError<FatalError> {
Malformed(FatalError),
}

use std::fmt;

impl<FatalError> fmt::Display for ConversionError<FatalError>
where
FatalError: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ConversionError::NoMatch => write!(f, "Rule did not match, failed to convert node"),
ConversionError::Malformed(fatalerror) => write!(f, "Malformed node: {}", fatalerror),
}
}
}

use std::error;

impl<FatalError> error::Error for ConversionError<FatalError>
where
FatalError: error::Error + 'static,
{
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
ConversionError::NoMatch => None,
ConversionError::Malformed(ref fatalerror) => Some(fatalerror),
}
}
}

/// Potentially borrowing conversion from a pest parse tree.
pub trait FromPest<'pest>: Sized {
/// The rule type for the parse tree this type corresponds to.
Expand Down

0 comments on commit fffd2d7

Please sign in to comment.