Skip to content

Implement std::error::Error for errors #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
33 changes: 33 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use cow_rc_str::CowRcStr;
use smallvec::SmallVec;
use std::error::Error;
use std::fmt;
use std::ops::BitOr;
use std::ops::Range;
use tokenizer::{SourceLocation, SourcePosition, Token, Tokenizer};
Expand Down Expand Up @@ -53,6 +55,18 @@ pub enum BasicParseErrorKind<'i> {
QualifiedRuleInvalid,
}

impl<'i> BasicParseErrorKind<'i> {
fn description(&self) -> &'static str {
match self {
BasicParseErrorKind::UnexpectedToken(_) => "Unexpected token",
BasicParseErrorKind::EndOfInput => "End of input",
BasicParseErrorKind::AtRuleInvalid(_) => "Invalid @ rule",
BasicParseErrorKind::AtRuleBodyInvalid => "Invalid @ rule body",
BasicParseErrorKind::QualifiedRuleInvalid => "Invalid qualified rule",
}
}
}

/// The funamental parsing errors that can be triggered by built-in parsing routines.
#[derive(Clone, Debug, PartialEq)]
pub struct BasicParseError<'i> {
Expand All @@ -62,6 +76,14 @@ pub struct BasicParseError<'i> {
pub location: SourceLocation,
}

impl<'i> fmt::Display for BasicParseError<'i> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{}", self.kind.description())
Copy link
Contributor

Choose a reason for hiding this comment

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

This won't print enough information, i.e. we end up not printing the payloads of UnexpectedToken(_) and AtRuleInvalid(_).

}
}

impl<'i> Error for BasicParseError<'i> {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you not implement description here?

Copy link
Author

Choose a reason for hiding this comment

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

The documentation for Error says: "This method is soft-deprecated. Although using it won’t cause compilation warning, new code should use Display instead and new impls can omit it."

Copy link
Contributor

Choose a reason for hiding this comment

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

Please implement it anyway, calling BasicParseErrorKind::description.


impl<'i, T> From<BasicParseError<'i>> for ParseError<'i, T> {
#[inline]
fn from(this: BasicParseError<'i>) -> ParseError<'i, T> {
Expand Down Expand Up @@ -156,6 +178,17 @@ impl<'i, T> ParseError<'i, T> {
}
}

impl<'i, T> fmt::Display for ParseError<'i, T> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{}", match &self.kind {
ParseErrorKind::Basic(basic_kind) => basic_kind.description(),
ParseErrorKind::Custom(_) => "Custom error",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is the payload disregarded here?

})
Comment on lines +183 to +186
Copy link
Contributor

Choose a reason for hiding this comment

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

Please put the match outside and avoid write!.

match &self.kind {
    ParseErrorKind::Basic(basic) => basic.fmt(formatter),
    ParseErrorKind::Custom(_) => "custom error".fmt(formatter),
}

}
}

impl<'i, T> Error for ParseError<'i, T> where T: fmt::Debug {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Implement description here.


/// The owned input for a parser.
pub struct ParserInput<'i> {
tokenizer: Tokenizer<'i>,
Expand Down