Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions pest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
27 changes: 22 additions & 5 deletions pest/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R> {
/// Variant of the error
pub variant: ErrorVariant<R>,
Expand All @@ -43,9 +42,11 @@ pub struct Error<R> {
parse_attempts: Option<ParseAttempts<R>>,
}

#[cfg(feature = "std")]
impl<R: RuleType> core::error::Error for Error<R> {}
Comment on lines +45 to +46
Copy link
Contributor Author

@CosmicHorrorDev CosmicHorrorDev Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh awkward mix of std and core here, but worth asking. Would you be fine bumping the MSRV up to 1.81.0 for core::error::Error? It's only one version up and I believe debian's latest release is 1.84


/// Different kinds of parsing errors.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum ErrorVariant<R> {
/// Generated parsing error with expected and unexpected `Rule`s
ParsingError {
Expand All @@ -61,6 +62,9 @@ pub enum ErrorVariant<R> {
},
}

#[cfg(feature = "std")]
impl<R: RuleType> std::error::Error for ErrorVariant<R> {}

/// Where an `Error` has occurred.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum InputLocation {
Expand Down Expand Up @@ -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;
Expand All @@ -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<R: RuleType>(pub(crate) Error<R>);

impl<R: RuleType> Diagnostic for MietteAdapter<R> {
Expand All @@ -769,10 +773,23 @@ mod miette_adapter {
Some(Box::new(std::iter::once(span)))
}

fn help<'a>(&'a self) -> Option<Box<dyn core::fmt::Display + 'a>> {
fn help<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
Some(Box::new(self.0.message()))
}
}

impl<R: RuleType> fmt::Display for MietteAdapter<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Failure to parse at {:?}", self.0.line_col)
}
}

impl<R> std::error::Error for MietteAdapter<R>
where
R: RuleType,
Self: fmt::Debug + fmt::Display,
{
}
}

#[cfg(test)]
Expand Down