Skip to content

Commit

Permalink
Merge pull request #694 from pest-parser/CAD97-patch-1
Browse files Browse the repository at this point in the history
Remove trait bound from Error type
  • Loading branch information
CAD97 committed Aug 16, 2022
2 parents 8d1f7b8 + 5aa9ff2 commit 0640862
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions pest/src/error.rs
Expand Up @@ -16,6 +16,7 @@ use alloc::string::String;
use alloc::string::ToString;
use alloc::vec::Vec;
use core::cmp;
use core::fmt;
use core::mem;

use crate::position::Position;
Expand All @@ -25,8 +26,7 @@ use crate::RuleType;
/// Parse-related error type.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
#[cfg_attr(feature = "std", error("{}", self.format()))]
pub struct Error<R: RuleType> {
pub struct Error<R> {
/// Variant of the error
pub variant: ErrorVariant<R>,
/// Location within the input string
Expand All @@ -41,17 +41,15 @@ pub struct Error<R: RuleType> {
/// Different kinds of parsing errors.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum ErrorVariant<R: RuleType> {
pub enum ErrorVariant<R> {
/// Generated parsing error with expected and unexpected `Rule`s
#[cfg_attr(feature = "std", error("parsing error: {}", self.message()))]
ParsingError {
/// Positive attempts
positives: Vec<R>,
/// Negative attempts
negatives: Vec<R>,
},
/// Custom error with a message
#[cfg_attr(feature = "std", error("{}", self.message()))]
CustomError {
/// Short explanation
message: String,
Expand Down Expand Up @@ -520,6 +518,21 @@ impl<R: RuleType> ErrorVariant<R> {
}
}

impl<R: RuleType> fmt::Display for Error<R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.format())
}
}

impl<R: RuleType> fmt::Display for ErrorVariant<R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ErrorVariant::ParsingError { .. } => write!(f, "parsing error: {}", self.message()),
ErrorVariant::CustomError { .. } => write!(f, "{}", self.message()),
}
}
}

fn visualize_whitespace(input: &str) -> String {
input.to_owned().replace('\r', "␍").replace('\n', "␊")
}
Expand Down

0 comments on commit 0640862

Please sign in to comment.