Skip to content

Commit 94215af

Browse files
author
bors-servo
authored
Auto merge of #525 - dekellum:future-proof, r=SimonSapin
Add unused variants to ParseError and SyntaxViolation enums Per discussion in #498 (comment) , this adds a hidden unused variant to each of `ParseError` and `SyntaxViolation` enums so as to make future additions non-breaking changes. This is the long standing manual way (e.g. originally used by`std::io::ErrorKind`), given lack of a stable feature and acceptable rust MSRV with rust-lang/rust#44109. Also these types already implemented `Display` but having been burnt with `Debug` vs `Display` versions of the `fmt` function, I also disambiguated paths in these `Display` implementations. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/525) <!-- Reviewable:end -->
2 parents 0ab166e + 8cc477f commit 94215af

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/parser.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,17 @@ pub type ParseResult<T> = Result<T, ParseError>;
4949
macro_rules! simple_enum_error {
5050
($($name: ident => $description: expr,)+) => {
5151
/// Errors that can occur during parsing.
52+
///
53+
/// This may be extended in the future so exhaustive matching is
54+
/// discouraged with an unused variant.
5255
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
5356
pub enum ParseError {
5457
$(
5558
$name,
5659
)+
60+
/// Unused variant enable non-exhaustive matching
61+
#[doc(hidden)]
62+
__FutureProof,
5763
}
5864

5965
impl Error for ParseError {
@@ -62,6 +68,9 @@ macro_rules! simple_enum_error {
6268
$(
6369
ParseError::$name => $description,
6470
)+
71+
ParseError::__FutureProof => {
72+
unreachable!("Don't abuse the FutureProof!");
73+
}
6574
}
6675
}
6776
}
@@ -82,8 +91,8 @@ simple_enum_error! {
8291
}
8392

8493
impl fmt::Display for ParseError {
85-
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
86-
self.description().fmt(fmt)
94+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
95+
fmt::Display::fmt(self.description(), f)
8796
}
8897
}
8998

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

106121
impl SyntaxViolation {
@@ -109,6 +124,9 @@ macro_rules! syntax_violation_enum {
109124
$(
110125
SyntaxViolation::$name => $description,
111126
)+
127+
SyntaxViolation::__FutureProof => {
128+
unreachable!("Don't abuse the FutureProof!");
129+
}
112130
}
113131
}
114132
}
@@ -133,8 +151,8 @@ syntax_violation_enum! {
133151
}
134152

135153
impl fmt::Display for SyntaxViolation {
136-
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
137-
self.description().fmt(fmt)
154+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
155+
fmt::Display::fmt(self.description(), f)
138156
}
139157
}
140158

tests/unit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ fn test_syntax_violation_callback() {
512512
let v = violation.take().unwrap();
513513
assert_eq!(v, ExpectedDoubleSlash);
514514
assert_eq!(v.description(), "expected //");
515+
assert_eq!(v.to_string(), "expected //");
515516
}
516517

517518
#[test]

0 commit comments

Comments
 (0)