Skip to content

Commit 5ad1bb8

Browse files
committed
Merge branch 'refs/heads/main' into 162
# Conflicts: # Cargo.toml
2 parents 5ccc949 + cdf29f5 commit 5ad1bb8

File tree

4 files changed

+57
-33
lines changed

4 files changed

+57
-33
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ repository = "https://github.com/denoland/rust-urlpattern"
88
license = "MIT"
99

1010
[dependencies]
11-
derive_more = "0.99.16"
1211
url = "2.5.2"
1312
regex = "1.10.5"
1413
serde = { version = "1.0.127", features = ["derive"] }

src/error.rs

+54-27
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,80 @@
1-
use derive_more::Display;
1+
use std::fmt;
22

33
use crate::tokenizer::TokenType;
44

55
/// A error occurring during URL pattern construction, or matching.
6-
#[derive(Display)]
6+
#[derive(Debug)]
77
pub enum Error {
8-
#[display(fmt = "a relative input without a base URL is not valid")]
98
BaseUrlRequired,
10-
11-
#[display(
12-
fmt = "specifying both an init object, and a separate base URL is not valid"
13-
)]
149
BaseUrlWithInit,
15-
16-
#[display(fmt = "tokenizer error: {_0} (at char {_1})")]
1710
Tokenizer(TokenizerError, usize),
18-
19-
#[display(fmt = "parser error: {_0}")]
2011
Parser(ParserError),
21-
2212
Url(url::ParseError),
23-
24-
#[display(fmt = "regexp error")]
2513
RegExp(()),
2614
}
2715

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+
}
3332
}
3433
}
3534

36-
#[derive(Debug, Display)]
35+
impl std::error::Error for Error {}
36+
37+
#[derive(Debug)]
3738
pub enum TokenizerError {
38-
#[display(fmt = "incomplete escape code")]
3939
IncompleteEscapeCode,
40-
#[display(fmt = "invalid name; must be at least length 1")]
4140
InvalidName,
42-
#[display(fmt = "invalid regex: {_0}")]
4341
InvalidRegex(&'static str),
4442
}
4543

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)]
4759
pub enum ParserError {
48-
#[display(fmt = "expected token {_0}, found '{_2}' of type {_1}")]
4960
ExpectedToken(TokenType, TokenType, String),
50-
51-
#[display(fmt = "pattern contains duplicate name {_0}")]
5261
DuplicateName(String),
5362
}
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 {}

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ mod tests {
578578
}
579579

580580
fn test_case(case: TestCase) {
581-
let input = case.pattern.get(0).cloned();
581+
let input = case.pattern.first().cloned();
582582
let mut base_url = case.pattern.get(1).map(|input| match input {
583583
StringOrInit::String(str) => str.clone(),
584584
StringOrInit::Init(_) => unreachable!(),
@@ -689,7 +689,7 @@ mod tests {
689689
assert_field!(search);
690690
assert_field!(hash);
691691

692-
let input = case.inputs.get(0).cloned();
692+
let input = case.inputs.first().cloned();
693693
let base_url = case.inputs.get(1).map(|input| match input {
694694
StringOrInit::String(str) => str.clone(),
695695
StringOrInit::Init(_) => unreachable!(),

src/tokenizer.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
22

3-
use derive_more::Display;
4-
53
use crate::error::TokenizerError;
64
use crate::Error;
75

86
// Ref: https://wicg.github.io/urlpattern/#tokens
97
// Ref: https://wicg.github.io/urlpattern/#tokenizing
108

119
// Ref: https://wicg.github.io/urlpattern/#token-type
12-
#[derive(Debug, Display, Clone, Eq, PartialEq)]
10+
#[derive(Debug, Clone, Eq, PartialEq)]
1311
pub enum TokenType {
1412
Open,
1513
Close,

0 commit comments

Comments
 (0)