From d35fc61d3fd94861debc69cf7ccce3d04c834893 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Mon, 15 Aug 2022 18:42:20 -0500 Subject: [PATCH 1/3] Remove trait bound from Error type --- pest/src/error.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pest/src/error.rs b/pest/src/error.rs index c2ab8066..c199ad98 100644 --- a/pest/src/error.rs +++ b/pest/src/error.rs @@ -26,7 +26,7 @@ use crate::RuleType; #[derive(Clone, Debug, Eq, Hash, PartialEq)] #[cfg_attr(feature = "std", derive(thiserror::Error))] #[cfg_attr(feature = "std", error("{}", self.format()))] -pub struct Error { +pub struct Error { /// Variant of the error pub variant: ErrorVariant, /// Location within the input string @@ -41,7 +41,7 @@ pub struct Error { /// Different kinds of parsing errors. #[derive(Clone, Debug, Eq, Hash, PartialEq)] #[cfg_attr(feature = "std", derive(thiserror::Error))] -pub enum ErrorVariant { +pub enum ErrorVariant { /// Generated parsing error with expected and unexpected `Rule`s #[cfg_attr(feature = "std", error("parsing error: {}", self.message()))] ParsingError { From 8fbf5da1be8522c5fd8a21bb632da36d92cb5b1f Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Mon, 15 Aug 2022 20:05:54 -0500 Subject: [PATCH 2/3] Return manual Error Display impl --- pest/src/error.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pest/src/error.rs b/pest/src/error.rs index c199ad98..cec778b1 100644 --- a/pest/src/error.rs +++ b/pest/src/error.rs @@ -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; @@ -25,7 +26,6 @@ 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 { /// Variant of the error pub variant: ErrorVariant, @@ -43,7 +43,6 @@ pub struct Error { #[cfg_attr(feature = "std", derive(thiserror::Error))] pub enum ErrorVariant { /// Generated parsing error with expected and unexpected `Rule`s - #[cfg_attr(feature = "std", error("parsing error: {}", self.message()))] ParsingError { /// Positive attempts positives: Vec, @@ -51,7 +50,6 @@ pub enum ErrorVariant { negatives: Vec, }, /// Custom error with a message - #[cfg_attr(feature = "std", error("{}", self.message()))] CustomError { /// Short explanation message: String, @@ -520,6 +518,18 @@ impl ErrorVariant { } } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.format()) + } +} + +impl fmt::Display for ErrorVariant { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.message()) + } +} + fn visualize_whitespace(input: &str) -> String { input.to_owned().replace('\r', "␍").replace('\n', "␊") } From 5aa9ff20b65c5cb42de2e55df439f6a13afbd856 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Mon, 15 Aug 2022 20:07:39 -0500 Subject: [PATCH 3/3] Restore `parsing error:` error introducer --- pest/src/error.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pest/src/error.rs b/pest/src/error.rs index cec778b1..a83e23a4 100644 --- a/pest/src/error.rs +++ b/pest/src/error.rs @@ -526,7 +526,10 @@ impl fmt::Display for Error { impl fmt::Display for ErrorVariant { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.message()) + match self { + ErrorVariant::ParsingError { .. } => write!(f, "parsing error: {}", self.message()), + ErrorVariant::CustomError { .. } => write!(f, "{}", self.message()), + } } }