Skip to content
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

Add unused variants to ParseError and SyntaxViolation enums #525

Merged
merged 2 commits into from Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/parser.rs
Expand Up @@ -49,11 +49,17 @@ pub type ParseResult<T> = Result<T, ParseError>;
macro_rules! simple_enum_error {
($($name: ident => $description: expr,)+) => {
/// Errors that can occur during parsing.
///
/// This may be extended in the future so exhaustive matching is
/// discouraged with an unused variant.
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum ParseError {
$(
$name,
)+
/// Unused variant enable non-exhaustive matching
#[doc(hidden)]
__FutureProof,
}

impl Error for ParseError {
Expand All @@ -62,6 +68,9 @@ macro_rules! simple_enum_error {
$(
ParseError::$name => $description,
)+
ParseError::__FutureProof => {
unreachable!("Don't abuse the FutureProof!");
}
}
}
}
Expand All @@ -82,8 +91,8 @@ simple_enum_error! {
}

impl fmt::Display for ParseError {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
self.description().fmt(fmt)
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
fmt::Display::fmt(self.description(), f)
}
}

Expand All @@ -96,11 +105,17 @@ impl From<::idna::Errors> for ParseError {
macro_rules! syntax_violation_enum {
($($name: ident => $description: expr,)+) => {
/// Non-fatal syntax violations that can occur during parsing.
///
/// This may be extended in the future so exhaustive matching is
/// discouraged with an unused variant.
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum SyntaxViolation {
$(
$name,
)+
/// Unused variant enable non-exhaustive matching
#[doc(hidden)]
__FutureProof,
}

impl SyntaxViolation {
Expand All @@ -109,6 +124,9 @@ macro_rules! syntax_violation_enum {
$(
SyntaxViolation::$name => $description,
)+
SyntaxViolation::__FutureProof => {
unreachable!("Don't abuse the FutureProof!");
}
}
}
}
Expand All @@ -133,8 +151,8 @@ syntax_violation_enum! {
}

impl fmt::Display for SyntaxViolation {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
self.description().fmt(fmt)
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
fmt::Display::fmt(self.description(), f)
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/unit.rs
Expand Up @@ -512,6 +512,7 @@ fn test_syntax_violation_callback() {
let v = violation.take().unwrap();
assert_eq!(v, ExpectedDoubleSlash);
assert_eq!(v.description(), "expected //");
assert_eq!(v.to_string(), "expected //");
}

#[test]
Expand Down