##// END OF EJS Templates
rustdoc: summary line for hg_path_to_os_string...
Georges Racinet -
r51274:d27b6fc7 default
parent child Browse files
Show More
@@ -1,812 +1,816
1 1 // hg_path.rs
2 2 //
3 3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4 4 //
5 5 // This software may be used and distributed according to the terms of the
6 6 // GNU General Public License version 2 or any later version.
7 7
8 8 use crate::utils::SliceExt;
9 9 use std::borrow::Borrow;
10 10 use std::borrow::Cow;
11 11 use std::ffi::{OsStr, OsString};
12 12 use std::fmt;
13 13 use std::ops::Deref;
14 14 use std::path::{Path, PathBuf};
15 15
16 16 #[derive(Debug, Eq, PartialEq)]
17 17 pub enum HgPathError {
18 18 /// Bytes from the invalid `HgPath`
19 19 LeadingSlash(Vec<u8>),
20 20 ConsecutiveSlashes {
21 21 bytes: Vec<u8>,
22 22 second_slash_index: usize,
23 23 },
24 24 ContainsNullByte {
25 25 bytes: Vec<u8>,
26 26 null_byte_index: usize,
27 27 },
28 28 /// Bytes
29 29 DecodeError(Vec<u8>),
30 30 /// The rest come from audit errors
31 31 EndsWithSlash(HgPathBuf),
32 32 ContainsIllegalComponent(HgPathBuf),
33 33 /// Path is inside the `.hg` folder
34 34 InsideDotHg(HgPathBuf),
35 35 IsInsideNestedRepo {
36 36 path: HgPathBuf,
37 37 nested_repo: HgPathBuf,
38 38 },
39 39 TraversesSymbolicLink {
40 40 path: HgPathBuf,
41 41 symlink: HgPathBuf,
42 42 },
43 43 NotFsCompliant(HgPathBuf),
44 44 /// `path` is the smallest invalid path
45 45 NotUnderRoot {
46 46 path: PathBuf,
47 47 root: PathBuf,
48 48 },
49 49 }
50 50
51 51 impl fmt::Display for HgPathError {
52 52 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53 53 match self {
54 54 HgPathError::LeadingSlash(bytes) => {
55 55 write!(f, "Invalid HgPath '{:?}': has a leading slash.", bytes)
56 56 }
57 57 HgPathError::ConsecutiveSlashes {
58 58 bytes,
59 59 second_slash_index: pos,
60 60 } => write!(
61 61 f,
62 62 "Invalid HgPath '{:?}': consecutive slashes at pos {}.",
63 63 bytes, pos
64 64 ),
65 65 HgPathError::ContainsNullByte {
66 66 bytes,
67 67 null_byte_index: pos,
68 68 } => write!(
69 69 f,
70 70 "Invalid HgPath '{:?}': contains null byte at pos {}.",
71 71 bytes, pos
72 72 ),
73 73 HgPathError::DecodeError(bytes) => write!(
74 74 f,
75 75 "Invalid HgPath '{:?}': could not be decoded.",
76 76 bytes
77 77 ),
78 78 HgPathError::EndsWithSlash(path) => {
79 79 write!(f, "Audit failed for '{}': ends with a slash.", path)
80 80 }
81 81 HgPathError::ContainsIllegalComponent(path) => write!(
82 82 f,
83 83 "Audit failed for '{}': contains an illegal component.",
84 84 path
85 85 ),
86 86 HgPathError::InsideDotHg(path) => write!(
87 87 f,
88 88 "Audit failed for '{}': is inside the '.hg' folder.",
89 89 path
90 90 ),
91 91 HgPathError::IsInsideNestedRepo {
92 92 path,
93 93 nested_repo: nested,
94 94 } => {
95 95 write!(f,
96 96 "Audit failed for '{}': is inside a nested repository '{}'.",
97 97 path, nested
98 98 )
99 99 }
100 100 HgPathError::TraversesSymbolicLink { path, symlink } => write!(
101 101 f,
102 102 "Audit failed for '{}': traverses symbolic link '{}'.",
103 103 path, symlink
104 104 ),
105 105 HgPathError::NotFsCompliant(path) => write!(
106 106 f,
107 107 "Audit failed for '{}': cannot be turned into a \
108 108 filesystem path.",
109 109 path
110 110 ),
111 111 HgPathError::NotUnderRoot { path, root } => write!(
112 112 f,
113 113 "Audit failed for '{}': not under root {}.",
114 114 path.display(),
115 115 root.display()
116 116 ),
117 117 }
118 118 }
119 119 }
120 120
121 121 impl From<HgPathError> for std::io::Error {
122 122 fn from(e: HgPathError) -> Self {
123 123 std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())
124 124 }
125 125 }
126 126
127 127 /// This is a repository-relative path (or canonical path):
128 128 /// - no null characters
129 129 /// - `/` separates directories
130 130 /// - no consecutive slashes
131 131 /// - no leading slash,
132 132 /// - no `.` nor `..` of special meaning
133 133 /// - stored in repository and shared across platforms
134 134 ///
135 135 /// Note: there is no guarantee of any `HgPath` being well-formed at any point
136 136 /// in its lifetime for performance reasons and to ease ergonomics. It is
137 137 /// however checked using the `check_state` method before any file-system
138 138 /// operation.
139 139 ///
140 140 /// This allows us to be encoding-transparent as much as possible, until really
141 141 /// needed; `HgPath` can be transformed into a platform-specific path (`OsStr`
142 142 /// or `Path`) whenever more complex operations are needed:
143 143 /// On Unix, it's just byte-to-byte conversion. On Windows, it has to be
144 144 /// decoded from MBCS to WTF-8. If WindowsUTF8Plan is implemented, the source
145 145 /// character encoding will be determined on a per-repository basis.
146 146 #[derive(Eq, Ord, PartialEq, PartialOrd, Hash)]
147 147 #[repr(transparent)]
148 148 pub struct HgPath {
149 149 inner: [u8],
150 150 }
151 151
152 152 impl HgPath {
153 153 pub fn new<S: AsRef<[u8]> + ?Sized>(s: &S) -> &Self {
154 154 unsafe { &*(s.as_ref() as *const [u8] as *const Self) }
155 155 }
156 156 pub fn is_empty(&self) -> bool {
157 157 self.inner.is_empty()
158 158 }
159 159 pub fn len(&self) -> usize {
160 160 self.inner.len()
161 161 }
162 162 fn to_hg_path_buf(&self) -> HgPathBuf {
163 163 HgPathBuf {
164 164 inner: self.inner.to_owned(),
165 165 }
166 166 }
167 167 pub fn bytes(&self) -> std::slice::Iter<u8> {
168 168 self.inner.iter()
169 169 }
170 170 pub fn to_ascii_uppercase(&self) -> HgPathBuf {
171 171 HgPathBuf::from(self.inner.to_ascii_uppercase())
172 172 }
173 173 pub fn to_ascii_lowercase(&self) -> HgPathBuf {
174 174 HgPathBuf::from(self.inner.to_ascii_lowercase())
175 175 }
176 176 pub fn as_bytes(&self) -> &[u8] {
177 177 &self.inner
178 178 }
179 179 pub fn contains(&self, other: u8) -> bool {
180 180 self.inner.contains(&other)
181 181 }
182 182 pub fn starts_with(&self, needle: impl AsRef<Self>) -> bool {
183 183 self.inner.starts_with(needle.as_ref().as_bytes())
184 184 }
185 185 pub fn trim_trailing_slash(&self) -> &Self {
186 186 Self::new(if self.inner.last() == Some(&b'/') {
187 187 &self.inner[..self.inner.len() - 1]
188 188 } else {
189 189 &self.inner[..]
190 190 })
191 191 }
192 192 /// Returns a tuple of slices `(base, filename)` resulting from the split
193 193 /// at the rightmost `/`, if any.
194 194 ///
195 195 /// # Examples:
196 196 ///
197 197 /// ```
198 198 /// use hg::utils::hg_path::HgPath;
199 199 ///
200 200 /// let path = HgPath::new(b"cool/hg/path").split_filename();
201 201 /// assert_eq!(path, (HgPath::new(b"cool/hg"), HgPath::new(b"path")));
202 202 ///
203 203 /// let path = HgPath::new(b"pathwithoutsep").split_filename();
204 204 /// assert_eq!(path, (HgPath::new(b""), HgPath::new(b"pathwithoutsep")));
205 205 /// ```
206 206 pub fn split_filename(&self) -> (&Self, &Self) {
207 207 match &self.inner.iter().rposition(|c| *c == b'/') {
208 208 None => (HgPath::new(""), self),
209 209 Some(size) => (
210 210 HgPath::new(&self.inner[..*size]),
211 211 HgPath::new(&self.inner[*size + 1..]),
212 212 ),
213 213 }
214 214 }
215 215
216 216 pub fn join(&self, path: &HgPath) -> HgPathBuf {
217 217 let mut buf = self.to_owned();
218 218 buf.push(path);
219 219 buf
220 220 }
221 221
222 222 pub fn components(&self) -> impl Iterator<Item = &HgPath> {
223 223 self.inner.split(|&byte| byte == b'/').map(HgPath::new)
224 224 }
225 225
226 226 /// Returns the first (that is "root-most") slash-separated component of
227 227 /// the path, and the rest after the first slash if there is one.
228 228 pub fn split_first_component(&self) -> (&HgPath, Option<&HgPath>) {
229 229 match self.inner.split_2(b'/') {
230 230 Some((a, b)) => (HgPath::new(a), Some(HgPath::new(b))),
231 231 None => (self, None),
232 232 }
233 233 }
234 234
235 235 pub fn parent(&self) -> &Self {
236 236 let inner = self.as_bytes();
237 237 HgPath::new(match inner.iter().rposition(|b| *b == b'/') {
238 238 Some(pos) => &inner[..pos],
239 239 None => &[],
240 240 })
241 241 }
242 242 /// Given a base directory, returns the slice of `self` relative to the
243 243 /// base directory. If `base` is not a directory (does not end with a
244 244 /// `b'/'`), returns `None`.
245 245 pub fn relative_to(&self, base: impl AsRef<Self>) -> Option<&Self> {
246 246 let base = base.as_ref();
247 247 if base.is_empty() {
248 248 return Some(self);
249 249 }
250 250 let is_dir = base.as_bytes().ends_with(b"/");
251 251 if is_dir && self.starts_with(base) {
252 252 Some(Self::new(&self.inner[base.len()..]))
253 253 } else {
254 254 None
255 255 }
256 256 }
257 257
258 258 #[cfg(windows)]
259 259 /// Copied from the Python stdlib's `os.path.splitdrive` implementation.
260 260 ///
261 261 /// Split a pathname into drive/UNC sharepoint and relative path
262 262 /// specifiers. Returns a 2-tuple (drive_or_unc, path); either part may
263 263 /// be empty.
264 264 ///
265 265 /// If you assign
266 266 /// result = split_drive(p)
267 267 /// It is always true that:
268 268 /// result[0] + result[1] == p
269 269 ///
270 270 /// If the path contained a drive letter, drive_or_unc will contain
271 271 /// everything up to and including the colon.
272 272 /// e.g. split_drive("c:/dir") returns ("c:", "/dir")
273 273 ///
274 274 /// If the path contained a UNC path, the drive_or_unc will contain the
275 275 /// host name and share up to but not including the fourth directory
276 276 /// separator character.
277 277 /// e.g. split_drive("//host/computer/dir") returns ("//host/computer",
278 278 /// "/dir")
279 279 ///
280 280 /// Paths cannot contain both a drive letter and a UNC path.
281 281 pub fn split_drive<'a>(&self) -> (&HgPath, &HgPath) {
282 282 let bytes = self.as_bytes();
283 283 let is_sep = |b| std::path::is_separator(b as char);
284 284
285 285 if self.len() < 2 {
286 286 (HgPath::new(b""), &self)
287 287 } else if is_sep(bytes[0])
288 288 && is_sep(bytes[1])
289 289 && (self.len() == 2 || !is_sep(bytes[2]))
290 290 {
291 291 // Is a UNC path:
292 292 // vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
293 293 // \\machine\mountpoint\directory\etc\...
294 294 // directory ^^^^^^^^^^^^^^^
295 295
296 296 let machine_end_index = bytes[2..].iter().position(|b| is_sep(*b));
297 297 let mountpoint_start_index = if let Some(i) = machine_end_index {
298 298 i + 2
299 299 } else {
300 300 return (HgPath::new(b""), &self);
301 301 };
302 302
303 303 match bytes[mountpoint_start_index + 1..]
304 304 .iter()
305 305 .position(|b| is_sep(*b))
306 306 {
307 307 // A UNC path can't have two slashes in a row
308 308 // (after the initial two)
309 309 Some(0) => (HgPath::new(b""), &self),
310 310 Some(i) => {
311 311 let (a, b) =
312 312 bytes.split_at(mountpoint_start_index + 1 + i);
313 313 (HgPath::new(a), HgPath::new(b))
314 314 }
315 315 None => (&self, HgPath::new(b"")),
316 316 }
317 317 } else if bytes[1] == b':' {
318 318 // Drive path c:\directory
319 319 let (a, b) = bytes.split_at(2);
320 320 (HgPath::new(a), HgPath::new(b))
321 321 } else {
322 322 (HgPath::new(b""), &self)
323 323 }
324 324 }
325 325
326 326 #[cfg(unix)]
327 327 /// Split a pathname into drive and path. On Posix, drive is always empty.
328 328 pub fn split_drive(&self) -> (&HgPath, &HgPath) {
329 329 (HgPath::new(b""), self)
330 330 }
331 331
332 332 /// Checks for errors in the path, short-circuiting at the first one.
333 333 /// This generates fine-grained errors useful for debugging.
334 334 /// To simply check if the path is valid during tests, use `is_valid`.
335 335 pub fn check_state(&self) -> Result<(), HgPathError> {
336 336 if self.is_empty() {
337 337 return Ok(());
338 338 }
339 339 let bytes = self.as_bytes();
340 340 let mut previous_byte = None;
341 341
342 342 if bytes[0] == b'/' {
343 343 return Err(HgPathError::LeadingSlash(bytes.to_vec()));
344 344 }
345 345 for (index, byte) in bytes.iter().enumerate() {
346 346 match byte {
347 347 0 => {
348 348 return Err(HgPathError::ContainsNullByte {
349 349 bytes: bytes.to_vec(),
350 350 null_byte_index: index,
351 351 })
352 352 }
353 353 b'/' => {
354 354 if previous_byte.is_some() && previous_byte == Some(b'/') {
355 355 return Err(HgPathError::ConsecutiveSlashes {
356 356 bytes: bytes.to_vec(),
357 357 second_slash_index: index,
358 358 });
359 359 }
360 360 }
361 361 _ => (),
362 362 };
363 363 previous_byte = Some(*byte);
364 364 }
365 365 Ok(())
366 366 }
367 367
368 368 #[cfg(test)]
369 369 /// Only usable during tests to force developers to handle invalid states
370 370 fn is_valid(&self) -> bool {
371 371 self.check_state().is_ok()
372 372 }
373 373 }
374 374
375 375 impl fmt::Debug for HgPath {
376 376 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
377 377 write!(f, "HgPath({:?})", String::from_utf8_lossy(&self.inner))
378 378 }
379 379 }
380 380
381 381 impl fmt::Display for HgPath {
382 382 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
383 383 write!(f, "{}", String::from_utf8_lossy(&self.inner))
384 384 }
385 385 }
386 386
387 387 #[derive(
388 388 Default, Eq, Ord, Clone, PartialEq, PartialOrd, Hash, derive_more::From,
389 389 )]
390 390 pub struct HgPathBuf {
391 391 inner: Vec<u8>,
392 392 }
393 393
394 394 impl HgPathBuf {
395 395 pub fn new() -> Self {
396 396 Default::default()
397 397 }
398 398
399 399 pub fn push<T: ?Sized + AsRef<HgPath>>(&mut self, other: &T) {
400 400 if !self.inner.is_empty() && self.inner.last() != Some(&b'/') {
401 401 self.inner.push(b'/');
402 402 }
403 403 self.inner.extend(other.as_ref().bytes())
404 404 }
405 405
406 406 pub fn push_byte(&mut self, byte: u8) {
407 407 self.inner.push(byte);
408 408 }
409 409 pub fn from_bytes(s: &[u8]) -> HgPathBuf {
410 410 HgPath::new(s).to_owned()
411 411 }
412 412 pub fn into_vec(self) -> Vec<u8> {
413 413 self.inner
414 414 }
415 415 }
416 416
417 417 impl fmt::Debug for HgPathBuf {
418 418 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
419 419 write!(f, "HgPathBuf({:?})", String::from_utf8_lossy(&self.inner))
420 420 }
421 421 }
422 422
423 423 impl fmt::Display for HgPathBuf {
424 424 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
425 425 write!(f, "{}", String::from_utf8_lossy(&self.inner))
426 426 }
427 427 }
428 428
429 429 impl Deref for HgPathBuf {
430 430 type Target = HgPath;
431 431
432 432 #[inline]
433 433 fn deref(&self) -> &HgPath {
434 434 HgPath::new(&self.inner)
435 435 }
436 436 }
437 437
438 438 impl<T: ?Sized + AsRef<HgPath>> From<&T> for HgPathBuf {
439 439 fn from(s: &T) -> HgPathBuf {
440 440 s.as_ref().to_owned()
441 441 }
442 442 }
443 443
444 444 impl From<HgPathBuf> for Vec<u8> {
445 445 fn from(val: HgPathBuf) -> Self {
446 446 val.inner
447 447 }
448 448 }
449 449
450 450 impl Borrow<HgPath> for HgPathBuf {
451 451 fn borrow(&self) -> &HgPath {
452 452 HgPath::new(self.as_bytes())
453 453 }
454 454 }
455 455
456 456 impl ToOwned for HgPath {
457 457 type Owned = HgPathBuf;
458 458
459 459 fn to_owned(&self) -> HgPathBuf {
460 460 self.to_hg_path_buf()
461 461 }
462 462 }
463 463
464 464 impl AsRef<HgPath> for HgPath {
465 465 fn as_ref(&self) -> &HgPath {
466 466 self
467 467 }
468 468 }
469 469
470 470 impl AsRef<HgPath> for HgPathBuf {
471 471 fn as_ref(&self) -> &HgPath {
472 472 self
473 473 }
474 474 }
475 475
476 476 impl Extend<u8> for HgPathBuf {
477 477 fn extend<T: IntoIterator<Item = u8>>(&mut self, iter: T) {
478 478 self.inner.extend(iter);
479 479 }
480 480 }
481 481
482 /// Create a new [`OsString`] from types referenceable as [`HgPath`].
483 ///
482 484 /// TODO: Once <https://www.mercurial-scm.org/wiki/WindowsUTF8Plan> is
483 485 /// implemented, these conversion utils will have to work differently depending
484 486 /// on the repository encoding: either `UTF-8` or `MBCS`.
485
486 487 pub fn hg_path_to_os_string<P: AsRef<HgPath>>(
487 488 hg_path: P,
488 489 ) -> Result<OsString, HgPathError> {
489 490 hg_path.as_ref().check_state()?;
490 491 let os_str;
491 492 #[cfg(unix)]
492 493 {
493 494 use std::os::unix::ffi::OsStrExt;
494 495 os_str = std::ffi::OsStr::from_bytes(hg_path.as_ref().as_bytes());
495 496 }
496 497 // TODO Handle other platforms
497 498 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
498 499 Ok(os_str.to_os_string())
499 500 }
500 501
502 /// Create a new [`PathBuf`] from types referenceable as [`HgPath`].
501 503 pub fn hg_path_to_path_buf<P: AsRef<HgPath>>(
502 504 hg_path: P,
503 505 ) -> Result<PathBuf, HgPathError> {
504 506 Ok(Path::new(&hg_path_to_os_string(hg_path)?).to_path_buf())
505 507 }
506 508
509 /// Create a new [`HgPathBuf`] from types referenceable as [`OsStr`].
507 510 pub fn os_string_to_hg_path_buf<S: AsRef<OsStr>>(
508 511 os_string: S,
509 512 ) -> Result<HgPathBuf, HgPathError> {
510 513 let buf;
511 514 #[cfg(unix)]
512 515 {
513 516 use std::os::unix::ffi::OsStrExt;
514 517 buf = HgPathBuf::from_bytes(os_string.as_ref().as_bytes());
515 518 }
516 519 // TODO Handle other platforms
517 520 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
518 521
519 522 buf.check_state()?;
520 523 Ok(buf)
521 524 }
522 525
526 /// Create a new [`HgPathBuf`] from types referenceable as [`Path`].
523 527 pub fn path_to_hg_path_buf<P: AsRef<Path>>(
524 528 path: P,
525 529 ) -> Result<HgPathBuf, HgPathError> {
526 530 let buf;
527 531 let os_str = path.as_ref().as_os_str();
528 532 #[cfg(unix)]
529 533 {
530 534 use std::os::unix::ffi::OsStrExt;
531 535 buf = HgPathBuf::from_bytes(os_str.as_bytes());
532 536 }
533 537 // TODO Handle other platforms
534 538 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
535 539
536 540 buf.check_state()?;
537 541 Ok(buf)
538 542 }
539 543
540 544 impl TryFrom<PathBuf> for HgPathBuf {
541 545 type Error = HgPathError;
542 546 fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
543 547 path_to_hg_path_buf(path)
544 548 }
545 549 }
546 550
547 551 impl From<HgPathBuf> for Cow<'_, HgPath> {
548 552 fn from(path: HgPathBuf) -> Self {
549 553 Cow::Owned(path)
550 554 }
551 555 }
552 556
553 557 impl<'a> From<&'a HgPath> for Cow<'a, HgPath> {
554 558 fn from(path: &'a HgPath) -> Self {
555 559 Cow::Borrowed(path)
556 560 }
557 561 }
558 562
559 563 impl<'a> From<&'a HgPathBuf> for Cow<'a, HgPath> {
560 564 fn from(path: &'a HgPathBuf) -> Self {
561 565 Cow::Borrowed(&**path)
562 566 }
563 567 }
564 568
565 569 #[cfg(test)]
566 570 mod tests {
567 571 use super::*;
568 572 use pretty_assertions::assert_eq;
569 573
570 574 #[test]
571 575 fn test_path_states() {
572 576 assert_eq!(
573 577 Err(HgPathError::LeadingSlash(b"/".to_vec())),
574 578 HgPath::new(b"/").check_state()
575 579 );
576 580 assert_eq!(
577 581 Err(HgPathError::ConsecutiveSlashes {
578 582 bytes: b"a/b//c".to_vec(),
579 583 second_slash_index: 4
580 584 }),
581 585 HgPath::new(b"a/b//c").check_state()
582 586 );
583 587 assert_eq!(
584 588 Err(HgPathError::ContainsNullByte {
585 589 bytes: b"a/b/\0c".to_vec(),
586 590 null_byte_index: 4
587 591 }),
588 592 HgPath::new(b"a/b/\0c").check_state()
589 593 );
590 594 // TODO test HgPathError::DecodeError for the Windows implementation.
591 595 assert_eq!(true, HgPath::new(b"").is_valid());
592 596 assert_eq!(true, HgPath::new(b"a/b/c").is_valid());
593 597 // Backslashes in paths are not significant, but allowed
594 598 assert_eq!(true, HgPath::new(br"a\b/c").is_valid());
595 599 // Dots in paths are not significant, but allowed
596 600 assert_eq!(true, HgPath::new(b"a/b/../c/").is_valid());
597 601 assert_eq!(true, HgPath::new(b"./a/b/../c/").is_valid());
598 602 }
599 603
600 604 #[test]
601 605 fn test_iter() {
602 606 let path = HgPath::new(b"a");
603 607 let mut iter = path.bytes();
604 608 assert_eq!(Some(&b'a'), iter.next());
605 609 assert_eq!(None, iter.next_back());
606 610 assert_eq!(None, iter.next());
607 611
608 612 let path = HgPath::new(b"a");
609 613 let mut iter = path.bytes();
610 614 assert_eq!(Some(&b'a'), iter.next_back());
611 615 assert_eq!(None, iter.next_back());
612 616 assert_eq!(None, iter.next());
613 617
614 618 let path = HgPath::new(b"abc");
615 619 let mut iter = path.bytes();
616 620 assert_eq!(Some(&b'a'), iter.next());
617 621 assert_eq!(Some(&b'c'), iter.next_back());
618 622 assert_eq!(Some(&b'b'), iter.next_back());
619 623 assert_eq!(None, iter.next_back());
620 624 assert_eq!(None, iter.next());
621 625
622 626 let path = HgPath::new(b"abc");
623 627 let mut iter = path.bytes();
624 628 assert_eq!(Some(&b'a'), iter.next());
625 629 assert_eq!(Some(&b'b'), iter.next());
626 630 assert_eq!(Some(&b'c'), iter.next());
627 631 assert_eq!(None, iter.next_back());
628 632 assert_eq!(None, iter.next());
629 633
630 634 let path = HgPath::new(b"abc");
631 635 let iter = path.bytes();
632 636 let mut vec = Vec::new();
633 637 vec.extend(iter);
634 638 assert_eq!(vec![b'a', b'b', b'c'], vec);
635 639
636 640 let path = HgPath::new(b"abc");
637 641 let mut iter = path.bytes();
638 642 assert_eq!(Some(2), iter.rposition(|c| *c == b'c'));
639 643
640 644 let path = HgPath::new(b"abc");
641 645 let mut iter = path.bytes();
642 646 assert_eq!(None, iter.rposition(|c| *c == b'd'));
643 647 }
644 648
645 649 #[test]
646 650 fn test_join() {
647 651 let path = HgPathBuf::from_bytes(b"a").join(HgPath::new(b"b"));
648 652 assert_eq!(b"a/b", path.as_bytes());
649 653
650 654 let path = HgPathBuf::from_bytes(b"a/").join(HgPath::new(b"b/c"));
651 655 assert_eq!(b"a/b/c", path.as_bytes());
652 656
653 657 // No leading slash if empty before join
654 658 let path = HgPathBuf::new().join(HgPath::new(b"b/c"));
655 659 assert_eq!(b"b/c", path.as_bytes());
656 660
657 661 // The leading slash is an invalid representation of an `HgPath`, but
658 662 // it can happen. This creates another invalid representation of
659 663 // consecutive bytes.
660 664 // TODO What should be done in this case? Should we silently remove
661 665 // the extra slash? Should we change the signature to a problematic
662 666 // `Result<HgPathBuf, HgPathError>`, or should we just keep it so and
663 667 // let the error happen upon filesystem interaction?
664 668 let path = HgPathBuf::from_bytes(b"a/").join(HgPath::new(b"/b"));
665 669 assert_eq!(b"a//b", path.as_bytes());
666 670 let path = HgPathBuf::from_bytes(b"a").join(HgPath::new(b"/b"));
667 671 assert_eq!(b"a//b", path.as_bytes());
668 672 }
669 673
670 674 #[test]
671 675 fn test_relative_to() {
672 676 let path = HgPath::new(b"");
673 677 let base = HgPath::new(b"");
674 678 assert_eq!(Some(path), path.relative_to(base));
675 679
676 680 let path = HgPath::new(b"path");
677 681 let base = HgPath::new(b"");
678 682 assert_eq!(Some(path), path.relative_to(base));
679 683
680 684 let path = HgPath::new(b"a");
681 685 let base = HgPath::new(b"b");
682 686 assert_eq!(None, path.relative_to(base));
683 687
684 688 let path = HgPath::new(b"a/b");
685 689 let base = HgPath::new(b"a");
686 690 assert_eq!(None, path.relative_to(base));
687 691
688 692 let path = HgPath::new(b"a/b");
689 693 let base = HgPath::new(b"a/");
690 694 assert_eq!(Some(HgPath::new(b"b")), path.relative_to(base));
691 695
692 696 let path = HgPath::new(b"nested/path/to/b");
693 697 let base = HgPath::new(b"nested/path/");
694 698 assert_eq!(Some(HgPath::new(b"to/b")), path.relative_to(base));
695 699
696 700 let path = HgPath::new(b"ends/with/dir/");
697 701 let base = HgPath::new(b"ends/");
698 702 assert_eq!(Some(HgPath::new(b"with/dir/")), path.relative_to(base));
699 703 }
700 704
701 705 #[test]
702 706 #[cfg(unix)]
703 707 fn test_split_drive() {
704 708 // Taken from the Python stdlib's tests
705 709 assert_eq!(
706 710 HgPath::new(br"/foo/bar").split_drive(),
707 711 (HgPath::new(b""), HgPath::new(br"/foo/bar"))
708 712 );
709 713 assert_eq!(
710 714 HgPath::new(br"foo:bar").split_drive(),
711 715 (HgPath::new(b""), HgPath::new(br"foo:bar"))
712 716 );
713 717 assert_eq!(
714 718 HgPath::new(br":foo:bar").split_drive(),
715 719 (HgPath::new(b""), HgPath::new(br":foo:bar"))
716 720 );
717 721 // Also try NT paths; should not split them
718 722 assert_eq!(
719 723 HgPath::new(br"c:\foo\bar").split_drive(),
720 724 (HgPath::new(b""), HgPath::new(br"c:\foo\bar"))
721 725 );
722 726 assert_eq!(
723 727 HgPath::new(b"c:/foo/bar").split_drive(),
724 728 (HgPath::new(b""), HgPath::new(br"c:/foo/bar"))
725 729 );
726 730 assert_eq!(
727 731 HgPath::new(br"\\conky\mountpoint\foo\bar").split_drive(),
728 732 (
729 733 HgPath::new(b""),
730 734 HgPath::new(br"\\conky\mountpoint\foo\bar")
731 735 )
732 736 );
733 737 }
734 738
735 739 #[test]
736 740 #[cfg(windows)]
737 741 fn test_split_drive() {
738 742 assert_eq!(
739 743 HgPath::new(br"c:\foo\bar").split_drive(),
740 744 (HgPath::new(br"c:"), HgPath::new(br"\foo\bar"))
741 745 );
742 746 assert_eq!(
743 747 HgPath::new(b"c:/foo/bar").split_drive(),
744 748 (HgPath::new(br"c:"), HgPath::new(br"/foo/bar"))
745 749 );
746 750 assert_eq!(
747 751 HgPath::new(br"\\conky\mountpoint\foo\bar").split_drive(),
748 752 (
749 753 HgPath::new(br"\\conky\mountpoint"),
750 754 HgPath::new(br"\foo\bar")
751 755 )
752 756 );
753 757 assert_eq!(
754 758 HgPath::new(br"//conky/mountpoint/foo/bar").split_drive(),
755 759 (
756 760 HgPath::new(br"//conky/mountpoint"),
757 761 HgPath::new(br"/foo/bar")
758 762 )
759 763 );
760 764 assert_eq!(
761 765 HgPath::new(br"\\\conky\mountpoint\foo\bar").split_drive(),
762 766 (
763 767 HgPath::new(br""),
764 768 HgPath::new(br"\\\conky\mountpoint\foo\bar")
765 769 )
766 770 );
767 771 assert_eq!(
768 772 HgPath::new(br"///conky/mountpoint/foo/bar").split_drive(),
769 773 (
770 774 HgPath::new(br""),
771 775 HgPath::new(br"///conky/mountpoint/foo/bar")
772 776 )
773 777 );
774 778 assert_eq!(
775 779 HgPath::new(br"\\conky\\mountpoint\foo\bar").split_drive(),
776 780 (
777 781 HgPath::new(br""),
778 782 HgPath::new(br"\\conky\\mountpoint\foo\bar")
779 783 )
780 784 );
781 785 assert_eq!(
782 786 HgPath::new(br"//conky//mountpoint/foo/bar").split_drive(),
783 787 (
784 788 HgPath::new(br""),
785 789 HgPath::new(br"//conky//mountpoint/foo/bar")
786 790 )
787 791 );
788 792 // UNC part containing U+0130
789 793 assert_eq!(
790 794 HgPath::new(b"//conky/MOUNTPO\xc4\xb0NT/foo/bar").split_drive(),
791 795 (
792 796 HgPath::new(b"//conky/MOUNTPO\xc4\xb0NT"),
793 797 HgPath::new(br"/foo/bar")
794 798 )
795 799 );
796 800 }
797 801
798 802 #[test]
799 803 fn test_parent() {
800 804 let path = HgPath::new(b"");
801 805 assert_eq!(path.parent(), path);
802 806
803 807 let path = HgPath::new(b"a");
804 808 assert_eq!(path.parent(), HgPath::new(b""));
805 809
806 810 let path = HgPath::new(b"a/b");
807 811 assert_eq!(path.parent(), HgPath::new(b"a"));
808 812
809 813 let path = HgPath::new(b"a/other/b");
810 814 assert_eq!(path.parent(), HgPath::new(b"a/other"));
811 815 }
812 816 }
General Comments 0
You need to be logged in to leave comments. Login now