better_url/parts/path/segment/
either.rs1use crate::prelude::*;
4
5#[derive(Debug, Clone)]
7pub enum PathSegment<'a> {
8 File (FilePathSegment <'a>),
9 SpecialNotFile(SpecialNotFilePathSegment<'a>),
10 NonSpecial (NonSpecialPathSegment <'a>),
11}
12
13impl<'a> PathSegment<'a> {
14 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 pub fn new_file <T: Into<FilePathSegment <'a>>>(value: T) -> Self {FilePathSegment ::new(value).into()}
24 pub fn new_special_not_file<T: Into<SpecialNotFilePathSegment<'a>>>(value: T) -> Self {SpecialNotFilePathSegment::new(value).into()}
25 pub fn new_non_special <T: Into<NonSpecialPathSegment <'a>>>(value: T) -> Self {NonSpecialPathSegment ::new(value).into()}
26
27
28
29 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 pub unsafe fn new_file_unchecked <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {FilePathSegment ::new_unchecked(value)}.into()}
44
45 pub unsafe fn new_special_not_file_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {SpecialNotFilePathSegment::new_unchecked(value)}.into()}
49
50 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 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 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 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 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 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 pub fn is_drive_letter (&self) -> bool {path_segment_is_drive_letter (self.as_str())}
111 pub fn is_normalized_drive_letter (&self) -> bool {path_segment_is_normalized_drive_letter (self.as_str())}
112 pub fn is_non_normalized_drive_letter(&self) -> bool {path_segment_is_non_normalized_drive_letter(self.as_str())}
113 pub fn is_single_dot (&self) -> bool {path_segment_is_single_dot (self.as_str())}
114 pub fn is_double_dot (&self) -> bool {path_segment_is_double_dot (self.as_str())}
115
116
117
118 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 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 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 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 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 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)}}