use crate::Error;
use crate::Error::InvalidPath;
use crate::PieceIterator;
use crate::parse;
use std::borrow::Borrow;
use std::fmt::{Debug, Display, Formatter};
#[must_use]
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Path<'a> {
path: &'a str,
}
impl<'a> Path<'a> {
#[must_use]
pub const fn is_valid(path: &str) -> bool {
parse::is_valid_segment(path, b'/', "?")
}
}
impl<'a> Path<'a> {
pub const fn new(path: &'a str) -> Result<Self, Error> {
if Self::is_valid(path) {
Ok(Self { path })
} else {
Err(InvalidPath)
}
}
pub const unsafe fn new_unchecked(path: &'a str) -> Self {
debug_assert!(Self::is_valid(path));
Self { path }
}
}
impl<'a> Default for Path<'a> {
fn default() -> Self {
Self { path: "/" }
}
}
impl<'a> TryFrom<&'a str> for Path<'a> {
type Error = Error;
fn try_from(path: &'a str) -> Result<Self, Self::Error> {
Self::new(path)
}
}
impl<'a> Path<'a> {
#[must_use]
pub const fn as_str(self) -> &'a str {
self.path
}
}
impl<'a> Path<'a> {
pub const fn iter_segments(self) -> PieceIterator<'a> {
PieceIterator::new(self.path, b'/')
}
}
impl<'a> IntoIterator for Path<'a> {
type Item = &'a str;
type IntoIter = PieceIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter_segments()
}
}
impl<'a> PartialEq<str> for Path<'a> {
fn eq(&self, other: &str) -> bool {
self.path == other
}
}
impl<'a> PartialEq<Path<'a>> for str {
fn eq(&self, other: &Path<'a>) -> bool {
self == other.path
}
}
impl<'a> PartialEq<&str> for Path<'a> {
fn eq(&self, other: &&str) -> bool {
self.path == *other
}
}
impl<'a> PartialEq<Path<'a>> for &str {
fn eq(&self, other: &Path<'a>) -> bool {
*self == other.path
}
}
impl<'a> PartialEq<String> for Path<'a> {
fn eq(&self, other: &String) -> bool {
self.path == other.as_str()
}
}
impl<'a> PartialEq<Path<'a>> for String {
fn eq(&self, other: &Path<'a>) -> bool {
self.as_str() == other.path
}
}
impl<'a> AsRef<str> for Path<'a> {
fn as_ref(&self) -> &str {
self.path
}
}
impl<'a> Borrow<str> for Path<'a> {
fn borrow(&self) -> &str {
self.path
}
}
impl<'a> Debug for Path<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self, f)
}
}
impl<'a> Display for Path<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.pad(self.path)
}
}
#[cfg(test)]
mod tests {
use crate::Path;
#[test]
fn is_valid() {
let test_cases: &[(&str, bool)] = &[
("", false),
("/", true),
("///", true),
("/azAZ09", true),
("/!/&/=/~/", true),
("/-._~!$&'()*+,;=:@", true),
("/%20", true),
("/a%2Fb%2fc", true),
("/%00", true),
("/%", false),
("/%2", false),
("/%zz", false),
("/a%2gb", false),
("/?", false),
("/#", false),
("no-slash", false),
("/ ", false),
("/<>", false),
("/[]", false),
("/^`{|}\\\"", false),
("/\u{0}", false),
("/\u{4f60}", false),
];
for (path, expected) in test_cases {
let result: bool = Path::is_valid(path);
assert_eq!(result, *expected, "path={}", path);
}
}
#[test]
fn iter_segments() {
let test_cases: &[(&str, &[&str])] = &[
("/", &[""]),
("//", &["", ""]),
("/a", &["a"]),
("/the/path", &["the", "path"]),
("/the/path/", &["the", "path", ""]),
];
for (path, expected) in test_cases {
let path: Path = Path::new(path).unwrap();
let result: Vec<&str> = path.iter_segments().collect();
assert_eq!(result.as_slice(), *expected, "path={}", path);
}
}
}