use std::{
error::Error,
fmt::{self, Display, Formatter},
};
use http::Method;
pub const MAX_NORMALIZED_ROUTE_BYTES: usize = 256;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NormalizedRoute(String);
impl NormalizedRoute {
pub fn new(value: impl Into<String>) -> Result<Self, NormalizedRouteError> {
let value = value.into();
if !value.starts_with('/') {
return Err(NormalizedRouteError::MissingRoot);
}
if value.len() > MAX_NORMALIZED_ROUTE_BYTES {
return Err(NormalizedRouteError::TooLong {
actual_bytes: value.len(),
});
}
if let Some((character_index, _)) =
value.chars().enumerate().find(|(_, ch)| ch.is_control())
{
return Err(NormalizedRouteError::ControlCharacter { character_index });
}
Ok(Self(value))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Display for NormalizedRoute {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum NormalizedRouteError {
MissingRoot,
TooLong {
actual_bytes: usize,
},
ControlCharacter {
character_index: usize,
},
}
impl Display for NormalizedRouteError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::MissingRoot => formatter.write_str("normalized route must begin with '/'"),
Self::TooLong { actual_bytes } => write!(
formatter,
"normalized route is {actual_bytes} bytes; maximum is {MAX_NORMALIZED_ROUTE_BYTES}"
),
Self::ControlCharacter { character_index } => write!(
formatter,
"normalized route contains a control character at index {character_index}"
),
}
}
}
impl Error for NormalizedRouteError {}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct HttpEventContext {
method: Option<Method>,
route: Option<NormalizedRoute>,
}
impl HttpEventContext {
pub const fn new() -> Self {
Self {
method: None,
route: None,
}
}
#[must_use]
pub fn with_method(mut self, method: Method) -> Self {
self.method = Some(method);
self
}
#[must_use]
pub fn with_route(mut self, route: NormalizedRoute) -> Self {
self.route = Some(route);
self
}
pub const fn method(&self) -> Option<&Method> {
self.method.as_ref()
}
pub const fn route(&self) -> Option<&NormalizedRoute> {
self.route.as_ref()
}
}