-
Notifications
You must be signed in to change notification settings - Fork 136
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
@@ -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> { | ||
|
@@ -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()) | ||
} | ||
} | ||
|
||
impl<'i> Error for BasicParseError<'i> {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you not implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please implement it anyway, calling |
||
|
||
impl<'i, T> From<BasicParseError<'i>> for ParseError<'i, T> { | ||
#[inline] | ||
fn from(this: BasicParseError<'i>) -> ParseError<'i, T> { | ||
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the payload disregarded here? |
||
}) | ||
Comment on lines
+183
to
+186
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put the match outside and avoid 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 {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implement |
||
|
||
/// The owned input for a parser. | ||
pub struct ParserInput<'i> { | ||
tokenizer: Tokenizer<'i>, | ||
|
There was a problem hiding this comment.
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(_)
andAtRuleInvalid(_)
.