Skip to main content

better_url/parts/path/segments/
either.rs

1//! [`PathSegments`].
2
3use crate::prelude::*;
4
5/// Either [`FilePathSegments`], [`SpecialNotFilePathSegments`], or [`NonSpecialPathSegments`].
6#[derive(Debug, Clone)]
7pub enum PathSegments<'a> {
8    /** [`FilePathSegments`].           */ File          (FilePathSegments          <'a>),
9    /** [`SpecialNotFilePathSegments`]. */ SpecialNotFile(SpecialNotFilePathSegments<'a>),
10    /** [`NonSpecialPathSegments`].     */ NonSpecial    (NonSpecialPathSegments    <'a>),
11}
12
13impl<'a> PathSegments<'a> {
14    /// Make a new [`Self`] of the [`SegmentedPathType`].
15    pub fn new<T: Into<FilePathSegments<'a>> + Into<SpecialNotFilePathSegments<'a>> + Into<NonSpecialPathSegments<'a>>>(value: T, r#type: SegmentedPathType) -> Self {
16        match r#type {
17            SegmentedPathType::File           => Self::new_file            (value),
18            SegmentedPathType::SpecialNotFile => Self::new_special_not_file(value),
19            SegmentedPathType::NonSpecial     => Self::new_non_special     (value),
20        }
21    }
22
23    /** [`FilePathSegments::new`].           */ pub fn new_file            <T: Into<FilePathSegments          <'a>>>(value: T) -> Self {FilePathSegments          ::new(value).into()}
24    /** [`SpecialNotFilePathSegments::new`]. */ pub fn new_special_not_file<T: Into<SpecialNotFilePathSegments<'a>>>(value: T) -> Self {SpecialNotFilePathSegments::new(value).into()}
25    /** [`NonSpecialPathSegments::new`].     */ pub fn new_non_special     <T: Into<NonSpecialPathSegments    <'a>>>(value: T) -> Self {NonSpecialPathSegments    ::new(value).into()}
26
27
28
29    /// Either [`Self::new_file_unchecked`], [`Self::new_special_not_file_unchecked`], or [`Self::new_non_special_unchecked`].
30    /// # Safety
31    /// Either [`Self::new_file_unchecked`], [`Self::new_special_not_file_unchecked`], or [`Self::new_non_special_unchecked`].
32    pub unsafe fn new_unchecked<T: Into<Cow<'a, str>>>(value: T, r#type: SegmentedPathType) -> Self {
33        match r#type {
34            SegmentedPathType::File           => unsafe {Self::new_file_unchecked            (value)},
35            SegmentedPathType::SpecialNotFile => unsafe {Self::new_special_not_file_unchecked(value)},
36            SegmentedPathType::NonSpecial     => unsafe {Self::new_non_special_unchecked     (value)},
37        }
38    }
39
40    /// [`FilePathSegments::new_unchecked`].
41    /// # Safety
42    /// [`FilePathSegments::new_unchecked`].
43    pub unsafe fn new_file_unchecked            <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {FilePathSegments          ::new_unchecked(value)}.into()}
44
45    /// [`SpecialNotFilePathSegments::new_unchecked`].
46    /// # Safety
47    /// [`SpecialNotFilePathSegments::new_unchecked`].
48    pub unsafe fn new_special_not_file_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {SpecialNotFilePathSegments::new_unchecked(value)}.into()}
49
50    /// [`NonSpecialPathSegments::new_unchecked`].
51    /// # Safety
52    /// [`NonSpecialPathSegments::new_unchecked`].
53    pub unsafe fn new_non_special_unchecked     <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {NonSpecialPathSegments    ::new_unchecked(value)}.into()}
54
55
56
57    /// Borrow as a [`str`].
58    pub fn as_str(&self) -> &str {
59        match self {
60            Self::SpecialNotFile(x) => x.as_str(),
61            Self::File          (x) => x.as_str(),
62            Self::NonSpecial    (x) => x.as_str(),
63        }
64    }
65
66    /// The [`SegmentedPathType`].
67    pub fn r#type(&self) -> SegmentedPathType {
68        match self {
69            Self::File          (_) => SegmentedPathType::File          ,
70            Self::SpecialNotFile(_) => SegmentedPathType::SpecialNotFile,
71            Self::NonSpecial    (_) => SegmentedPathType::NonSpecial    ,
72        }
73    }
74
75
76
77    /// The [`PathSegmentsIter`].
78    pub fn iter(&self) -> PathSegmentsIter<'_> {
79        self.into_iter()
80    }
81
82    /// The `index`th [`PathSegment`].
83    pub fn get(&self, index: isize) -> Option<PathSegment<'_>> {
84        self.iter().neg_nth(index)
85    }
86
87
88
89    /// [`Self::File`].
90    pub fn file(self) -> Option<FilePathSegments<'a>> {
91        match self {
92            Self::File          (x) => Some(x),
93            Self::SpecialNotFile(_) => None,
94            Self::NonSpecial    (_) => None,
95        }
96    }
97
98    /// [`Self::SpecialNotFile`].
99    pub fn special_not_file(self) -> Option<SpecialNotFilePathSegments<'a>> {
100        match self {
101            Self::File          (_) => None,
102            Self::SpecialNotFile(x) => Some(x),
103            Self::NonSpecial    (_) => None,
104        }
105    }
106
107    /// [`Self::NonSpecial`].
108    pub fn non_special(self) -> Option<NonSpecialPathSegments<'a>> {
109        match self {
110            Self::File          (_) => None,
111            Self::SpecialNotFile(_) => None,
112            Self::NonSpecial    (x) => Some(x),
113        }
114    }
115
116
117
118    /// Turn into the inner [`Cow`].
119    pub fn into_inner(self) -> Cow<'a, str> {
120        match self {
121            Self::File          (x) => x.into_inner(),
122            Self::SpecialNotFile(x) => x.into_inner(),
123            Self::NonSpecial    (x) => x.into_inner(),
124        }
125    }
126
127    /// Turn into the inner [`Cow`].
128    pub fn into_owned(self) -> PathSegments<'static> {
129        match self {
130            Self::File          (x) => x.into_owned().into(),
131            Self::SpecialNotFile(x) => x.into_owned().into(),
132            Self::NonSpecial    (x) => x.into_owned().into(),
133        }
134    }
135
136    /// Turn into the inner [`Cow`].
137    pub fn borrowed(&self) -> PathSegments<'_> {
138        match self {
139            Self::File          (x) => x.borrowed().into(),
140            Self::SpecialNotFile(x) => x.borrowed().into(),
141            Self::NonSpecial    (x) => x.borrowed().into(),
142        }
143    }
144}
145
146
147
148impl<'a> From<PathSegment<'a>> for PathSegments<'a> {
149    fn from(value: PathSegment<'a>) -> Self {
150        match value {
151            PathSegment::File          (x) => x.into(),
152            PathSegment::SpecialNotFile(x) => x.into(),
153            PathSegment::NonSpecial    (x) => x.into(),
154        }
155    }
156}
157
158impl<'a> From<FilePathSegments          <'a>> for PathSegments<'a> {fn from(value: FilePathSegments          <'a>) -> Self {Self::File          (value)}}
159impl<'a> From<SpecialNotFilePathSegments<'a>> for PathSegments<'a> {fn from(value: SpecialNotFilePathSegments<'a>) -> Self {Self::SpecialNotFile(value)}}
160impl<'a> From<NonSpecialPathSegments    <'a>> for PathSegments<'a> {fn from(value: NonSpecialPathSegments    <'a>) -> Self {Self::NonSpecial    (value)}}
161
162impl<'a> From<FilePathSegment           <'a>> for PathSegments<'a> {fn from(value: FilePathSegment           <'a>) -> Self {Self::File          (value.into())}}
163impl<'a> From<SpecialNotFilePathSegment <'a>> for PathSegments<'a> {fn from(value: SpecialNotFilePathSegment <'a>) -> Self {Self::SpecialNotFile(value.into())}}
164impl<'a> From<NonSpecialPathSegment     <'a>> for PathSegments<'a> {fn from(value: NonSpecialPathSegment     <'a>) -> Self {Self::NonSpecial    (value.into())}}