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