diff --git a/pest/Cargo.toml b/pest/Cargo.toml index 238e905b..a56a5343 100644 --- a/pest/Cargo.toml +++ b/pest/Cargo.toml @@ -21,6 +21,8 @@ std = ["ucd-trie/std", "dep:thiserror"] 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"] [dependencies] ucd-trie = { version = "0.1.5", default-features = false } diff --git a/pest/examples/parens.rs b/pest/examples/parens.rs index 689d1623..f91cb53b 100644 --- a/pest/examples/parens.rs +++ b/pest/examples/parens.rs @@ -69,7 +69,7 @@ fn main() { line.pop(); let parsed = ParenParser::parse(Rule::expr, &line); - #[cfg(feature = "miette")] + #[cfg(feature = "miette-error")] let parsed = parsed .map_err(Error::into_miette) .map_err(miette::Report::from); @@ -77,10 +77,10 @@ fn main() { match parsed { Ok(pairs) => println!("{:?}", expr(pairs)), // To print pest errors, use Display formatting. - #[cfg(not(feature = "miette"))] + #[cfg(not(feature = "miette-error"))] Err(e) => eprintln!("\n{}", e), // To print miette errors, use Debug formatting. - #[cfg(feature = "miette")] + #[cfg(feature = "miette-error")] Err(e) => eprintln!("\n{:?}", e), }; } diff --git a/pest/src/error.rs b/pest/src/error.rs index e9b48f61..0142b342 100644 --- a/pest/src/error.rs +++ b/pest/src/error.rs @@ -672,7 +672,7 @@ impl Error { } } - #[cfg(feature = "miette")] + #[cfg(feature = "miette-error")] /// Turns an error into a [miette](crates.io/miette) Diagnostic. pub fn into_miette(self) -> impl ::miette::Diagnostic { miette_adapter::MietteAdapter(self) @@ -734,19 +734,19 @@ fn visualize_whitespace(input: &str) -> String { input.to_owned().replace('\r', "␍").replace('\n', "␊") } -#[cfg(feature = "miette")] +#[cfg(feature = "miette-error")] mod miette_adapter { use alloc::string::ToString; use std::boxed::Box; use crate::error::LineColLocation; - use super::{Error, InputLocation, RuleType}; + use super::{Error, RuleType}; use miette::{Diagnostic, LabeledSpan, SourceCode}; #[derive(thiserror::Error, Debug)] - #[error("Failure to parse at {}", self.0.line_col)] + #[error("Failure to parse at {:?}", self.0.line_col)] pub(crate) struct MietteAdapter(pub(crate) Error); impl Diagnostic for MietteAdapter { @@ -758,8 +758,8 @@ mod miette_adapter { let message = self.0.variant.message().to_string(); let (offset, length) = match self.0.line_col { - LineColLocation::Pos((r, c)) => (c - 1, 1), - LineColLocation::Span((start_r, start_c), (end_r, end_c)) => { + LineColLocation::Pos((_, c)) => (c - 1, 1), + LineColLocation::Span((_, start_c), (_, end_c)) => { (start_c - 1, end_c - start_c + 1) } }; @@ -1152,13 +1152,11 @@ mod tests { ); } - #[cfg(feature = "miette")] + #[cfg(feature = "miette-error")] #[test] fn miette_error() { - use miette::{Diagnostic, LabeledSpan, MietteDiagnostic}; - let input = "abc\ndef"; - let pos = position::Position::new(input, 4).unwrap(); + let pos = Position::new(input, 4).unwrap(); let error: Error = Error::new_from_pos( ErrorVariant::ParsingError { positives: vec![1, 2, 3],