Skip to content

Commit

Permalink
Fixed dependencies for miette
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed Sep 10, 2024
1 parent 205973b commit fb3a32e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 3 additions & 3 deletions pest/examples/parens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ 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);

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),
};
}
Expand Down
18 changes: 8 additions & 10 deletions pest/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ impl<R: RuleType> Error<R> {
}
}

#[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)
Expand Down Expand Up @@ -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<R: RuleType>(pub(crate) Error<R>);

impl<R: RuleType> Diagnostic for MietteAdapter<R> {
Expand All @@ -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)
}
};
Expand Down Expand Up @@ -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<u32> = Error::new_from_pos(
ErrorVariant::ParsingError {
positives: vec![1, 2, 3],
Expand Down

0 comments on commit fb3a32e

Please sign in to comment.