use std::collections::HashMap;
use crate::{HttpError, Method, RouteCollection};
pub const ROUTE_ATTRIBUTE: &str = "_route";
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MatchResult {
route_name: String,
parameters: HashMap<String, String>,
}
impl MatchResult {
#[must_use]
pub fn route_name(&self) -> &str {
&self.route_name
}
#[must_use]
pub const fn parameters(&self) -> &HashMap<String, String> {
&self.parameters
}
}
#[derive(Clone, Debug, Default)]
pub struct UrlMatcher {
collection: RouteCollection,
}
impl UrlMatcher {
#[must_use]
pub const fn new(collection: RouteCollection) -> Self {
Self { collection }
}
#[must_use]
pub const fn collection(&self) -> &RouteCollection {
&self.collection
}
pub fn match_request(&self, method: Method, path: &str) -> Result<MatchResult, HttpError> {
let mut path_matched = false;
for route in self.collection.routes() {
let Some(parameters) = match_path(route.path(), path) else {
continue;
};
path_matched = true;
if route.allows(method) {
return Ok(MatchResult {
route_name: route.name().to_owned(),
parameters,
});
}
}
if path_matched {
Err(HttpError::status(
405,
format!("method `{method}` not allowed"),
))
} else {
Err(HttpError::status(404, format!("no route for `{path}`")))
}
}
pub fn apply(&self, request: &mut crate::Request) -> Result<MatchResult, HttpError> {
let matched = self.match_request(request.method(), request.path())?;
request
.attributes_mut()
.insert(ROUTE_ATTRIBUTE, matched.route_name().to_owned());
for (key, value) in matched.parameters() {
request.attributes_mut().insert(key.clone(), value.clone());
}
Ok(matched)
}
}
fn match_path(pattern: &str, path: &str) -> Option<HashMap<String, String>> {
let pattern_segments = crate::route::split_segments(pattern);
let path_segments = crate::route::split_segments(path);
if pattern_segments.len() != path_segments.len() {
return None;
}
let mut parameters = HashMap::new();
for (expected, actual) in pattern_segments.into_iter().zip(path_segments) {
if let Some(name) = crate::route::parameter_name(expected) {
parameters.insert(name.to_owned(), actual.to_owned());
} else if expected != actual {
return None;
}
}
Some(parameters)
}