Skip to main content

better_url/parts/path/entire/segmented/either/
mod.rs

1//! [`SegmentedPath`].
2
3use crate::prelude::*;
4
5mod get;
6mod set;
7mod remove;
8
9/// Either a [`FilePath`], [`SpecialNotFilePath`], or [`NonSpecialPath`].
10#[derive(Debug, Clone)]
11pub enum SegmentedPath<'a> {
12    /** [`FilePath`].           */ File          (FilePath          <'a>),
13    /** [`SpecialNotFilePath`]. */ SpecialNotFile(SpecialNotFilePath<'a>),
14    /** [`NonSpecialPath`].     */ NonSpecial    (NonSpecialPath    <'a>),
15}
16
17impl<'a> SegmentedPath<'a> {
18    /// Make a new [`Self`] of the [`SegmentedPathType`].
19    pub fn new<T: Into<FilePath<'a>> + Into<SpecialNotFilePath<'a>> + Into<NonSpecialPath<'a>>>(value: T, r#type: SegmentedPathType) -> Self {
20        match r#type {
21            SegmentedPathType::File           => Self::new_file            (value),
22            SegmentedPathType::SpecialNotFile => Self::new_special_not_file(value),
23            SegmentedPathType::NonSpecial     => Self::new_non_special     (value),
24        }
25    }
26
27    /** [`FilePath::new`].           */ pub fn new_file            <T: Into<FilePath          <'a>>>(value: T) -> Self {FilePath          ::new(value).into()}
28    /** [`SpecialNotFilePath::new`]. */ pub fn new_special_not_file<T: Into<SpecialNotFilePath<'a>>>(value: T) -> Self {SpecialNotFilePath::new(value).into()}
29    /** [`NonSpecialPath::new`].     */ pub fn new_non_special     <T: Into<NonSpecialPath    <'a>>>(value: T) -> Self {NonSpecialPath    ::new(value).into()}
30
31
32
33    /// Either [`Self::new_file_unchecked`], [`Self::new_special_not_file_unchecked`], or [`Self::new_non_special_unchecked`].
34    /// # Safety
35    /// Either [`Self::new_file_unchecked`], [`Self::new_special_not_file_unchecked`], or [`Self::new_non_special_unchecked`].
36    pub unsafe fn new_unchecked<T: Into<Cow<'a, str>>>(value: T, r#type: SegmentedPathType) -> Self {
37        match r#type {
38            SegmentedPathType::File           => unsafe {Self::new_file_unchecked            (value)},
39            SegmentedPathType::SpecialNotFile => unsafe {Self::new_special_not_file_unchecked(value)},
40            SegmentedPathType::NonSpecial     => unsafe {Self::new_non_special_unchecked     (value)},
41        }
42    }
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
60
61    /// Borrow as a [`str`].
62    pub fn as_str(&self) -> &str {
63        match self {
64            Self::File          (x) => x.as_str(),
65            Self::SpecialNotFile(x) => x.as_str(),
66            Self::NonSpecial    (x) => x.as_str(),
67        }
68    }
69
70    /// The [`SegmentedPathType`].
71    pub fn r#type(&self) -> SegmentedPathType {
72        match self {
73            Self::File          (_) => SegmentedPathType::File          ,
74            Self::SpecialNotFile(_) => SegmentedPathType::SpecialNotFile,
75            Self::NonSpecial    (_) => SegmentedPathType::NonSpecial    ,
76        }
77    }
78
79    /// The [`PathSegments`].
80    ///
81    /// This is an [`Option`] because of [`NonSpecialPath`].
82    pub fn segments(&self) -> Option<PathSegments<'_>> {
83        Some(unsafe {PathSegments::new_unchecked(self.as_str().strip_prefix('/')?, self.r#type())})
84    }
85
86
87
88    /** If it's [`Self::SpecialNotFile`] or [`Self::File`]. */ pub fn is_special         (&self) -> bool {matches!(self, Self::SpecialNotFile(_) | Self::File(_))}
89    /** If it's [`Self::SpecialNotFile`].                   */ pub fn is_special_not_file(&self) -> bool {matches!(self, Self::SpecialNotFile(_)                )}
90    /** If it's [`Self::File`].                             */ pub fn is_file            (&self) -> bool {matches!(self, Self::File          (_)                )}
91    /** If it's [`Self::NonSpecial`].                       */ pub fn is_non_special     (&self) -> bool {matches!(self, Self::NonSpecial    (_)                )}
92
93
94
95    /// [`Self::File`].
96    pub fn file(self) -> Option<FilePath<'a>> {
97        match self {
98            Self::File          (x) => Some(x),
99            Self::SpecialNotFile(_) => None,
100            Self::NonSpecial    (_) => None,
101        }
102    }
103
104    /// [`Self::SpecialNotFile`].
105    pub fn special_not_file(self) -> Option<SpecialNotFilePath<'a>> {
106        match self {
107            Self::File          (_) => None,
108            Self::SpecialNotFile(x) => Some(x),
109            Self::NonSpecial    (_) => None,
110        }
111    }
112
113    /// [`Self::NonSpecial`].
114    pub fn non_special(self) -> Option<NonSpecialPath<'a>> {
115        match self {
116            Self::File          (_) => None,
117            Self::SpecialNotFile(_) => None,
118            Self::NonSpecial    (x) => Some(x),
119        }
120    }
121
122
123
124    /// Turn into the inner [`Cow`].
125    pub fn into_inner(self) -> Cow<'a, str> {
126        match self {
127            Self::SpecialNotFile(x) => x.into_inner(),
128            Self::File          (x) => x.into_inner(),
129            Self::NonSpecial    (x) => x.into_inner(),
130        }
131    }
132
133    /// Turn into an owned [`Self`].
134    pub fn into_owned(self) -> SegmentedPath<'static> {
135        match self {
136            Self::SpecialNotFile(x) => x.into_owned().into(),
137            Self::File          (x) => x.into_owned().into(),
138            Self::NonSpecial    (x) => x.into_owned().into(),
139        }
140    }
141
142    /// Make a borrowing [`Self`].
143    pub fn borrowed(&self) -> SegmentedPath<'_> {
144        match self {
145            Self::SpecialNotFile(x) => x.borrowed().into(),
146            Self::File          (x) => x.borrowed().into(),
147            Self::NonSpecial    (x) => x.borrowed().into(),
148        }
149    }
150}
151
152
153
154impl<'a> TryFrom<Path<'a>> for SegmentedPath<'a> {
155    type Error = OpaquePath<'a>;
156
157    fn try_from(value: Path<'a>) -> Result<Self, Self::Error> {
158        Ok(match value {
159            Path::Segmented(x) => x,
160            Path::Opaque   (x) => Err(x)?,
161        })
162    }
163}
164
165impl<'a> From<FilePath          <'a>> for SegmentedPath<'a> {fn from(value: FilePath          <'a>) -> Self {Self::File          (value)}}
166impl<'a> From<SpecialNotFilePath<'a>> for SegmentedPath<'a> {fn from(value: SpecialNotFilePath<'a>) -> Self {Self::SpecialNotFile(value)}}
167impl<'a> From<NonSpecialPath    <'a>> for SegmentedPath<'a> {fn from(value: NonSpecialPath    <'a>) -> Self {Self::NonSpecial    (value)}}
168
169
170
171impl<'a> TryFrom<&'a Path<'_>> for SegmentedPath<'a> {
172    type Error = OpaquePath<'a>;
173
174    fn try_from(value: &'a Path<'_>) -> Result<Self, Self::Error> {
175        value.borrowed().try_into()
176    }
177}