diff --git a/Cargo.toml b/Cargo.toml index 49b62b0..6f380a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ repository = "https://github.com/denoland/rust-urlpattern" license = "MIT" [dependencies] -derive_more = "0.99.16" url = "2.2.2" regex = "1.4.3" serde = { version = "1.0.127", features = ["derive"] } diff --git a/src/error.rs b/src/error.rs index b6bb8e2..62b50eb 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,53 +1,80 @@ -use derive_more::Display; +use std::fmt; use crate::tokenizer::TokenType; /// A error occurring during URL pattern construction, or matching. -#[derive(Display)] +#[derive(Debug)] pub enum Error { - #[display(fmt = "a relative input without a base URL is not valid")] BaseUrlRequired, - - #[display( - fmt = "specifying both an init object, and a separate base URL is not valid" - )] BaseUrlWithInit, - - #[display(fmt = "tokenizer error: {_0} (at char {_1})")] Tokenizer(TokenizerError, usize), - - #[display(fmt = "parser error: {_0}")] Parser(ParserError), - Url(url::ParseError), - - #[display(fmt = "regexp error")] RegExp(()), } -impl std::error::Error for Error {} - -impl std::fmt::Debug for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - std::fmt::Display::fmt(self, f) +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::BaseUrlRequired => { + f.write_str("a relative input without a base URL is not valid") + } + Error::BaseUrlWithInit => f.write_str( + "specifying both an init object, and a separate base URL is not valid", + ), + Error::Tokenizer(err, pos) => { + write!(f, "tokenizer error: {err} (at char {pos})") + } + Error::Parser(err) => write!(f, "parser error: {err}"), + Error::Url(err) => err.fmt(f), + Error::RegExp(_) => f.write_str("regexp error"), + } } } -#[derive(Debug, Display)] +impl std::error::Error for Error {} + +#[derive(Debug)] pub enum TokenizerError { - #[display(fmt = "incomplete escape code")] IncompleteEscapeCode, - #[display(fmt = "invalid name; must be at least length 1")] InvalidName, - #[display(fmt = "invalid regex: {_0}")] InvalidRegex(&'static str), } -#[derive(Debug, Display)] +impl fmt::Display for TokenizerError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::IncompleteEscapeCode => f.write_str("incomplete escape code"), + Self::InvalidName => { + f.write_str("invalid name; must be at least length 1") + } + Self::InvalidRegex(err) => write!(f, "invalid regex: {err}"), + } + } +} + +impl std::error::Error for TokenizerError {} + +#[derive(Debug)] pub enum ParserError { - #[display(fmt = "expected token {_0}, found '{_2}' of type {_1}")] ExpectedToken(TokenType, TokenType, String), - - #[display(fmt = "pattern contains duplicate name {_0}")] DuplicateName(String), } + +impl fmt::Display for ParserError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::ExpectedToken(expected_ty, found_ty, found_val) => { + write!( + f, + "expected token {expected_ty:?}, found '{found_val}' of type {found_ty:?}" + ) + } + Self::DuplicateName(name) => { + write!(f, "pattern contains duplicate name {name}") + } + } + } +} + +impl std::error::Error for ParserError {} diff --git a/src/lib.rs b/src/lib.rs index ed688cd..e8571f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -578,7 +578,7 @@ mod tests { } fn test_case(case: TestCase) { - let input = case.pattern.get(0).cloned(); + let input = case.pattern.first().cloned(); let mut base_url = case.pattern.get(1).map(|input| match input { StringOrInit::String(str) => str.clone(), StringOrInit::Init(_) => unreachable!(), @@ -689,7 +689,7 @@ mod tests { assert_field!(search); assert_field!(hash); - let input = case.inputs.get(0).cloned(); + let input = case.inputs.first().cloned(); let base_url = case.inputs.get(1).map(|input| match input { StringOrInit::String(str) => str.clone(), StringOrInit::Init(_) => unreachable!(), diff --git a/src/tokenizer.rs b/src/tokenizer.rs index c02b503..17014c6 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -1,7 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use derive_more::Display; - use crate::error::TokenizerError; use crate::Error; @@ -9,7 +7,7 @@ use crate::Error; // Ref: https://wicg.github.io/urlpattern/#tokenizing // Ref: https://wicg.github.io/urlpattern/#token-type -#[derive(Debug, Display, Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq)] pub enum TokenType { Open, Close,