1#[derive(Debug, Clone, PartialEq, Eq)]
2enum Component {
3 UriScheme(String),
4 DrivePrefix(String),
5 Root,
6 CurrentDir,
7 ParentDir,
8 Normal(String),
9}
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Path {
13 separator: char,
14 components: Vec<Component>,
15}
16
17impl Path {
18 pub fn new_specifying_separator(path: impl Into<String>, separator: char) -> Path {
19 Path::from_optional_sep(path, Some(separator))
20 }
21
22 pub fn new(path: impl Into<String>) -> Path {
23 Path::from_optional_sep(path, None)
24 }
25
26 fn from_optional_sep(path: impl Into<String>, separator: Option<char>) -> Path {
27 let path: String = path.into();
28 let mut path = path.as_str();
29 let mut components = Vec::with_capacity(4);
30 let inferred_separator = match drive_prefix(path) {
31 Some(prefix) => {
32 components.push(Component::DrivePrefix(prefix));
33 path = &path[2..];
34 '\\'
35 }
36 _ => {
37 if path.contains('\\') {
38 '\\'
39 } else if path.contains('/') {
40 '/'
41 } else {
42 std::path::MAIN_SEPARATOR
43 }
44 }
45 };
46 if path.starts_with(inferred_separator) {
47 components.push(Component::Root);
48 path = &path[1..];
49 }
50 path.split(inferred_separator).for_each(|s| match s {
51 "" => {}
52 "." => components.push(Component::CurrentDir),
53 ".." => components.push(Component::ParentDir),
54 "https:" => components.push(Component::UriScheme("https".to_string())),
55 "http:" => components.push(Component::UriScheme("http".to_string())),
56 _ => components.push(Component::Normal(s.to_string())),
57 });
58 Path {
59 separator: separator.unwrap_or(inferred_separator),
60 components,
61 }
62 }
63
64 pub fn push(&mut self, other: impl Into<Path>) {
65 let other: Path = other.into();
66 if other.is_absolute() {
67 self.components.clear();
68 }
69 self.components.extend(other.components);
70 }
71
72 pub fn is_absolute(&self) -> bool {
73 if self.is_uri() && matches!(self.components.first(), Some(Component::UriScheme(_))) {
74 return true;
75 }
76 self.components.first() == Some(&Component::Root)
77 || (matches!(self.components.first(), Some(Component::DrivePrefix(_)))
78 && matches!(self.components.get(1), Some(Component::Root)))
79 }
80
81 pub fn is_relative(&self) -> bool {
82 !self.is_absolute()
83 }
84
85 pub fn pop(&mut self) -> bool {
86 if self.components.len() > 1 {
87 self.components.pop();
88 true
89 } else {
90 false
91 }
92 }
93
94 pub fn join(&self, other: impl Into<Path>) -> Path {
95 let mut joined = self.clone();
96 joined.push(other);
97 joined
98 }
99
100 pub fn file_name(&self) -> &str {
101 self._file_name(self.components.len() - 1)
102 }
103
104 fn _file_name(&self, idx: usize) -> &str {
105 match self.components.get(idx) {
106 Some(Component::Normal(s)) => s,
107 Some(Component::CurrentDir) => self._file_name(idx - 1),
108 _ => "",
109 }
110 }
111
112 pub fn file_stem(&self) -> &str {
113 let file_name = self.file_name();
114 file_name
115 .rsplit_once('.')
116 .map(|(before, _)| before)
117 .unwrap_or(file_name)
118 }
119
120 pub fn extension(&self) -> &str {
121 let filename = self.file_name();
122 if let Some(idx) = filename.rfind('.') { &filename[idx..] } else { "" }
123 }
124
125 pub fn dirname(&self) -> String {
126 if self.components.len() == 1 && self.components[0] == Component::Root {
127 return self.to_string();
128 }
129 if self.components.len() == 2
130 && matches!(self.components[0], Component::DrivePrefix(_))
131 && self.components[1] == Component::Root
132 {
133 return self.to_string();
134 }
135 let mut path = String::with_capacity(32);
136 for (i, component) in self.components.iter().enumerate() {
137 if i == self.components.len() - 1 {
138 break;
139 }
140 match component {
141 Component::UriScheme(s) => {
142 path.push_str(s);
143 path.push(':');
144 }
145 Component::DrivePrefix(s) => path.push_str(s),
146 Component::Root => path.push(self.separator),
147 Component::CurrentDir => path.push('.'),
148 Component::ParentDir => path.push_str(".."),
149 Component::Normal(s) => path.push_str(s),
150 }
151 if i < self.components.len() - 2
152 && component != &Component::Root
153 && !matches!(component, Component::DrivePrefix(_))
154 {
155 path.push(self.separator);
156 }
157 }
158 path
159 }
160
161 pub fn is_uri(&self) -> bool {
162 matches!(self.components.first(), Some(Component::UriScheme(_)))
163 }
164}
165
166impl From<std::path::PathBuf> for Path {
167 fn from(path: std::path::PathBuf) -> Self {
168 Path::new(path.to_string_lossy())
169 }
170}
171
172impl From<&str> for Path {
173 fn from(path: &str) -> Self {
174 Path::new(path)
175 }
176}
177
178impl std::fmt::Display for Path {
179 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180 let mut path = String::with_capacity(32);
181 for (i, component) in self.components.iter().enumerate() {
182 match component {
183 Component::UriScheme(s) => {
184 path.push_str(s);
185 path.push_str(":/");
186 }
187 Component::DrivePrefix(s) => path.push_str(s),
188 Component::Root => path.push(self.separator),
189 Component::CurrentDir => path.push('.'),
190 Component::ParentDir => path.push_str(".."),
191 Component::Normal(s) => path.push_str(s),
192 }
193 if i < self.components.len() - 1
194 && component != &Component::Root
195 && !matches!(component, Component::DrivePrefix(_))
196 {
197 path.push(self.separator);
198 }
199 }
200 write!(f, "{path}")
201 }
202}
203
204fn drive_prefix(path: &str) -> Option<String> {
205 if path.len() < 2 {
206 return None;
207 }
208 let bytes = path.as_bytes();
209 if bytes[1] == b':' && bytes[0].is_ascii_alphabetic() {
210 let prefix = path[..2].to_string();
211 Some(prefix)
212 } else {
213 None
214 }
215}
216
217#[cfg(test)]
218mod tests {
219 use super::*;
220
221 fn path(s: &str) -> Path {
222 Path::new(s)
223 }
224
225 #[test]
226 fn path_new() {
227 let path = Path::new("/usr/local");
228 assert_eq!(path.separator, '/');
229 assert_eq!(path.components.len(), 3);
230 assert_eq!(path.components[0], Component::Root);
231 assert_eq!(path.components[1], Component::Normal("usr".to_string()));
232 assert_eq!(path.components[2], Component::Normal("local".to_string()));
233 assert_eq!(path.to_string(), "/usr/local");
234 let path = Path::new("/usr/local/");
235 assert_eq!(path.to_string(), "/usr/local");
236 }
237
238 #[test]
239 fn path_new_windows() {
240 let mut path = Path::new(r#"c:\windows\foo.dll"#);
241 assert_eq!(path.separator, '\\');
242 assert_eq!(path.components.len(), 4);
243 assert_eq!(path.components[0], Component::DrivePrefix("c:".to_string()));
244 assert_eq!(path.components[1], Component::Root);
245 assert_eq!(path.components[2], Component::Normal("windows".to_string()));
246 assert_eq!(path.components[3], Component::Normal("foo.dll".to_string()));
247 assert_eq!(path.to_string(), r#"c:\windows\foo.dll"#);
248 path.pop();
249 path.push("baz");
250 path.push("qux.dll");
251 assert_eq!(path.to_string(), r#"c:\windows\baz\qux.dll"#);
252 assert!(!path.is_uri());
253 }
254
255 #[test]
256 fn path_new_uri() {
257 let path = Path::new(r#"https://example.com/path"#);
258 assert_eq!(path.separator, '/');
259 assert_eq!(path.components.len(), 3);
260 assert_eq!(
261 path.components[0],
262 Component::UriScheme("https".to_string())
263 );
264 assert_eq!(
265 path.components[1],
266 Component::Normal("example.com".to_string())
267 );
268 assert_eq!(path.components[2], Component::Normal("path".to_string()));
269 assert_eq!(path.to_string(), "https://example.com/path");
270 assert!(path.is_uri());
271 }
272
273 #[test]
274 fn path_is_absolute() {
275 assert!(path("/usr/local").is_absolute());
276 assert!(!path("usr/local").is_absolute());
277 assert!(path(r#"c:\foo"#).is_absolute());
278 assert!(path(r#"\foo"#).is_absolute());
279 assert!(path(r#"http://foo.com"#).is_absolute());
280 assert!(!path(r#"c:foo"#).is_absolute());
281 }
282
283 #[test]
284 fn path_push_pop() {
285 let mut path = Path::new("/usr/local");
286 path.push("bin");
287 assert_eq!(path.components.len(), 4);
288 assert_eq!(path.components[3], Component::Normal("bin".to_string()));
289 assert_eq!(path.to_string(), "/usr/local/bin");
290 assert!(path.pop());
291 assert_eq!(path.to_string(), "/usr/local");
292 assert!(path.pop());
293 assert_eq!(path.to_string(), "/usr");
294 assert!(path.pop());
295 assert_eq!(path.to_string(), "/");
296 assert!(!path.pop());
297 assert_eq!(path.to_string(), "/");
298 let mut path = Path::new("/usr/local");
300 path.push("/bin");
301 assert_eq!(path.to_string(), "/bin");
302 }
303
304 #[test]
305 fn path_join() {
306 let path = Path::new("/etc");
307 assert_eq!(path.to_string(), "/etc");
308 let joined = path.join("passwd");
309 assert_eq!(path.to_string(), "/etc");
310 assert_eq!(joined.to_string(), "/etc/passwd");
311 }
312
313 #[test]
314 fn path_dirname() {
315 assert_eq!("", &path("foo.txt").dirname());
316 assert_eq!("bar", &path("bar/foo.txt").dirname());
317 assert_eq!("/", &path("/foo.txt").dirname());
318 assert_eq!("/", &path("/").dirname());
319 assert_eq!("c:\\foo", path("c:\\foo\\baz.adoc").dirname());
320 assert_eq!("c:\\", path("c:\\").dirname());
321 }
322
323 #[test]
324 fn path_extension() {
325 assert_eq!(".txt", path("foo/bar/baz.txt").extension());
326 assert_eq!(".asciidoc", path("foo/bar/baz.asciidoc").extension());
327 assert_eq!(".txt", path("baz.txt").extension());
328 assert_eq!("", path("foo/bar/baz").extension());
329 assert_eq!("", path("foo").extension());
330 assert_eq!("", path("foo/b.ar/baz").extension());
331 }
332
333 #[test]
334 fn path_file_name() {
335 assert_eq!("bin", path("bin").file_name());
336 assert_eq!("bin", path("bin/").file_name());
337 assert_eq!("foo.txt", path("tmp/foo.txt").file_name());
338 assert_eq!("foo.txt", path("foo.txt/.").file_name());
339 assert_eq!("foo.txt", path("foo.txt/./././././.").file_name());
340 assert_eq!("foo.txt", path("foo.txt/.//").file_name());
341 assert_eq!("", path("foo.txt/..").file_name());
342 assert_eq!("", path("/").file_name());
343 assert_eq!("", path("c:\\").file_name());
344 assert_eq!("foo", path("c:\\foo").file_name());
345 assert_eq!("foo", path("\\foo").file_name());
346 }
347
348 #[test]
349 fn path_file_stem() {
350 assert_eq!("bin", path("bin").file_stem());
351 assert_eq!("bin", path("bin/").file_stem());
352 assert_eq!("foo", path("foo.rs").file_stem());
353 assert_eq!("foo", path("/weird.txt/foo.bar/foo.rs").file_stem());
354 assert_eq!("foo.tar", path("foo.tar.gz").file_stem());
355 }
356
357 #[test]
358 fn join_uri_relative() {
359 let src = Path::new("https://example.com/foo/bar");
360 let dir = Path::new(src.dirname());
361 assert_eq!(dir.to_string(), "https://example.com/foo");
362 let rel = Path::new("baz");
363 let abs = dir.join(rel);
364 assert_eq!(abs.to_string(), "https://example.com/foo/baz");
365 assert!(abs.is_uri());
366 }
367}