Skip to main content

better_url/parts/path/entire/
either.rs

1//! [`Path`].
2
3use crate::prelude::*;
4
5/// Either a [`SegmentedPath`] or an [`OpaquePath`].
6#[derive(Debug, Clone)]
7pub enum Path<'a> {
8    /** [`SegmentedPath`]. */ Segmented(SegmentedPath<'a>),
9    /** [`OpaquePath`].    */ Opaque   (OpaquePath   <'a>),
10}
11
12impl<'a> Path<'a> {
13    /// Either [`Self::new_segmented`] or [`Self::new_opaque`].
14    pub fn new<T: Into<FilePath<'a>> + Into<SpecialNotFilePath<'a>> + Into<NonSpecialPath<'a>> + Into<OpaquePath<'a>>>(value: T, r#type: PathType) -> Self {
15        match r#type {
16            PathType::Segmented(x) => Self::new_segmented(value, x),
17            PathType::Opaque       => Self::new_opaque   (value   ),
18        }
19    }
20
21    /** [`SegmentedPath::new`].      */ pub fn new_segmented       <T: Into<FilePath<'a>> + Into<SpecialNotFilePath<'a>> + Into<NonSpecialPath<'a>>>(value: T, r#type: SegmentedPathType) -> Self {SegmentedPath::new(value, r#type).into()}
22    /** [`FilePath::new`].           */ pub fn new_file            <T: Into<FilePath          <'a>>>(value: T) -> Self {FilePath          ::new(value).into()}
23    /** [`SpecialNotFilePath::new`]. */ pub fn new_special_not_file<T: Into<SpecialNotFilePath<'a>>>(value: T) -> Self {SpecialNotFilePath::new(value).into()}
24    /** [`NonSpecialPath::new`].     */ pub fn new_non_special     <T: Into<NonSpecialPath    <'a>>>(value: T) -> Self {NonSpecialPath    ::new(value).into()}
25    /** [`OpaquePath::new`].         */ pub fn new_opaque          <T: Into<OpaquePath        <'a>>>(value: T) -> Self {OpaquePath        ::new(value).into()}
26
27
28
29    /// Either [`Self::new_segmented_unchecked`] or [`Self::new_opaque_unchecked`].
30    /// # Safety
31    /// Either [`Self::new_segmented_unchecked`] or [`Self::new_opaque_unchecked`].
32    pub unsafe fn new_unchecked<T: Into<Cow<'a, str>>>(value: T, r#type: PathType) -> Self {
33        match r#type {
34            PathType::Segmented(x) => unsafe {Self::new_segmented_unchecked(value, x)},
35            PathType::Opaque       => unsafe {Self::new_opaque_unchecked   (value   )},
36        }
37    }
38
39    /// [`SegmentedPath::new_unchecked`].
40    /// # Safety
41    /// [`SegmentedPath::new_unchecked`].
42    pub unsafe fn new_segmented_unchecked       <T: Into<Cow<'a, str>>>(value: T, r#type: SegmentedPathType) -> Self {unsafe {SegmentedPath::new_unchecked(value, r#type)}.into()}
43
44    /// [`FilePath::new_unchecked`].
45    /// # Safety
46    /// [`FilePath::new_unchecked`].
47    pub unsafe fn new_file_unchecked            <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {FilePath          ::new_unchecked(value)}.into()}
48
49    /// [`SpecialNotFilePath::new_unchecked`].
50    /// # Safety
51    /// [`SpecialNotFilePath::new_unchecked`].
52    pub unsafe fn new_special_not_file_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {SpecialNotFilePath::new_unchecked(value)}.into()}
53
54    /// [`NonSpecialPath::new_unchecked`].
55    /// # Safety
56    /// [`NonSpecialPath::new_unchecked`].
57    pub unsafe fn new_non_special_unchecked     <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {NonSpecialPath    ::new_unchecked(value)}.into()}
58
59    /// [`OpaquePath::new_unchecked`].
60    /// # Safety
61    /// [`OpaquePath::new_unchecked`].
62    pub unsafe fn new_opaque_unchecked          <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {OpaquePath        ::new_unchecked(value)}.into()}
63
64
65
66
67    /// Borrow as a [`str`].
68    pub fn as_str(&self) -> &str {
69        match self {
70            Self::Segmented(x) => x.as_str(),
71            Self::Opaque   (x) => x.as_str(),
72        }
73    }
74
75    /// The [`PathType`].
76    pub fn r#type(&self) -> PathType {
77        match self {
78            Self::Segmented(x) => x.r#type().into(),
79            Self::Opaque   (_) => PathType::Opaque,
80        }
81    }
82
83
84
85    /** If it's [`Self::Segmented`].               */ pub fn is_segmented       (&self) -> bool {matches!(self, Self::Segmented(_))}
86    /** If it's [`SegmentedPath::File`].           */ pub fn is_file            (&self) -> bool {matches!(self, Self::Segmented(SegmentedPath::File          (_)))}
87    /** If it's [`SegmentedPath::SpecialNotFile`]. */ pub fn is_special_not_file(&self) -> bool {matches!(self, Self::Segmented(SegmentedPath::SpecialNotFile(_)))}
88    /** If it's [`SegmentedPath::NonSpecial`].     */ pub fn is_non_special     (&self) -> bool {matches!(self, Self::Segmented(SegmentedPath::NonSpecial    (_)))}
89    /** If it's [`Self::Opaque`].                  */ pub fn is_opaque          (&self) -> bool {matches!(self, Self::Opaque  (_))}
90
91
92
93    /// The [`SegmentedPath`].
94    pub fn segmented(self) -> Option<SegmentedPath<'a>> {
95        match self {
96            Self::Segmented(x) => Some(x),
97            Self::Opaque   (_) => None
98        }
99    }
100
101    /** The [`FilePath`].           */ pub fn file            (self) -> Option<FilePath          <'a>> {self.segmented()?.file            ()}
102    /** The [`SpecialNotFilePath`]. */ pub fn special_not_file(self) -> Option<SpecialNotFilePath<'a>> {self.segmented()?.special_not_file()}
103    /** The [`NonSpecialPath`].     */ pub fn non_special     (self) -> Option<NonSpecialPath    <'a>> {self.segmented()?.non_special     ()}
104
105    /// The [`OpaquePath`].
106    pub fn opaque(self) -> Option<OpaquePath<'a>> {
107        match self {
108            Self::Segmented(_) => None,
109            Self::Opaque   (x) => Some(x),
110        }
111    }
112
113
114
115    /// Turn into the inner [`Cow`].
116    pub fn into_inner(self) -> Cow<'a, str> {
117        match self {
118            Self::Segmented(x) => x.into_inner(),
119            Self::Opaque   (x) => x.into_inner(),
120        }
121    }
122
123    /// Turn into an owned [`Self`].
124    pub fn into_owned(self) -> Path<'static> {
125        match self {
126            Self::Segmented(x) => x.into_owned().into(),
127            Self::Opaque   (x) => x.into_owned().into(),
128        }
129    }
130
131    /// Make a borrowing [`Self`].
132    pub fn borrowed(&self) -> Path<'_> {
133        match self {
134            Self::Segmented(x) => x.borrowed().into() ,
135            Self::Opaque   (x) => x.borrowed().into() ,
136        }
137    }
138}
139
140
141
142impl<'a> From<SegmentedPath     <'a>> for Path<'a> {fn from(value: SegmentedPath     <'a>) -> Self {Self::Segmented(value       )}}
143impl<'a> From<FilePath          <'a>> for Path<'a> {fn from(value: FilePath          <'a>) -> Self {Self::Segmented(value.into())}}
144impl<'a> From<SpecialNotFilePath<'a>> for Path<'a> {fn from(value: SpecialNotFilePath<'a>) -> Self {Self::Segmented(value.into())}}
145impl<'a> From<NonSpecialPath    <'a>> for Path<'a> {fn from(value: NonSpecialPath    <'a>) -> Self {Self::Segmented(value.into())}}
146impl<'a> From<OpaquePath        <'a>> for Path<'a> {fn from(value: OpaquePath        <'a>) -> Self {Self::Opaque   (value       )}}