1use crate::paths::{PathStyle, is_absolute};
2use anyhow::{Context as _, Result, anyhow};
3use serde::{Deserialize, Serialize};
4use std::{
5 borrow::{Borrow, Cow},
6 fmt,
7 ops::Deref,
8 path::{Path, PathBuf},
9 sync::Arc,
10};
11
12#[repr(transparent)]
23#[derive(PartialEq, Eq, Hash, Serialize)]
24pub struct RelPath(str);
25
26#[derive(Clone, Serialize, Deserialize)]
31pub struct RelPathBuf(String);
32
33impl RelPath {
34 pub fn empty() -> &'static Self {
36 Self::new_unchecked("")
37 }
38
39 #[track_caller]
47 pub fn new<'a>(path: &'a Path, path_style: PathStyle) -> Result<Cow<'a, Self>> {
48 let mut path = path.to_str().context("non utf-8 path")?;
49
50 let (prefixes, suffixes): (&[_], &[_]) = match path_style {
51 PathStyle::Posix => (&["./"], &['/']),
52 PathStyle::Windows => (&["./", ".\\"], &['/', '\\']),
53 };
54
55 while prefixes.iter().any(|prefix| path.starts_with(prefix)) {
56 path = &path[prefixes[0].len()..];
57 }
58 while let Some(prefix) = path.strip_suffix(suffixes)
59 && !prefix.is_empty()
60 {
61 path = prefix;
62 }
63
64 if is_absolute(&path, path_style) {
65 return Err(anyhow!("absolute path not allowed: {path:?}"));
66 }
67
68 let mut string = Cow::Borrowed(path);
69 if path_style == PathStyle::Windows && path.contains('\\') {
70 string = Cow::Owned(string.as_ref().replace('\\', "/"))
71 }
72
73 let mut result = match string {
74 Cow::Borrowed(string) => Cow::Borrowed(Self::new_unchecked(string)),
75 Cow::Owned(string) => Cow::Owned(RelPathBuf(string)),
76 };
77
78 if result
79 .components()
80 .any(|component| component == "" || component == "." || component == "..")
81 {
82 let mut normalized = RelPathBuf::new();
83 for component in result.components() {
84 match component {
85 "" => {}
86 "." => {}
87 ".." => {
88 if !normalized.pop() {
89 return Err(anyhow!("path is not relative: {result:?}"));
90 }
91 }
92 other => normalized.push(RelPath::new_unchecked(other)),
93 }
94 }
95 result = Cow::Owned(normalized)
96 }
97
98 Ok(result)
99 }
100
101 #[track_caller]
106 pub fn unix<S: AsRef<Path> + ?Sized>(path: &S) -> anyhow::Result<&Self> {
107 let path = path.as_ref();
108 match Self::new(path, PathStyle::Posix)? {
109 Cow::Borrowed(path) => Ok(path),
110 Cow::Owned(_) => Err(anyhow!("invalid relative path {path:?}")),
111 }
112 }
113
114 fn new_unchecked(s: &str) -> &Self {
115 unsafe { &*(s as *const str as *const Self) }
117 }
118
119 pub fn is_empty(&self) -> bool {
120 self.0.is_empty()
121 }
122
123 pub fn components(&self) -> RelPathComponents<'_> {
124 RelPathComponents(&self.0)
125 }
126
127 pub fn ancestors(&self) -> RelPathAncestors<'_> {
128 RelPathAncestors(Some(&self.0))
129 }
130
131 pub fn file_name(&self) -> Option<&str> {
132 self.components().next_back()
133 }
134
135 pub fn file_stem(&self) -> Option<&str> {
136 Some(self.as_std_path().file_stem()?.to_str().unwrap())
137 }
138
139 pub fn extension(&self) -> Option<&str> {
140 Some(self.as_std_path().extension()?.to_str().unwrap())
141 }
142
143 pub fn parent(&self) -> Option<&Self> {
144 let mut components = self.components();
145 components.next_back()?;
146 Some(components.rest())
147 }
148
149 pub fn starts_with(&self, other: &Self) -> bool {
150 self.strip_prefix(other).is_ok()
151 }
152
153 pub fn ends_with(&self, other: &Self) -> bool {
154 if let Some(suffix) = self.0.strip_suffix(&other.0) {
155 if suffix.ends_with('/') {
156 return true;
157 } else if suffix.is_empty() {
158 return true;
159 }
160 }
161 false
162 }
163
164 pub fn strip_prefix<'a>(&'a self, other: &Self) -> Result<&'a Self> {
165 if other.is_empty() {
166 return Ok(self);
167 }
168 if let Some(suffix) = self.0.strip_prefix(&other.0) {
169 if let Some(suffix) = suffix.strip_prefix('/') {
170 return Ok(Self::new_unchecked(suffix));
171 } else if suffix.is_empty() {
172 return Ok(Self::empty());
173 }
174 }
175 Err(anyhow!("failed to strip prefix: {other:?} from {self:?}"))
176 }
177
178 pub fn len(&self) -> usize {
179 self.0.matches('/').count() + 1
180 }
181
182 pub fn last_n_components(&self, count: usize) -> Option<&Self> {
183 let len = self.len();
184 if len >= count {
185 let mut components = self.components();
186 for _ in 0..(len - count) {
187 components.next()?;
188 }
189 Some(components.rest())
190 } else {
191 None
192 }
193 }
194
195 pub fn join(&self, other: &Self) -> Arc<Self> {
196 let result = if self.0.is_empty() {
197 Cow::Borrowed(&other.0)
198 } else if other.0.is_empty() {
199 Cow::Borrowed(&self.0)
200 } else {
201 Cow::Owned(format!("{}/{}", &self.0, &other.0))
202 };
203 Arc::from(Self::new_unchecked(result.as_ref()))
204 }
205
206 pub fn to_rel_path_buf(&self) -> RelPathBuf {
207 RelPathBuf(self.0.to_string())
208 }
209
210 pub fn into_arc(&self) -> Arc<Self> {
211 Arc::from(self)
212 }
213
214 pub fn to_proto(&self) -> String {
216 self.as_unix_str().to_owned()
217 }
218
219 pub fn from_proto(path: &str) -> Result<Arc<Self>> {
221 Ok(Arc::from(Self::unix(path)?))
222 }
223
224 pub fn display(&self, style: PathStyle) -> Cow<'_, str> {
229 match style {
230 PathStyle::Posix => Cow::Borrowed(&self.0),
231 PathStyle::Windows => Cow::Owned(self.0.replace('/', "\\")),
232 }
233 }
234
235 pub fn as_unix_str(&self) -> &str {
239 &self.0
240 }
241
242 pub fn as_std_path(&self) -> &Path {
249 Path::new(&self.0)
250 }
251}
252
253impl ToOwned for RelPath {
254 type Owned = RelPathBuf;
255
256 fn to_owned(&self) -> Self::Owned {
257 self.to_rel_path_buf()
258 }
259}
260
261impl Borrow<RelPath> for RelPathBuf {
262 fn borrow(&self) -> &RelPath {
263 self.as_rel_path()
264 }
265}
266
267impl PartialOrd for RelPath {
268 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
269 Some(self.cmp(other))
270 }
271}
272
273impl Ord for RelPath {
274 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
275 self.components().cmp(other.components())
276 }
277}
278
279impl fmt::Debug for RelPath {
280 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
281 fmt::Debug::fmt(&self.0, f)
282 }
283}
284
285impl fmt::Debug for RelPathBuf {
286 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
287 fmt::Debug::fmt(&self.0, f)
288 }
289}
290
291impl RelPathBuf {
292 pub fn new() -> Self {
293 Self(String::new())
294 }
295
296 pub fn pop(&mut self) -> bool {
297 if let Some(ix) = self.0.rfind('/') {
298 self.0.truncate(ix);
299 true
300 } else if !self.is_empty() {
301 self.0.clear();
302 true
303 } else {
304 false
305 }
306 }
307
308 pub fn push(&mut self, path: &RelPath) {
309 if !self.is_empty() {
310 self.0.push('/');
311 }
312 self.0.push_str(&path.0);
313 }
314
315 pub fn as_rel_path(&self) -> &RelPath {
316 RelPath::new_unchecked(self.0.as_str())
317 }
318
319 pub fn set_extension(&mut self, extension: &str) -> bool {
320 if let Some(filename) = self.file_name() {
321 let mut filename = PathBuf::from(filename);
322 filename.set_extension(extension);
323 self.pop();
324 self.0.push_str(filename.to_str().unwrap());
325 true
326 } else {
327 false
328 }
329 }
330}
331
332impl Into<Arc<RelPath>> for RelPathBuf {
333 fn into(self) -> Arc<RelPath> {
334 Arc::from(self.as_rel_path())
335 }
336}
337
338impl AsRef<RelPath> for RelPathBuf {
339 fn as_ref(&self) -> &RelPath {
340 self.as_rel_path()
341 }
342}
343
344impl Deref for RelPathBuf {
345 type Target = RelPath;
346
347 fn deref(&self) -> &Self::Target {
348 self.as_ref()
349 }
350}
351
352impl<'a> From<&'a RelPath> for Cow<'a, RelPath> {
353 fn from(value: &'a RelPath) -> Self {
354 Self::Borrowed(value)
355 }
356}
357
358impl From<&RelPath> for Arc<RelPath> {
359 fn from(rel_path: &RelPath) -> Self {
360 let bytes: Arc<str> = Arc::from(&rel_path.0);
361 unsafe { Arc::from_raw(Arc::into_raw(bytes) as *const RelPath) }
362 }
363}
364
365#[cfg(any(test, feature = "test-support"))]
366#[track_caller]
367pub fn rel_path(path: &str) -> &RelPath {
368 RelPath::unix(path).unwrap()
369}
370
371impl PartialEq<str> for RelPath {
372 fn eq(&self, other: &str) -> bool {
373 self.0 == *other
374 }
375}
376
377pub struct RelPathComponents<'a>(&'a str);
378
379pub struct RelPathAncestors<'a>(Option<&'a str>);
380
381const SEPARATOR: char = '/';
382
383impl<'a> RelPathComponents<'a> {
384 pub fn rest(&self) -> &'a RelPath {
385 RelPath::new_unchecked(self.0)
386 }
387}
388
389impl<'a> Iterator for RelPathComponents<'a> {
390 type Item = &'a str;
391
392 fn next(&mut self) -> Option<Self::Item> {
393 if let Some(sep_ix) = self.0.find(SEPARATOR) {
394 let (head, tail) = self.0.split_at(sep_ix);
395 self.0 = &tail[1..];
396 Some(head)
397 } else if self.0.is_empty() {
398 None
399 } else {
400 let result = self.0;
401 self.0 = "";
402 Some(result)
403 }
404 }
405}
406
407impl<'a> Iterator for RelPathAncestors<'a> {
408 type Item = &'a RelPath;
409
410 fn next(&mut self) -> Option<Self::Item> {
411 let result = self.0?;
412 if let Some(sep_ix) = result.rfind(SEPARATOR) {
413 self.0 = Some(&result[..sep_ix]);
414 } else if !result.is_empty() {
415 self.0 = Some("");
416 } else {
417 self.0 = None;
418 }
419 Some(RelPath::new_unchecked(result))
420 }
421}
422
423impl<'a> DoubleEndedIterator for RelPathComponents<'a> {
424 fn next_back(&mut self) -> Option<Self::Item> {
425 if let Some(sep_ix) = self.0.rfind(SEPARATOR) {
426 let (head, tail) = self.0.split_at(sep_ix);
427 self.0 = head;
428 Some(&tail[1..])
429 } else if self.0.is_empty() {
430 None
431 } else {
432 let result = self.0;
433 self.0 = "";
434 Some(result)
435 }
436 }
437}
438
439#[cfg(test)]
440mod tests {
441 use super::*;
442 use itertools::Itertools;
443 use pretty_assertions::assert_matches;
444
445 #[test]
446 fn test_rel_path_new() {
447 assert!(RelPath::new(Path::new("/"), PathStyle::local()).is_err());
448 assert!(RelPath::new(Path::new("//"), PathStyle::local()).is_err());
449 assert!(RelPath::new(Path::new("/foo/"), PathStyle::local()).is_err());
450
451 let path = RelPath::new("foo/".as_ref(), PathStyle::local()).unwrap();
452 assert_eq!(path, rel_path("foo").into());
453 assert_matches!(path, Cow::Borrowed(_));
454
455 let path = RelPath::new("foo\\".as_ref(), PathStyle::Windows).unwrap();
456 assert_eq!(path, rel_path("foo").into());
457 assert_matches!(path, Cow::Borrowed(_));
458
459 assert_eq!(
460 RelPath::new("foo/bar/../baz/./quux/".as_ref(), PathStyle::local())
461 .unwrap()
462 .as_ref(),
463 rel_path("foo/baz/quux")
464 );
465
466 let path = RelPath::new("./foo/bar".as_ref(), PathStyle::Posix).unwrap();
467 assert_eq!(path.as_ref(), rel_path("foo/bar"));
468 assert_matches!(path, Cow::Borrowed(_));
469
470 let path = RelPath::new(".\\foo".as_ref(), PathStyle::Windows).unwrap();
471 assert_eq!(path, rel_path("foo").into());
472 assert_matches!(path, Cow::Borrowed(_));
473
474 let path = RelPath::new("./.\\./foo/\\/".as_ref(), PathStyle::Windows).unwrap();
475 assert_eq!(path, rel_path("foo").into());
476 assert_matches!(path, Cow::Borrowed(_));
477
478 let path = RelPath::new("foo/./bar".as_ref(), PathStyle::Posix).unwrap();
479 assert_eq!(path.as_ref(), rel_path("foo/bar"));
480 assert_matches!(path, Cow::Owned(_));
481
482 let path = RelPath::new("./foo/bar".as_ref(), PathStyle::Windows).unwrap();
483 assert_eq!(path.as_ref(), rel_path("foo/bar"));
484 assert_matches!(path, Cow::Borrowed(_));
485
486 let path = RelPath::new(".\\foo\\bar".as_ref(), PathStyle::Windows).unwrap();
487 assert_eq!(path.as_ref(), rel_path("foo/bar"));
488 assert_matches!(path, Cow::Owned(_));
489 }
490
491 #[test]
492 fn test_rel_path_components() {
493 let path = rel_path("foo/bar/baz");
494 assert_eq!(
495 path.components().collect::<Vec<_>>(),
496 vec!["foo", "bar", "baz"]
497 );
498 assert_eq!(
499 path.components().rev().collect::<Vec<_>>(),
500 vec!["baz", "bar", "foo"]
501 );
502
503 let path = rel_path("");
504 let mut components = path.components();
505 assert_eq!(components.next(), None);
506 }
507
508 #[test]
509 fn test_rel_path_ancestors() {
510 let path = rel_path("foo/bar/baz");
511 let mut ancestors = path.ancestors();
512 assert_eq!(ancestors.next(), Some(rel_path("foo/bar/baz")));
513 assert_eq!(ancestors.next(), Some(rel_path("foo/bar")));
514 assert_eq!(ancestors.next(), Some(rel_path("foo")));
515 assert_eq!(ancestors.next(), Some(rel_path("")));
516 assert_eq!(ancestors.next(), None);
517
518 let path = rel_path("foo");
519 let mut ancestors = path.ancestors();
520 assert_eq!(ancestors.next(), Some(rel_path("foo")));
521 assert_eq!(ancestors.next(), Some(RelPath::empty()));
522 assert_eq!(ancestors.next(), None);
523
524 let path = RelPath::empty();
525 let mut ancestors = path.ancestors();
526 assert_eq!(ancestors.next(), Some(RelPath::empty()));
527 assert_eq!(ancestors.next(), None);
528 }
529
530 #[test]
531 fn test_rel_path_parent() {
532 assert_eq!(rel_path("foo/bar/baz").parent(), Some(rel_path("foo/bar")));
533 assert_eq!(rel_path("foo").parent(), Some(RelPath::empty()));
534 assert_eq!(rel_path("").parent(), None);
535 }
536
537 #[test]
538 fn test_rel_path_partial_ord_is_compatible_with_std() {
539 let test_cases = ["a/b/c", "relative/path/with/dot.", "relative/path/with.dot"];
540 for [lhs, rhs] in test_cases.iter().array_combinations::<2>() {
541 assert_eq!(
542 Path::new(lhs).cmp(Path::new(rhs)),
543 RelPath::unix(lhs)
544 .unwrap()
545 .cmp(&RelPath::unix(rhs).unwrap())
546 );
547 }
548 }
549
550 #[test]
551 fn test_strip_prefix() {
552 let parent = rel_path("");
553 let child = rel_path(".foo");
554
555 assert!(child.starts_with(parent));
556 assert_eq!(child.strip_prefix(parent).unwrap(), child);
557 }
558
559 #[test]
560 fn test_rel_path_constructors_absolute_path() {
561 assert!(RelPath::new(Path::new("/a/b"), PathStyle::Windows).is_err());
562 assert!(RelPath::new(Path::new("\\a\\b"), PathStyle::Windows).is_err());
563 assert!(RelPath::new(Path::new("/a/b"), PathStyle::Posix).is_err());
564 assert!(RelPath::new(Path::new("C:/a/b"), PathStyle::Windows).is_err());
565 assert!(RelPath::new(Path::new("C:\\a\\b"), PathStyle::Windows).is_err());
566 assert!(RelPath::new(Path::new("C:/a/b"), PathStyle::Posix).is_ok());
567 }
568
569 #[test]
570 fn test_pop() {
571 let mut path = rel_path("a/b").to_rel_path_buf();
572 path.pop();
573 assert_eq!(path.as_rel_path().as_unix_str(), "a");
574 path.pop();
575 assert_eq!(path.as_rel_path().as_unix_str(), "");
576 path.pop();
577 assert_eq!(path.as_rel_path().as_unix_str(), "");
578 }
579}