|
1 |
| -use derive_more::Display; |
| 1 | +use std::fmt; |
2 | 2 |
|
3 | 3 | use crate::tokenizer::TokenType;
|
4 | 4 |
|
5 | 5 | /// A error occurring during URL pattern construction, or matching.
|
6 |
| -#[derive(Display)] |
| 6 | +#[derive(Debug)] |
7 | 7 | pub enum Error {
|
8 |
| - #[display(fmt = "a relative input without a base URL is not valid")] |
9 | 8 | BaseUrlRequired,
|
10 |
| - |
11 |
| - #[display( |
12 |
| - fmt = "specifying both an init object, and a separate base URL is not valid" |
13 |
| - )] |
14 | 9 | BaseUrlWithInit,
|
15 |
| - |
16 |
| - #[display(fmt = "tokenizer error: {_0} (at char {_1})")] |
17 | 10 | Tokenizer(TokenizerError, usize),
|
18 |
| - |
19 |
| - #[display(fmt = "parser error: {_0}")] |
20 | 11 | Parser(ParserError),
|
21 |
| - |
22 | 12 | Url(url::ParseError),
|
23 |
| - |
24 |
| - #[display(fmt = "regexp error")] |
25 | 13 | RegExp(()),
|
26 | 14 | }
|
27 | 15 |
|
28 |
| -impl std::error::Error for Error {} |
29 |
| - |
30 |
| -impl std::fmt::Debug for Error { |
31 |
| - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
32 |
| - std::fmt::Display::fmt(self, f) |
| 16 | +impl fmt::Display for Error { |
| 17 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 18 | + match self { |
| 19 | + Error::BaseUrlRequired => { |
| 20 | + f.write_str("a relative input without a base URL is not valid") |
| 21 | + } |
| 22 | + Error::BaseUrlWithInit => f.write_str( |
| 23 | + "specifying both an init object, and a separate base URL is not valid", |
| 24 | + ), |
| 25 | + Error::Tokenizer(err, pos) => { |
| 26 | + write!(f, "tokenizer error: {err} (at char {pos})") |
| 27 | + } |
| 28 | + Error::Parser(err) => write!(f, "parser error: {err}"), |
| 29 | + Error::Url(err) => err.fmt(f), |
| 30 | + Error::RegExp(_) => f.write_str("regexp error"), |
| 31 | + } |
33 | 32 | }
|
34 | 33 | }
|
35 | 34 |
|
36 |
| -#[derive(Debug, Display)] |
| 35 | +impl std::error::Error for Error {} |
| 36 | + |
| 37 | +#[derive(Debug)] |
37 | 38 | pub enum TokenizerError {
|
38 |
| - #[display(fmt = "incomplete escape code")] |
39 | 39 | IncompleteEscapeCode,
|
40 |
| - #[display(fmt = "invalid name; must be at least length 1")] |
41 | 40 | InvalidName,
|
42 |
| - #[display(fmt = "invalid regex: {_0}")] |
43 | 41 | InvalidRegex(&'static str),
|
44 | 42 | }
|
45 | 43 |
|
46 |
| -#[derive(Debug, Display)] |
| 44 | +impl fmt::Display for TokenizerError { |
| 45 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 46 | + match self { |
| 47 | + Self::IncompleteEscapeCode => f.write_str("incomplete escape code"), |
| 48 | + Self::InvalidName => { |
| 49 | + f.write_str("invalid name; must be at least length 1") |
| 50 | + } |
| 51 | + Self::InvalidRegex(err) => write!(f, "invalid regex: {err}"), |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl std::error::Error for TokenizerError {} |
| 57 | + |
| 58 | +#[derive(Debug)] |
47 | 59 | pub enum ParserError {
|
48 |
| - #[display(fmt = "expected token {_0}, found '{_2}' of type {_1}")] |
49 | 60 | ExpectedToken(TokenType, TokenType, String),
|
50 |
| - |
51 |
| - #[display(fmt = "pattern contains duplicate name {_0}")] |
52 | 61 | DuplicateName(String),
|
53 | 62 | }
|
| 63 | + |
| 64 | +impl fmt::Display for ParserError { |
| 65 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 66 | + match self { |
| 67 | + Self::ExpectedToken(expected_ty, found_ty, found_val) => { |
| 68 | + write!( |
| 69 | + f, |
| 70 | + "expected token {expected_ty:?}, found '{found_val}' of type {found_ty:?}" |
| 71 | + ) |
| 72 | + } |
| 73 | + Self::DuplicateName(name) => { |
| 74 | + write!(f, "pattern contains duplicate name {name}") |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl std::error::Error for ParserError {} |
0 commit comments