mod component;
pub use component::*;
use core::{cmp, fmt, iter};
use crate::typed::Utf8TypedPath;
use crate::unix::Utf8UnixComponents;
use crate::windows::Utf8WindowsComponents;
use crate::{private, Utf8Components};
#[derive(Clone)]
pub enum Utf8TypedComponents<'a> {
Unix(Utf8UnixComponents<'a>),
Windows(Utf8WindowsComponents<'a>),
}
impl<'a> Utf8TypedComponents<'a> {
pub fn to_path(&self) -> Utf8TypedPath<'a> {
match self {
Self::Unix(components) => Utf8TypedPath::Unix(components.as_path()),
Self::Windows(components) => Utf8TypedPath::Windows(components.as_path()),
}
}
pub fn as_str(&self) -> &'a str {
impl_typed_fn!(self, as_str)
}
pub fn is_absolute(&self) -> bool {
impl_typed_fn!(self, is_absolute)
}
pub fn has_root(&self) -> bool {
impl_typed_fn!(self, has_root)
}
}
impl private::Sealed for Utf8TypedComponents<'_> {}
impl AsRef<[u8]> for Utf8TypedComponents<'_> {
#[inline]
fn as_ref(&self) -> &[u8] {
impl_typed_fn!(self, as_ref)
}
}
impl AsRef<str> for Utf8TypedComponents<'_> {
#[inline]
fn as_ref(&self) -> &str {
impl_typed_fn!(self, as_ref)
}
}
impl<'a> fmt::Debug for Utf8TypedComponents<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct DebugHelper<'a>(Utf8TypedComponents<'a>);
impl<'a> fmt::Debug for DebugHelper<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.0.clone()).finish()
}
}
f.debug_tuple("Utf8TypedComponents")
.field(&DebugHelper(self.clone()))
.finish()
}
}
impl<'a> Iterator for Utf8TypedComponents<'a> {
type Item = Utf8TypedComponent<'a>;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Unix(it) => it.next().map(Utf8TypedComponent::Unix),
Self::Windows(it) => it.next().map(Utf8TypedComponent::Windows),
}
}
}
impl<'a> DoubleEndedIterator for Utf8TypedComponents<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
match self {
Self::Unix(it) => it.next_back().map(Utf8TypedComponent::Unix),
Self::Windows(it) => it.next_back().map(Utf8TypedComponent::Windows),
}
}
}
impl<'a> iter::FusedIterator for Utf8TypedComponents<'a> {}
impl<'a> cmp::PartialEq for Utf8TypedComponents<'a> {
#[inline]
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Unix(a), Self::Unix(b)) => a.eq(b),
(Self::Windows(a), Self::Windows(b)) => a.eq(b),
_ => false,
}
}
}
impl<'a> cmp::Eq for Utf8TypedComponents<'a> {}
impl<'a> cmp::PartialOrd for Utf8TypedComponents<'a> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
match (self, other) {
(Self::Unix(a), Self::Unix(b)) => a.partial_cmp(b),
(Self::Windows(a), Self::Windows(b)) => a.partial_cmp(b),
_ => None,
}
}
}