1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#![allow(clippy::module_name_repetitions)]
use crate::error::{Component, Error as IriError, ErrorKind, Result as IriResult};
use crate::pct_encoding::{path_map, pct_encode};
use crate::{parse, ValidateStr};
use crate::{Normalize, PercentEncoding};
use std::fmt::{Display, Formatter};
use std::str::FromStr;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Path(String);
const PATH_SEP: &str = "/";
const DOT: &str = ".";
const DOT_DOT: &str = "..";
const WELL_KNOWN: &str = "/.well-known/";
impl Default for Path {
fn default() -> Self {
Self(String::new())
}
}
impl Display for Path {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromStr for Path {
type Err = IriError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if Self::is_valid(s) {
Ok(Self(s.to_string()))
} else {
Err(ErrorKind::InvalidChar(Component::Path).into())
}
}
}
impl ValidateStr for Path {
fn is_valid(s: &str) -> bool {
parse::is_path(s)
}
}
impl Normalize for Path {
fn normalize(self) -> IriResult<Self> {
let mut segments = self.hierarchical_segments();
let mut index: usize = 0;
while index < segments.len() {
let segment = segments.get(index).unwrap();
if (segment.is_empty() && index != 0 && index != segments.len() - 1) || segment == DOT {
let _ = segments.remove(index);
} else if segment == DOT_DOT {
let _ = segments.remove(index);
if index > 0 {
index -= 1;
let _ = segments.remove(index);
}
} else {
index += 1;
}
}
Ok(Self(segments.join(PATH_SEP)))
}
}
impl PercentEncoding for Path {
fn encode(&self, for_uri: bool) -> Self
where
Self: Sized,
{
Self(pct_encode(&self.0, path_map(), for_uri))
}
}
impl Path {
pub fn root() -> Self {
Self(PATH_SEP.to_string())
}
pub fn well_known() -> Self {
Self(WELL_KNOWN.to_string())
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn is_absolute(&self) -> bool {
self.0.starts_with(PATH_SEP)
}
pub fn value(&self) -> &String {
&self.0
}
pub fn resolve(&self, relative_path: &Path) -> IriResult<Self> {
let new_path = if relative_path.is_empty() {
self.clone()
} else if relative_path.is_absolute() {
relative_path.clone()
} else if self.0.ends_with(PATH_SEP) {
let mut new = self.clone();
new.push(&relative_path.0)?;
new
} else {
let mut new = self.clone();
let _ = new.pop_slug();
new.push(&relative_path.0)?;
new
}
.normalize()?;
Ok(new_path)
}
pub fn is_normalized(&self) -> bool {
self.0
.split(PATH_SEP)
.all(|segment| segment != DOT && segment != DOT_DOT)
}
pub fn is_well_known(&self) -> bool {
self.0.starts_with(WELL_KNOWN)
}
fn hierarchical_segments(&self) -> Vec<String> {
self.0.split(PATH_SEP).map(|s| s.to_string()).collect()
}
pub fn push(&mut self, segment: &str) -> IriResult<()> {
if parse::is_path(segment) {
if self.0.ends_with(PATH_SEP) {
self.0 = format!("{}{}", self.0, segment);
} else {
self.0 = format!("{}/{}", self.0, segment);
}
Ok(())
} else {
Err(ErrorKind::InvalidChar(Component::Path).into())
}
}
pub fn pop(&mut self) -> Option<String> {
let mut segments = self.hierarchical_segments();
let last = segments.pop();
self.0 = segments.join(PATH_SEP);
last
}
pub fn has_slug(&self) -> bool {
!self.0.is_empty() && !self.0.ends_with(PATH_SEP)
}
pub fn slug(&mut self) -> Option<String> {
if self.has_slug() {
let segments = self.hierarchical_segments();
segments.last().cloned()
} else {
None
}
}
pub fn pop_slug(&mut self) -> Option<String> {
let mut segments = self.hierarchical_segments();
let last = segments.pop();
self.0 = segments.join(PATH_SEP);
if !self.0.is_empty() {
self.0 = format!("{}{}", self.0, PATH_SEP);
}
last
}
}