Enum pest::error::ErrorVariant
source · pub enum ErrorVariant<R> {
ParsingError {
positives: Vec<R>,
negatives: Vec<R>,
},
CustomError {
message: String,
},
}Expand description
Different kinds of parsing errors.
Variants§
ParsingError
Generated parsing error with expected and unexpected Rules
CustomError
Custom error with a message
Implementations§
source§impl<R: RuleType> ErrorVariant<R>
impl<R: RuleType> ErrorVariant<R>
sourcepub fn message(&self) -> Cow<'_, str>
pub fn message(&self) -> Cow<'_, str>
Returns the error message for ErrorVariant
If ErrorVariant is CustomError, it returns a
Cow::Borrowed reference to message. If ErrorVariant is ParsingError, a
Cow::Owned containing “expected ErrorVariant::ParsingError::positives ErrorVariant::ParsingError::negatives” is returned.
Examples
let variant = ErrorVariant::<()>::CustomError {
message: String::from("unexpected error")
};
println!("{}", variant.message());Examples found in repository?
src/error.rs (line 373)
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
fn message(&self) -> String {
self.variant.message().to_string()
}
fn parsing_error_message<F>(positives: &[R], negatives: &[R], mut f: F) -> String
where
F: FnMut(&R) -> String,
{
match (negatives.is_empty(), positives.is_empty()) {
(false, false) => format!(
"unexpected {}; expected {}",
Error::enumerate(negatives, &mut f),
Error::enumerate(positives, &mut f)
),
(false, true) => format!("unexpected {}", Error::enumerate(negatives, &mut f)),
(true, false) => format!("expected {}", Error::enumerate(positives, &mut f)),
(true, true) => "unknown parsing error".to_owned(),
}
}
fn enumerate<F>(rules: &[R], f: &mut F) -> String
where
F: FnMut(&R) -> String,
{
match rules.len() {
1 => f(&rules[0]),
2 => format!("{} or {}", f(&rules[0]), f(&rules[1])),
l => {
let non_separated = f(&rules[l - 1]);
let separated = rules
.iter()
.take(l - 1)
.map(f)
.collect::<Vec<_>>()
.join(", ");
format!("{}, or {}", separated, non_separated)
}
}
}
pub(crate) fn format(&self) -> String {
let spacing = self.spacing();
let path = self
.path
.as_ref()
.map(|path| format!("{}:", path))
.unwrap_or_default();
let pair = (self.line_col.clone(), &self.continued_line);
if let (LineColLocation::Span(_, end), &Some(ref continued_line)) = pair {
let has_line_gap = end.0 - self.start().0 > 1;
if has_line_gap {
format!(
"{s }--> {p}{ls}:{c}\n\
{s } |\n\
{ls:w$} | {line}\n\
{s } | ...\n\
{le:w$} | {continued_line}\n\
{s } | {underline}\n\
{s } |\n\
{s } = {message}",
s = spacing,
w = spacing.len(),
p = path,
ls = self.start().0,
le = end.0,
c = self.start().1,
line = self.line,
continued_line = continued_line,
underline = self.underline(),
message = self.message()
)
} else {
format!(
"{s }--> {p}{ls}:{c}\n\
{s } |\n\
{ls:w$} | {line}\n\
{le:w$} | {continued_line}\n\
{s } | {underline}\n\
{s } |\n\
{s } = {message}",
s = spacing,
w = spacing.len(),
p = path,
ls = self.start().0,
le = end.0,
c = self.start().1,
line = self.line,
continued_line = continued_line,
underline = self.underline(),
message = self.message()
)
}
} else {
format!(
"{s}--> {p}{l}:{c}\n\
{s} |\n\
{l} | {line}\n\
{s} | {underline}\n\
{s} |\n\
{s} = {message}",
s = spacing,
p = path,
l = self.start().0,
c = self.start().1,
line = self.line,
underline = self.underline(),
message = self.message()
)
}
}
}
impl<R: RuleType> ErrorVariant<R> {
///
/// Returns the error message for [`ErrorVariant`]
///
/// If [`ErrorVariant`] is [`CustomError`], it returns a
/// [`Cow::Borrowed`] reference to [`message`]. If [`ErrorVariant`] is [`ParsingError`], a
/// [`Cow::Owned`] containing "expected [ErrorVariant::ParsingError::positives] [ErrorVariant::ParsingError::negatives]" is returned.
///
/// [`ErrorVariant`]: enum.ErrorVariant.html
/// [`CustomError`]: enum.ErrorVariant.html#variant.CustomError
/// [`ParsingError`]: enum.ErrorVariant.html#variant.ParsingError
/// [`Cow::Owned`]: https://doc.rust-lang.org/std/borrow/enum.Cow.html#variant.Owned
/// [`Cow::Borrowed`]: https://doc.rust-lang.org/std/borrow/enum.Cow.html#variant.Borrowed
/// [`message`]: enum.ErrorVariant.html#variant.CustomError.field.message
/// # Examples
///
/// ```
/// # use pest::error::ErrorVariant;
/// let variant = ErrorVariant::<()>::CustomError {
/// message: String::from("unexpected error")
/// };
///
/// println!("{}", variant.message());
pub fn message(&self) -> Cow<'_, str> {
match self {
ErrorVariant::ParsingError {
ref positives,
ref negatives,
} => Cow::Owned(Error::parsing_error_message(positives, negatives, |r| {
format!("{:?}", r)
})),
ErrorVariant::CustomError { ref message } => Cow::Borrowed(message),
}
}
}
impl<R: RuleType> fmt::Display for Error<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.format())
}
}
impl<R: RuleType> fmt::Display for ErrorVariant<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorVariant::ParsingError { .. } => write!(f, "parsing error: {}", self.message()),
ErrorVariant::CustomError { .. } => write!(f, "{}", self.message()),
}
}Trait Implementations§
source§impl<R: Clone> Clone for ErrorVariant<R>
impl<R: Clone> Clone for ErrorVariant<R>
source§fn clone(&self) -> ErrorVariant<R>
fn clone(&self) -> ErrorVariant<R>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moresource§impl<R: Debug> Debug for ErrorVariant<R>
impl<R: Debug> Debug for ErrorVariant<R>
source§impl<R: RuleType> Display for ErrorVariant<R>
impl<R: RuleType> Display for ErrorVariant<R>
source§impl<R> Error for ErrorVariant<R>where
Self: Debug + Display,
impl<R> Error for ErrorVariant<R>where
Self: Debug + Display,
1.30.0 · source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
The lower-level source of this error, if any. Read more
1.0.0 · source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
source§impl<R: Hash> Hash for ErrorVariant<R>
impl<R: Hash> Hash for ErrorVariant<R>
source§impl<R: PartialEq> PartialEq<ErrorVariant<R>> for ErrorVariant<R>
impl<R: PartialEq> PartialEq<ErrorVariant<R>> for ErrorVariant<R>
source§fn eq(&self, other: &ErrorVariant<R>) -> bool
fn eq(&self, other: &ErrorVariant<R>) -> bool
This method tests for
self and other values to be equal, and is used
by ==.