use std::{
borrow::Cow,
ffi::{OsStr, OsString},
fmt,
hash::Hash,
};
pub trait PathSegment: Clone + Eq + Hash + fmt::Debug {
fn empty() -> Self;
#[must_use]
fn is_empty(&self) -> bool;
}
pub trait PathSegmentRef<T: PathSegment>: Eq + Hash + fmt::Debug {
#[must_use]
fn is_empty(&self) -> bool;
#[must_use]
fn to_owned(&self) -> T;
}
impl PathSegment for String {
fn empty() -> Self {
Self::new()
}
fn is_empty(&self) -> bool {
self.as_str().is_empty()
}
}
impl PathSegmentRef<String> for str {
fn is_empty(&self) -> bool {
self.is_empty()
}
fn to_owned(&self) -> String {
String::from(self)
}
}
impl<'a> PathSegment for Cow<'a, str> {
fn empty() -> Self {
Cow::Borrowed("")
}
fn is_empty(&self) -> bool {
self.as_ref().is_empty()
}
}
impl<'a> PathSegmentRef<Cow<'a, str>> for str {
fn is_empty(&self) -> bool {
self.is_empty()
}
fn to_owned(&self) -> Cow<'a, str> {
Cow::Owned(String::from(self))
}
}
impl PathSegment for OsString {
fn empty() -> Self {
Self::new()
}
fn is_empty(&self) -> bool {
self.as_os_str().is_empty()
}
}
impl PathSegmentRef<OsString> for OsStr {
fn is_empty(&self) -> bool {
self.is_empty()
}
fn to_owned(&self) -> OsString {
self.to_os_string()
}
}
pub trait SegmentedPath<S: PathSegment, R: PathSegmentRef<S> + ?Sized>:
Clone + Eq + Hash + fmt::Debug
{
#[must_use]
fn segments(&self) -> Box<dyn Iterator<Item = &R> + '_>;
#[must_use]
fn parent_child_segments(&self) -> (Box<dyn Iterator<Item = &R> + '_>, Option<&R>);
}
pub trait RootPath<S: PathSegment, R: PathSegmentRef<S> + ?Sized>: SegmentedPath<S, R> {
#[must_use]
fn is_root(&self) -> bool;
}