use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum PublicRouteError {
Empty,
NoLeadingSlash(String),
MissingTrailingSlash(String),
}
impl fmt::Display for PublicRouteError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => f.write_str("must not be empty"),
Self::NoLeadingSlash(value) => {
write!(f, "must start with '/' (got {value:?})")
}
Self::MissingTrailingSlash(value) => write!(
f,
"prefix must end with '/' to avoid matching sibling paths \
(got {value:?}; use `with_public_path` for an exact-match exemption)"
),
}
}
}
impl std::error::Error for PublicRouteError {}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PublicPath(String);
impl PublicPath {
pub fn new(value: impl Into<String>) -> Result<Self, PublicRouteError> {
let value = value.into();
validate(&value)?;
Ok(Self(value))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl AsRef<str> for PublicPath {
fn as_ref(&self) -> &str {
&self.0
}
}
impl fmt::Display for PublicPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl TryFrom<&str> for PublicPath {
type Error = PublicRouteError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::new(value)
}
}
impl TryFrom<String> for PublicPath {
type Error = PublicRouteError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::new(value)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PublicPrefix(String);
impl PublicPrefix {
pub fn new(value: impl Into<String>) -> Result<Self, PublicRouteError> {
let value = value.into();
validate(&value)?;
if !value.ends_with('/') {
return Err(PublicRouteError::MissingTrailingSlash(value));
}
Ok(Self(value))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl AsRef<str> for PublicPrefix {
fn as_ref(&self) -> &str {
&self.0
}
}
impl fmt::Display for PublicPrefix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl TryFrom<&str> for PublicPrefix {
type Error = PublicRouteError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::new(value)
}
}
impl TryFrom<String> for PublicPrefix {
type Error = PublicRouteError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::new(value)
}
}
fn validate(value: &str) -> Result<(), PublicRouteError> {
if value.is_empty() {
return Err(PublicRouteError::Empty);
}
if !value.starts_with('/') {
return Err(PublicRouteError::NoLeadingSlash(value.to_owned()));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn public_path_accepts_valid_input() {
let path = PublicPath::new("/healthz").unwrap();
assert_eq!(path.as_str(), "/healthz");
assert_eq!(path.to_string(), "/healthz");
assert_eq!(<PublicPath as AsRef<str>>::as_ref(&path), "/healthz");
}
#[test]
fn public_path_rejects_empty() {
assert_eq!(PublicPath::new(""), Err(PublicRouteError::Empty));
}
#[test]
fn public_path_rejects_missing_leading_slash() {
assert_eq!(
PublicPath::new("healthz"),
Err(PublicRouteError::NoLeadingSlash("healthz".to_string()))
);
}
#[test]
fn public_prefix_accepts_valid_input() {
let prefix = PublicPrefix::new("/dashboard/").unwrap();
assert_eq!(prefix.as_str(), "/dashboard/");
assert_eq!(prefix.to_string(), "/dashboard/");
}
#[test]
fn public_prefix_rejects_empty() {
assert_eq!(PublicPrefix::new(""), Err(PublicRouteError::Empty));
}
#[test]
fn public_prefix_rejects_missing_leading_slash() {
assert_eq!(
PublicPrefix::new("dashboard"),
Err(PublicRouteError::NoLeadingSlash("dashboard".to_string()))
);
}
#[test]
fn public_prefix_rejects_missing_trailing_slash() {
assert_eq!(
PublicPrefix::new("/dashboard"),
Err(PublicRouteError::MissingTrailingSlash(
"/dashboard".to_string()
))
);
}
#[test]
fn public_prefix_root_is_accepted() {
let prefix = PublicPrefix::new("/").unwrap();
assert_eq!(prefix.as_str(), "/");
}
#[test]
fn try_from_str_works() {
let path: PublicPath = "/v1/auth/login".try_into().unwrap();
let prefix: PublicPrefix = "/static/".try_into().unwrap();
assert_eq!(path.as_str(), "/v1/auth/login");
assert_eq!(prefix.as_str(), "/static/");
}
#[test]
fn try_from_string_works() {
let path: PublicPath = String::from("/healthz").try_into().unwrap();
let prefix: PublicPrefix = String::from("/dashboard/").try_into().unwrap();
assert_eq!(path.as_str(), "/healthz");
assert_eq!(prefix.as_str(), "/dashboard/");
}
#[test]
fn error_display_renders_clearly() {
assert_eq!(PublicRouteError::Empty.to_string(), "must not be empty");
assert_eq!(
PublicRouteError::NoLeadingSlash("dashboard".to_string()).to_string(),
"must start with '/' (got \"dashboard\")"
);
assert!(
PublicRouteError::MissingTrailingSlash("/dashboard".to_string())
.to_string()
.contains("must end with '/'")
);
}
}