Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace derived Display implementations #48

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
81 changes: 54 additions & 27 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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 {}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
Expand Down Expand Up @@ -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!(),
Expand Down
4 changes: 1 addition & 3 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

use derive_more::Display;

use crate::error::TokenizerError;
use crate::Error;

// Ref: https://wicg.github.io/urlpattern/#tokens
// 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,
Expand Down
Loading