better_url/parts/path/entire/segmented/either/
mod.rs1use crate::prelude::*;
4
5mod get;
6mod set;
7mod remove;
8
9#[derive(Debug, Clone)]
11pub enum SegmentedPath<'a> {
12 File (FilePath <'a>),
13 SpecialNotFile(SpecialNotFilePath<'a>),
14 NonSpecial (NonSpecialPath <'a>),
15}
16
17impl<'a> SegmentedPath<'a> {
18 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 pub fn new_file <T: Into<FilePath <'a>>>(value: T) -> Self {FilePath ::new(value).into()}
28 pub fn new_special_not_file<T: Into<SpecialNotFilePath<'a>>>(value: T) -> Self {SpecialNotFilePath::new(value).into()}
29 pub fn new_non_special <T: Into<NonSpecialPath <'a>>>(value: T) -> Self {NonSpecialPath ::new(value).into()}
30
31
32
33 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 pub unsafe fn new_file_unchecked <T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {FilePath ::new_unchecked(value)}.into()}
48
49 pub unsafe fn new_special_not_file_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {unsafe {SpecialNotFilePath::new_unchecked(value)}.into()}
53
54 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 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 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 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 pub fn is_special (&self) -> bool {matches!(self, Self::SpecialNotFile(_) | Self::File(_))}
89 pub fn is_special_not_file(&self) -> bool {matches!(self, Self::SpecialNotFile(_) )}
90 pub fn is_file (&self) -> bool {matches!(self, Self::File (_) )}
91 pub fn is_non_special (&self) -> bool {matches!(self, Self::NonSpecial (_) )}
92
93
94
95 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 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 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 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 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 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}