Skip to main content

better_url/parts/path/segment/
either.rs

1//! [`PathSegment`].
2
3use crate::prelude::*;
4
5/// Either [`FilePathSegment`], [`SpecialNotFilePathSegment`], or [`NonSpecialPathSegment`].
6#[derive(Debug, Clone)]
7pub enum PathSegment<'a> {
8    /** [`FilePathSegment`].           */ File          (FilePathSegment          <'a>),
9    /** [`SpecialNotFilePathSegment`]. */ SpecialNotFile(SpecialNotFilePathSegment<'a>),
10    /** [`NonSpecialPathSegment`].     */ NonSpecial    (NonSpecialPathSegment    <'a>),
11}
12
13impl<'a> PathSegment<'a> {
14    /// Make a new [`Self`] of the [`SegmentedPathType`].
15    pub fn new<T: Into<FilePathSegment<'a>> + Into<SpecialNotFilePathSegment<'a>> + Into<NonSpecialPathSegment<'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    /** [`FilePathSegment::new`].           */ pub fn new_file            <T: Into<FilePathSegment          <'a>>>(value: T) -> Self {FilePathSegment          ::new(value).into()}
24    /** [`SpecialNotFilePathSegment::new`]. */ pub fn new_special_not_file<T: Into<SpecialNotFilePathSegment<'a>>>(value: T) -> Self {SpecialNotFilePathSegment::new(value).into()}
25    /** [`NonSpecialPathSegment::new`].     */ pub fn new_non_special     <T: Into<NonSpecialPathSegment    <'a>>>(value: T) -> Self {NonSpecialPathSegment    ::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    /// [`FilePathSegment::new`].
41    /// # Safety
42    /// [`FilePathSegment::new`].
43    pub unsafe fn new_file_unchecked            <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {FilePathSegment          ::new_unchecked(value)}.into()}
44
45    /// [`SpecialNotFilePathSegment::new`].
46    /// # Safety
47    /// [`SpecialNotFilePathSegment::new`].
48    pub unsafe fn new_special_not_file_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {SpecialNotFilePathSegment::new_unchecked(value)}.into()}
49
50    /// [`NonSpecialPathSegment::new`].
51    /// # Safety
52    /// [`NonSpecialPathSegment::new`].
53    pub unsafe fn new_non_special_unchecked     <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {NonSpecialPathSegment    ::new_unchecked(value)}.into()}
54
55
56
57    /// Borrow as a [`str`].
58    pub fn as_str(&self) -> &str {
59        match self {
60            Self::File          (x) => x.as_str(),
61            Self::SpecialNotFile(x) => x.as_str(),
62            Self::NonSpecial    (x) => x.as_str(),
63        }
64    }
65
66    /// The decoded value.
67    pub fn decode(self) -> Cow<'a, [u8]> {
68        match self {
69            Self::File          (x) => x.decode(),
70            Self::SpecialNotFile(x) => x.decode(),
71            Self::NonSpecial    (x) => x.decode(),
72        }
73    }
74
75    /// The decoded value.
76    /// # Errors
77    /// If [`FilePathSegment::try_decode`] returns an error, that error is returned.
78    ///
79    /// If [`SpecialNotFilePathSegment::try_decode`] returns an error, that error is returned.
80    ///
81    /// If [`NonSpecialPathSegment::try_decode`] returns an error, that error is returned.
82    pub fn try_decode(self) -> Result<Cow<'a, str>, (std::str::Utf8Error, Cow<'a, [u8]>)> {
83        match self {
84            Self::File          (x) => x.try_decode(),
85            Self::SpecialNotFile(x) => x.try_decode(),
86            Self::NonSpecial    (x) => x.try_decode(),
87        }
88    }
89
90    /// The decoded value.
91    pub fn lossy_decode(self) -> Cow<'a, str> {
92        match self {
93            Self::File          (x) => x.lossy_decode(),
94            Self::SpecialNotFile(x) => x.lossy_decode(),
95            Self::NonSpecial    (x) => x.lossy_decode(),
96        }
97    }
98
99    /// The [`SegmentedPathType`].
100    pub fn r#type(&self) -> SegmentedPathType {
101        match self {
102            Self::File          (_) => SegmentedPathType::File          ,
103            Self::SpecialNotFile(_) => SegmentedPathType::SpecialNotFile,
104            Self::NonSpecial    (_) => SegmentedPathType::NonSpecial    ,
105        }
106    }
107
108
109
110    /** [`path_segment_is_drive_letter`].                */ pub fn is_drive_letter               (&self) -> bool {path_segment_is_drive_letter               (self.as_str())}
111    /** [`path_segment_is_normalized_drive_letter`].     */ pub fn is_normalized_drive_letter    (&self) -> bool {path_segment_is_normalized_drive_letter    (self.as_str())}
112    /** [`path_segment_is_non_normalized_drive_letter`]. */ pub fn is_non_normalized_drive_letter(&self) -> bool {path_segment_is_non_normalized_drive_letter(self.as_str())}
113    /** [`path_segment_is_single_dot`].                  */ pub fn is_single_dot                 (&self) -> bool {path_segment_is_single_dot                 (self.as_str())}
114    /** [`path_segment_is_double_dot`].                  */ pub fn is_double_dot                 (&self) -> bool {path_segment_is_double_dot                 (self.as_str())}
115
116
117
118    /// [`Self::File`].
119    pub fn file(self) -> Option<FilePathSegment<'a>> {
120        match self {
121            Self::File          (x) => Some(x),
122            Self::SpecialNotFile(_) => None,
123            Self::NonSpecial    (_) => None,
124        }
125    }
126
127    /// [`Self::SpecialNotFile`].
128    pub fn special_not_file(self) -> Option<SpecialNotFilePathSegment<'a>> {
129        match self {
130            Self::File          (_) => None,
131            Self::SpecialNotFile(x) => Some(x),
132            Self::NonSpecial    (_) => None,
133        }
134    }
135
136    /// [`Self::NonSpecial`].
137    pub fn non_special(self) -> Option<NonSpecialPathSegment<'a>> {
138        match self {
139            Self::File          (_) => None,
140            Self::SpecialNotFile(_) => None,
141            Self::NonSpecial    (x) => Some(x),
142        }
143    }
144
145
146
147    /// Make a borrowing [`Self`].
148    pub fn borrowed(&self) -> PathSegment<'_> {
149        match self {
150            Self::File          (x) => x.borrowed().into(),
151            Self::SpecialNotFile(x) => x.borrowed().into(),
152            Self::NonSpecial    (x) => x.borrowed().into(),
153        }
154    }
155
156    /// Turn into an owned [`Self`]
157    pub fn into_owned(self) -> PathSegment<'static> {
158        match self {
159            Self::File          (x) => x.into_owned().into(),
160            Self::SpecialNotFile(x) => x.into_owned().into(),
161            Self::NonSpecial    (x) => x.into_owned().into(),
162        }
163    }
164
165    /// Turn into the inner [`Cow`].
166    pub fn into_inner(self) -> Cow<'a, str> {
167        match self {
168            Self::File          (x) => x.into_inner(),
169            Self::SpecialNotFile(x) => x.into_inner(),
170            Self::NonSpecial    (x) => x.into_inner(),
171        }
172    }
173}
174
175
176
177impl<'a> From<FilePathSegment          <'a>> for PathSegment<'a> {fn from(value: FilePathSegment          <'a>) -> Self {Self::File          (value)}}
178impl<'a> From<SpecialNotFilePathSegment<'a>> for PathSegment<'a> {fn from(value: SpecialNotFilePathSegment<'a>) -> Self {Self::SpecialNotFile(value)}}
179impl<'a> From<NonSpecialPathSegment    <'a>> for PathSegment<'a> {fn from(value: NonSpecialPathSegment    <'a>) -> Self {Self::NonSpecial    (value)}}