diff --git a/pest/Cargo.toml b/pest/Cargo.toml index 3b90c9e4..70b86fb7 100644 --- a/pest/Cargo.toml +++ b/pest/Cargo.toml @@ -16,19 +16,18 @@ rust-version = "1.80" [features] default = ["std", "memchr"] # Implements `std::error::Error` for the `Error` type -std = ["ucd-trie/std", "dep:thiserror"] +std = ["ucd-trie/std"] # Enables the `to_json` function for `Pair` and `Pairs` pretty-print = ["dep:serde", "dep:serde_json"] # Enable const fn constructor for `PrecClimber` const_prec_climber = [] # Enable miette error -miette-error = ["std", "pretty-print", "dep:miette", "dep:thiserror"] +miette-error = ["std", "pretty-print", "dep:miette"] [dependencies] ucd-trie = { version = "0.1.5", default-features = false } serde = { version = "1.0.145", optional = true } serde_json = { version = "1.0.85", optional = true } -thiserror = { version = "2", optional = true } memchr = { version = "2", optional = true } miette = { version = "7.2.0", optional = true, features = ["fancy"] } diff --git a/pest/src/error.rs b/pest/src/error.rs index 5b3e194e..b2087a54 100644 --- a/pest/src/error.rs +++ b/pest/src/error.rs @@ -29,7 +29,6 @@ use crate::RuleType; /// Parse-related error type. #[derive(Clone, Debug, Eq, Hash, PartialEq)] -#[cfg_attr(feature = "std", derive(thiserror::Error))] pub struct Error { /// Variant of the error pub variant: ErrorVariant, @@ -43,9 +42,11 @@ pub struct Error { parse_attempts: Option>, } +#[cfg(feature = "std")] +impl core::error::Error for Error {} + /// Different kinds of parsing errors. #[derive(Clone, Debug, Eq, Hash, PartialEq)] -#[cfg_attr(feature = "std", derive(thiserror::Error))] pub enum ErrorVariant { /// Generated parsing error with expected and unexpected `Rule`s ParsingError { @@ -61,6 +62,9 @@ pub enum ErrorVariant { }, } +#[cfg(feature = "std")] +impl std::error::Error for ErrorVariant {} + /// Where an `Error` has occurred. #[derive(Clone, Debug, Eq, Hash, PartialEq)] pub enum InputLocation { @@ -737,6 +741,7 @@ fn visualize_whitespace(input: &str) -> String { #[cfg(feature = "miette-error")] mod miette_adapter { use alloc::string::ToString; + use core::fmt; use std::boxed::Box; use crate::error::LineColLocation; @@ -745,8 +750,7 @@ mod miette_adapter { use miette::{Diagnostic, LabeledSpan, SourceCode}; - #[derive(thiserror::Error, Debug)] - #[error("Failure to parse at {:?}", self.0.line_col)] + #[derive(Debug)] pub(crate) struct MietteAdapter(pub(crate) Error); impl Diagnostic for MietteAdapter { @@ -769,10 +773,23 @@ mod miette_adapter { Some(Box::new(std::iter::once(span))) } - fn help<'a>(&'a self) -> Option> { + fn help<'a>(&'a self) -> Option> { Some(Box::new(self.0.message())) } } + + impl fmt::Display for MietteAdapter { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Failure to parse at {:?}", self.0.line_col) + } + } + + impl std::error::Error for MietteAdapter + where + R: RuleType, + Self: fmt::Debug + fmt::Display, + { + } } #[cfg(test)]