from __future__ import annotations import re from dataclasses import dataclass from typing import Any, Mapping from .handler import APIHandler __all__ = ["Route"] @dataclass class Route: method: str pattern: str handler: APIHandler def __post_init__(self): self._compiled_pattern = re.compile(self.pattern) def match(self, method: str, path: str) -> Mapping[str, Any] | None: if method != self.method: return None match = self._compiled_pattern.match(path) if not match: return None return dict(match.groupdict())