1use std::{
2 borrow::Cow,
3 ffi::OsStr,
4 path::{Display, Path, PathBuf},
5 sync::Arc,
6};
7
8#[repr(transparent)]
9#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct RelPath([u8]);
11
12impl RelPath {
13 pub fn new<S: AsRef<[u8]> + ?Sized>(s: &S) -> &Self {
14 unsafe { &*(s.as_ref() as *const [u8] as *const Self) }
15 }
16
17 pub fn components(&self) -> RelPathComponents {
18 RelPathComponents(&self.0)
19 }
20
21 pub fn file_name(&self) -> Option<&[u8]> {
22 self.components().next_back()
23 }
24
25 pub fn parent(&self) -> Option<&Self> {
26 let mut components = self.components();
27 components.next_back()?;
28 Some(Self::new(components.0))
29 }
30
31 pub fn starts_with(&self, other: &Self) -> bool {
32 let mut components = self.components();
33 other.components().all(|other_component| {
34 components
35 .next()
36 .map_or(false, |component| component == other_component)
37 })
38 }
39
40 pub fn strip_prefix(&self, other: &Self) -> Result<&Self, ()> {
41 let mut components = self.components();
42 other
43 .components()
44 .all(|other_component| {
45 components
46 .next()
47 .map_or(false, |component| component == other_component)
48 })
49 .then(|| Self::new(components.0))
50 .ok_or_else(|| ())
51 }
52
53 pub fn append_to_abs_path(&self, abs_path: &Path) -> PathBuf {
54 // TODO: implement this differently
55 let mut result = abs_path.to_path_buf();
56 for component in self.components() {
57 result.push(String::from_utf8_lossy(component).as_ref());
58 }
59 result
60 }
61
62 pub fn to_proto(&self) -> String {
63 String::from_utf8_lossy(&self.0).to_string()
64 }
65
66 pub fn as_bytes(&self) -> &[u8] {
67 &self.0
68 }
69
70 pub fn as_os_str(&self) -> Cow<'_, OsStr> {
71 #[cfg(target_os = "windows")]
72 {
73 use std::ffi::OsString;
74 let path = String::from_utf8_lossy(&self.0);
75 match path {
76 Cow::Borrowed(s) => Cow::Borrowed(OsStr::new(s)),
77 Cow::Owned(s) => Cow::Owned(OsString::from(s)),
78 }
79 }
80 #[cfg(not(target_os = "windows"))]
81 {
82 Cow::Borrowed(self.0.as_os_str())
83 }
84 }
85}
86
87impl From<&RelPath> for Arc<RelPath> {
88 fn from(rel_path: &RelPath) -> Self {
89 let bytes: Arc<[u8]> = Arc::from(&rel_path.0);
90 unsafe { Arc::from_raw(Arc::into_raw(bytes) as *const RelPath) }
91 }
92}
93
94impl AsRef<RelPath> for &str {
95 fn as_ref(&self) -> &RelPath {
96 RelPath::new(self)
97 }
98}
99
100impl std::fmt::Debug for RelPath {
101 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102 if let Ok(str) = std::str::from_utf8(&self.0) {
103 write!(f, "RelPath({})", str)
104 } else {
105 write!(f, "RelPath({:?})", &self.0)
106 }
107 }
108}
109
110pub struct RelPathComponents<'a>(&'a [u8]);
111
112const SEPARATOR: u8 = b'/';
113
114impl<'a> Iterator for RelPathComponents<'a> {
115 type Item = &'a [u8];
116
117 fn next(&mut self) -> Option<Self::Item> {
118 if let Some(sep_ix) = self.0.iter().position(|&byte| byte == SEPARATOR) {
119 let (head, tail) = self.0.split_at(sep_ix);
120 self.0 = &tail[1..];
121 Some(head)
122 } else if self.0.is_empty() {
123 None
124 } else {
125 let result = self.0;
126 self.0 = &[];
127 Some(result)
128 }
129 }
130}
131
132impl<'a> DoubleEndedIterator for RelPathComponents<'a> {
133 fn next_back(&mut self) -> Option<Self::Item> {
134 if let Some(sep_ix) = self.0.iter().rposition(|&byte| byte == SEPARATOR) {
135 let (head, tail) = self.0.split_at(sep_ix);
136 self.0 = head;
137 Some(&tail[1..])
138 } else if self.0.is_empty() {
139 None
140 } else {
141 let result = self.0;
142 self.0 = &[];
143 Some(result)
144 }
145 }
146}
147
148#[cfg(test)]
149mod tests {
150 use super::*;
151
152 #[test]
153 fn test_rel_path_components() {
154 let path = RelPath::new("foo/bar/baz");
155 let mut components = path.components();
156 assert_eq!(components.next(), Some("foo".as_bytes()));
157 assert_eq!(components.next(), Some("bar".as_bytes()));
158 assert_eq!(components.next(), Some("baz".as_bytes()));
159 assert_eq!(components.next(), None);
160 }
161
162 #[test]
163 fn test_rel_path_parent() {
164 assert_eq!(
165 RelPath::new("foo/bar/baz").parent().unwrap(),
166 RelPath::new("foo/bar")
167 );
168 assert_eq!(RelPath::new("foo").parent().unwrap(), RelPath::new(""));
169 assert_eq!(RelPath::new("").parent(), None);
170 }
171}