From 12ee459903d50245570be8594e8aada4408e6139 Mon Sep 17 00:00:00 2001 From: David Kellum Date: Thu, 18 Jul 2019 14:50:53 -0700 Subject: [PATCH 1/2] Add FutureProof variants to ParseError and SyntaxViolation --- src/parser.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/parser.rs b/src/parser.rs index b65857f70..44f95b0f6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -49,11 +49,17 @@ pub type ParseResult = Result; 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 { @@ -62,6 +68,9 @@ macro_rules! simple_enum_error { $( ParseError::$name => $description, )+ + ParseError::__FutureProof => { + unreachable!("Don't abuse the FutureProof!"); + } } } } @@ -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 { @@ -109,6 +124,9 @@ macro_rules! syntax_violation_enum { $( SyntaxViolation::$name => $description, )+ + SyntaxViolation::__FutureProof => { + unreachable!("Don't abuse the FutureProof!"); + } } } } From 8cc477f5d3afee9ff8082fcee0139e54b11c89fc Mon Sep 17 00:00:00 2001 From: David Kellum Date: Mon, 22 Jul 2019 09:23:31 -0700 Subject: [PATCH 2/2] Disambiguate Display::fmt calls for error types --- src/parser.rs | 8 ++++---- tests/unit.rs | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 44f95b0f6..bf973659d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -91,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) } } @@ -151,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) } } diff --git a/tests/unit.rs b/tests/unit.rs index d5e81986a..9918ea316 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -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]