|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 | 11 | use std::fmt;
|
| 12 | +use std::iter::repeat; |
12 | 13 |
|
13 | 14 | use syntax;
|
14 | 15 |
|
15 | 16 | /// An error that occurred during parsing or compiling a regular expression.
|
16 |
| -#[derive(Clone, Debug, PartialEq)] |
| 17 | +#[derive(Clone, PartialEq)] |
17 | 18 | pub enum Error {
|
18 | 19 | /// A syntax error.
|
19 | 20 | Syntax(String),
|
@@ -56,6 +57,34 @@ impl fmt::Display for Error {
|
56 | 57 | }
|
57 | 58 | }
|
58 | 59 |
|
| 60 | +// We implement our own Debug implementation so that we show nicer syntax |
| 61 | +// errors when people use `Regex::new(...).unwrap()`. It's a little weird, |
| 62 | +// but the `Syntax` variant is already storing a `String` anyway, so we might |
| 63 | +// as well format it nicely. |
| 64 | +impl fmt::Debug for Error { |
| 65 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 66 | + match *self { |
| 67 | + Error::Syntax(ref err) => { |
| 68 | + let hr: String = repeat('~').take(79).collect(); |
| 69 | + try!(writeln!(f, "Syntax(")); |
| 70 | + try!(writeln!(f, "{}", hr)); |
| 71 | + try!(writeln!(f, "{}", err)); |
| 72 | + try!(writeln!(f, "{}", hr)); |
| 73 | + try!(write!(f, ")")); |
| 74 | + Ok(()) |
| 75 | + } |
| 76 | + Error::CompiledTooBig(limit) => { |
| 77 | + f.debug_tuple("CompiledTooBig") |
| 78 | + .field(&limit) |
| 79 | + .finish() |
| 80 | + } |
| 81 | + Error::__Nonexhaustive => { |
| 82 | + f.debug_tuple("__Nonexhaustive").finish() |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
59 | 88 | impl From<syntax::Error> for Error {
|
60 | 89 | fn from(err: syntax::Error) -> Error {
|
61 | 90 | Error::Syntax(err.to_string())
|
|
0 commit comments