use crate::prelude::*;
mod get;
mod set;
mod remove;
#[derive(Debug, Clone)]
pub enum SegmentedPath<'a> {
File (FilePath <'a>),
SpecialNotFile(SpecialNotFilePath<'a>),
NonSpecial (NonSpecialPath <'a>),
}
impl<'a> SegmentedPath<'a> {
pub fn new<T: Into<FilePath<'a>> + Into<SpecialNotFilePath<'a>> + Into<NonSpecialPath<'a>>>(value: T, r#type: SegmentedPathType) -> Self {
match r#type {
SegmentedPathType::File => Self::new_file (value),
SegmentedPathType::SpecialNotFile => Self::new_special_not_file(value),
SegmentedPathType::NonSpecial => Self::new_non_special (value),
}
}
pub fn new_file <T: Into<FilePath <'a>>>(value: T) -> Self {FilePath ::new(value).into()}
pub fn new_special_not_file<T: Into<SpecialNotFilePath<'a>>>(value: T) -> Self {SpecialNotFilePath::new(value).into()}
pub fn new_non_special <T: Into<NonSpecialPath <'a>>>(value: T) -> Self {NonSpecialPath ::new(value).into()}
pub unsafe fn new_unchecked<T: Into<Cow<'a, str>>>(value: T, r#type: SegmentedPathType) -> Self {
match r#type {
SegmentedPathType::File => unsafe {Self::new_file_unchecked (value)},
SegmentedPathType::SpecialNotFile => unsafe {Self::new_special_not_file_unchecked(value)},
SegmentedPathType::NonSpecial => unsafe {Self::new_non_special_unchecked (value)},
}
}
pub unsafe fn new_file_unchecked <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {FilePath ::new_unchecked(value)}.into()}
pub unsafe fn new_special_not_file_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {SpecialNotFilePath::new_unchecked(value)}.into()}
pub unsafe fn new_non_special_unchecked <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {NonSpecialPath ::new_unchecked(value)}.into()}
pub fn as_str(&self) -> &str {
match self {
Self::File (x) => x.as_str(),
Self::SpecialNotFile(x) => x.as_str(),
Self::NonSpecial (x) => x.as_str(),
}
}
pub fn r#type(&self) -> SegmentedPathType {
match self {
Self::File (_) => SegmentedPathType::File ,
Self::SpecialNotFile(_) => SegmentedPathType::SpecialNotFile,
Self::NonSpecial (_) => SegmentedPathType::NonSpecial ,
}
}
pub fn segments(&self) -> Option<PathSegments<'_>> {
Some(unsafe {PathSegments::new_unchecked(self.as_str().strip_prefix('/')?, self.r#type())})
}
pub fn is_special (&self) -> bool {matches!(self, Self::SpecialNotFile(_) | Self::File(_))}
pub fn is_special_not_file(&self) -> bool {matches!(self, Self::SpecialNotFile(_) )}
pub fn is_file (&self) -> bool {matches!(self, Self::File (_) )}
pub fn is_non_special (&self) -> bool {matches!(self, Self::NonSpecial (_) )}
pub fn file(self) -> Option<FilePath<'a>> {
match self {
Self::File (x) => Some(x),
Self::SpecialNotFile(_) => None,
Self::NonSpecial (_) => None,
}
}
pub fn special_not_file(self) -> Option<SpecialNotFilePath<'a>> {
match self {
Self::File (_) => None,
Self::SpecialNotFile(x) => Some(x),
Self::NonSpecial (_) => None,
}
}
pub fn non_special(self) -> Option<NonSpecialPath<'a>> {
match self {
Self::File (_) => None,
Self::SpecialNotFile(_) => None,
Self::NonSpecial (x) => Some(x),
}
}
pub fn into_inner(self) -> Cow<'a, str> {
match self {
Self::SpecialNotFile(x) => x.into_inner(),
Self::File (x) => x.into_inner(),
Self::NonSpecial (x) => x.into_inner(),
}
}
pub fn into_owned(self) -> SegmentedPath<'static> {
match self {
Self::SpecialNotFile(x) => x.into_owned().into(),
Self::File (x) => x.into_owned().into(),
Self::NonSpecial (x) => x.into_owned().into(),
}
}
pub fn borrowed(&self) -> SegmentedPath<'_> {
match self {
Self::SpecialNotFile(x) => x.borrowed().into(),
Self::File (x) => x.borrowed().into(),
Self::NonSpecial (x) => x.borrowed().into(),
}
}
}
impl<'a> TryFrom<Path<'a>> for SegmentedPath<'a> {
type Error = OpaquePath<'a>;
fn try_from(value: Path<'a>) -> Result<Self, Self::Error> {
Ok(match value {
Path::Segmented(x) => x,
Path::Opaque (x) => Err(x)?,
})
}
}
impl<'a> From<FilePath <'a>> for SegmentedPath<'a> {fn from(value: FilePath <'a>) -> Self {Self::File (value)}}
impl<'a> From<SpecialNotFilePath<'a>> for SegmentedPath<'a> {fn from(value: SpecialNotFilePath<'a>) -> Self {Self::SpecialNotFile(value)}}
impl<'a> From<NonSpecialPath <'a>> for SegmentedPath<'a> {fn from(value: NonSpecialPath <'a>) -> Self {Self::NonSpecial (value)}}
impl<'a> TryFrom<&'a Path<'_>> for SegmentedPath<'a> {
type Error = OpaquePath<'a>;
fn try_from(value: &'a Path<'_>) -> Result<Self, Self::Error> {
value.borrowed().try_into()
}
}