Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove trait bound from Error type #694

Merged
merged 3 commits into from Aug 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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