interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / uefi / src / fs / path / pathbuf.rs

206 lines · 5514 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use crate::fs::SEPARATOR;
4use crate::fs::path::Path;
5use crate::{CStr16, CString16, Char16, char16};
6use core::fmt::{self, Display, Formatter};
7
8/// A path buffer similar to the `PathBuf` of the standard library, but based on
9/// [`CString16`] strings and [`SEPARATOR`] as separator.
10///
11/// `/` is replaced by [`SEPARATOR`] on the fly.
12#[derive(Clone, Debug, Default, Eq, PartialOrd, Ord)]
13pub struct PathBuf(CString16);
14
15impl PathBuf {
16 /// Constructor.
17 #[must_use]
18 pub fn new() -> Self {
19 Self::default()
20 }
21
22 /// Truncates the path to zero length.
23 pub fn clear(&mut self) {
24 self.0.clear();
25 }
26
27 /// Constructor that replaces all occurrences of `/` with `\`.
28 fn new_from_cstring16(mut string: CString16) -> Self {
29 const SEARCH: Char16 = char16!('/');
30 string.replace_char(SEARCH, SEPARATOR);
31 Self(string)
32 }
33
34 /// Extends self with path.
35 ///
36 /// UNIX separators (`/`) will be replaced by [`SEPARATOR`] on the fly.
37 pub fn push<P: AsRef<Path>>(&mut self, path: P) {
38 const SEARCH: Char16 = char16!('/');
39
40 // do nothing on empty path
41 if path.as_ref().is_empty() {
42 return;
43 }
44
45 let empty = self.0.is_empty();
46 let needs_sep = *self
47 .0
48 .as_slice_with_nul()
49 .last()
50 .expect("Should have at least null character")
51 != SEPARATOR;
52 if !empty && needs_sep {
53 self.0.push(SEPARATOR)
54 }
55
56 self.0.push_str(path.as_ref().to_cstr16());
57 self.0.replace_char(SEARCH, SEPARATOR);
58 }
59}
60
61impl PartialEq for PathBuf {
62 fn eq(&self, other: &Self) -> bool {
63 let path1: &Path = self.as_ref();
64 let path2: &Path = other.as_ref();
65 path1 == path2
66 }
67}
68
69impl Display for PathBuf {
70 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
71 Display::fmt(self.to_cstr16(), f)
72 }
73}
74
75mod convenience_impls {
76 use super::*;
77 use core::borrow::Borrow;
78 use core::ops::Deref;
79
80 impl From<CString16> for PathBuf {
81 fn from(value: CString16) -> Self {
82 Self::new_from_cstring16(value)
83 }
84 }
85
86 impl From<&CStr16> for PathBuf {
87 fn from(value: &CStr16) -> Self {
88 Self::new_from_cstring16(CString16::from(value))
89 }
90 }
91
92 impl Deref for PathBuf {
93 type Target = Path;
94
95 fn deref(&self) -> &Self::Target {
96 Path::new(&self.0)
97 }
98 }
99
100 impl AsRef<Path> for PathBuf {
101 fn as_ref(&self) -> &Path {
102 // falls back to deref impl
103 self
104 }
105 }
106
107 impl Borrow<Path> for PathBuf {
108 fn borrow(&self) -> &Path {
109 // falls back to deref impl
110 self
111 }
112 }
113
114 impl AsRef<CStr16> for PathBuf {
115 fn as_ref(&self) -> &CStr16 {
116 &self.0
117 }
118 }
119
120 impl Borrow<CStr16> for PathBuf {
121 fn borrow(&self) -> &CStr16 {
122 &self.0
123 }
124 }
125}
126
127#[cfg(test)]
128mod tests {
129 use super::*;
130 use crate::cstr16;
131 use alloc::string::ToString;
132
133 #[test]
134 fn from_cstr16() {
135 let source: &CStr16 = cstr16!("\\hello\\foo\\bar");
136 let _path: PathBuf = source.into();
137 }
138
139 #[test]
140 fn from_cstring16() {
141 let source = CString16::try_from("\\hello\\foo\\bar").unwrap();
142 let _path: PathBuf = source.as_ref().into();
143 let _path: PathBuf = source.clone().into();
144 let _path: PathBuf = PathBuf::new_from_cstring16(source);
145 }
146
147 #[test]
148 fn from_std_string() {
149 let std_string = "\\hello\\foo\\bar".to_string();
150 let _path = PathBuf::new_from_cstring16(CString16::try_from(std_string.as_str()).unwrap());
151 }
152
153 #[test]
154 fn push() {
155 let mut pathbuf = PathBuf::new();
156 pathbuf.push(cstr16!("first"));
157 pathbuf.push(cstr16!("second"));
158 pathbuf.push(cstr16!("third"));
159 assert_eq!(pathbuf.to_cstr16(), cstr16!("first\\second\\third"));
160
161 let mut pathbuf = PathBuf::new();
162 pathbuf.push(cstr16!("\\first"));
163 pathbuf.push(cstr16!("second"));
164 assert_eq!(pathbuf.to_cstr16(), cstr16!("\\first\\second"));
165
166 // empty pushes should be ignored and have no effect
167 let empty_cstring16 = CString16::try_from("").unwrap();
168 let mut pathbuf = PathBuf::new();
169 pathbuf.push(cstr16!("first"));
170 pathbuf.push(empty_cstring16.as_ref());
171 pathbuf.push(empty_cstring16.as_ref());
172 pathbuf.push(empty_cstring16.as_ref());
173 pathbuf.push(cstr16!("second"));
174 assert_eq!(pathbuf.to_cstr16(), cstr16!("first\\second"));
175 }
176
177 #[test]
178 fn partial_eq() {
179 let mut pathbuf1 = PathBuf::new();
180 pathbuf1.push(cstr16!("first"));
181 pathbuf1.push(cstr16!("second"));
182 pathbuf1.push(cstr16!("third"));
183
184 assert_eq!(pathbuf1, pathbuf1);
185
186 let mut pathbuf2 = PathBuf::new();
187 pathbuf2.push(cstr16!("\\first"));
188 pathbuf2.push(cstr16!("second"));
189
190 assert_eq!(pathbuf2, pathbuf2);
191 assert_ne!(pathbuf1, pathbuf2);
192 }
193
194 #[test]
195 fn clear() {
196 let mut pathbuf = PathBuf::new();
197 pathbuf.push(cstr16!("first"));
198 assert_eq!(cstr16!("first"), pathbuf.to_cstr16());
199
200 pathbuf.clear();
201 assert_eq!(cstr16!(""), pathbuf.to_cstr16());
202
203 pathbuf.clear();
204 assert_eq!(cstr16!(""), pathbuf.to_cstr16());
205 }
206}