use std::collections::HashMap;
use crate::Method;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Route {
name: String,
path: String,
methods: Vec<Method>,
}
impl Route {
#[must_use]
pub fn new(
name: impl Into<String>,
path: impl Into<String>,
methods: impl IntoIterator<Item = Method>,
) -> Self {
Self {
name: name.into(),
path: path.into(),
methods: methods.into_iter().collect(),
}
}
#[must_use]
pub fn with_method(name: impl Into<String>, path: impl Into<String>, method: Method) -> Self {
Self::new(name, path, [method])
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn path(&self) -> &str {
&self.path
}
#[must_use]
pub fn methods(&self) -> &[Method] {
&self.methods
}
#[must_use]
pub fn allows(&self, method: Method) -> bool {
self.methods.is_empty() || self.methods.contains(&method)
}
}
#[derive(Clone, Debug, Default)]
pub struct RouteCollection {
routes: Vec<Route>,
}
impl RouteCollection {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn add(&mut self, route: Route) -> Result<(), crate::HttpError> {
if self
.routes
.iter()
.any(|existing| existing.name() == route.name())
{
return Err(crate::HttpError::failed(format!(
"route `{}` is already registered",
route.name()
)));
}
self.routes.push(route);
Ok(())
}
#[must_use]
pub fn get(&self, name: &str) -> Option<&Route> {
self.routes.iter().find(|route| route.name() == name)
}
pub fn generate(
&self,
name: &str,
params: &[(&str, &str)],
) -> Result<String, crate::HttpError> {
let route = self
.get(name)
.ok_or_else(|| crate::HttpError::status(404, format!("no route named `{name}`")))?;
let mut unused: HashMap<&str, &str> = HashMap::with_capacity(params.len());
for &(key, value) in params {
if unused.insert(key, value).is_some() {
return Err(crate::HttpError::status(
400,
format!("duplicate parameter `{key}` for route `{name}`"),
));
}
}
let segments = split_segments(route.path());
let mut out = Vec::with_capacity(segments.len());
for segment in segments {
if let Some(param) = parameter_name(segment) {
let Some(value) = unused.remove(param) else {
return Err(crate::HttpError::status(
400,
format!("missing parameter `{param}` for route `{name}`"),
));
};
out.push(encode_path_segment(value));
} else {
out.push(segment.to_owned());
}
}
if let Some((extra, _)) = unused.iter().next() {
return Err(crate::HttpError::status(
400,
format!("unused parameter `{extra}` for route `{name}`"),
));
}
if out.is_empty() {
return Ok("/".to_owned());
}
Ok(format!("/{}", out.join("/")))
}
#[must_use]
pub fn routes(&self) -> &[Route] {
&self.routes
}
#[must_use]
pub const fn len(&self) -> usize {
self.routes.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.routes.is_empty()
}
}
pub fn split_segments(path: &str) -> Vec<&str> {
path.trim_matches('/')
.split('/')
.filter(|s| !s.is_empty())
.collect()
}
pub fn parameter_name(segment: &str) -> Option<&str> {
segment
.strip_prefix('{')
.and_then(|rest| rest.strip_suffix('}'))
.filter(|name| !name.is_empty())
}
fn encode_path_segment(value: &str) -> String {
let mut out = String::with_capacity(value.len());
for byte in value.bytes() {
match byte {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
out.push(char::from(byte));
}
_ => {
let _ = std::fmt::Write::write_fmt(&mut out, format_args!("%{byte:02X}"));
}
}
}
out
}