Mercurial > public > mercurial-scm > hg
annotate rust/hg-core/src/utils/hg_path.rs @ 50818:28c0fcff24e5 stable
rhg: fix the bug where sparse config is interpreted as relglob instead of glob
relglob apparently (in contrast with relpath) matches everywhere in the tree,
whereas glob only matches at the root.
The python version interprets these patterns as "glob" (see
"normalize(include, b'glob', ...)" in match.py)
author | Arseniy Alekseyev <aalekseyev@janestreet.com> |
---|---|
date | Thu, 10 Aug 2023 19:00:19 +0100 |
parents | d27b6fc7c1bf |
children | de317a87ea6a |
rev | line source |
---|---|
42956
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 |
47192
1249eb9cc332
dirstate-tree: Refactor DirstateMap::drop_file to be recursive
Simon Sapin <simon.sapin@octobus.net>
parents:
47113
diff
changeset
|
8 use crate::utils::SliceExt; |
42956
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; |
47113
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
10 use std::borrow::Cow; |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
11 use std::ffi::{OsStr, OsString}; |
43827
c27e688fcdc3
rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43250
diff
changeset
|
12 use std::fmt; |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
13 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
|
14 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
|
15 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
16 #[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
|
17 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
|
18 /// 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
|
19 LeadingSlash(Vec<u8>), |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
20 ConsecutiveSlashes { |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
21 bytes: Vec<u8>, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
22 second_slash_index: usize, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
23 }, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
24 ContainsNullByte { |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
25 bytes: Vec<u8>, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
26 null_byte_index: usize, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
27 }, |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
28 /// Bytes |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
29 DecodeError(Vec<u8>), |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
30 /// The rest come from audit errors |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
31 EndsWithSlash(HgPathBuf), |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
32 ContainsIllegalComponent(HgPathBuf), |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
33 /// Path is inside the `.hg` folder |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
34 InsideDotHg(HgPathBuf), |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
35 IsInsideNestedRepo { |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
36 path: HgPathBuf, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
37 nested_repo: HgPathBuf, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
38 }, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
39 TraversesSymbolicLink { |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
40 path: HgPathBuf, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
41 symlink: HgPathBuf, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
42 }, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
43 NotFsCompliant(HgPathBuf), |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
44 /// `path` is the smallest invalid path |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
45 NotUnderRoot { |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
46 path: PathBuf, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
47 root: PathBuf, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
48 }, |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
49 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
50 |
46444
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
51 impl fmt::Display for HgPathError { |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
52 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
53 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
|
54 HgPathError::LeadingSlash(bytes) => { |
46444
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
55 write!(f, "Invalid HgPath '{:?}': has a leading slash.", bytes) |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
56 } |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
57 HgPathError::ConsecutiveSlashes { |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
58 bytes, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
59 second_slash_index: pos, |
46444
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
60 } => write!( |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
61 f, |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
62 "Invalid HgPath '{:?}': consecutive slashes at pos {}.", |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
63 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
|
64 ), |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
65 HgPathError::ContainsNullByte { |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
66 bytes, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
67 null_byte_index: pos, |
46444
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
68 } => write!( |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
69 f, |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
70 "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
|
71 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
|
72 ), |
46444
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
73 HgPathError::DecodeError(bytes) => write!( |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
74 f, |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
75 "Invalid HgPath '{:?}': could not be decoded.", |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
76 bytes |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
77 ), |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
78 HgPathError::EndsWithSlash(path) => { |
46444
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
79 write!(f, "Audit failed for '{}': ends with a slash.", path) |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
80 } |
46444
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
81 HgPathError::ContainsIllegalComponent(path) => write!( |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
82 f, |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
83 "Audit failed for '{}': contains an illegal component.", |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
84 path |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
85 ), |
46444
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
86 HgPathError::InsideDotHg(path) => write!( |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
87 f, |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
88 "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:
44191
diff
changeset
|
89 path |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
90 ), |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
91 HgPathError::IsInsideNestedRepo { |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
92 path, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
93 nested_repo: nested, |
46444
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
94 } => { |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
95 write!(f, |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
96 "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:
44191
diff
changeset
|
97 path, nested |
46444
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
98 ) |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
99 } |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
100 HgPathError::TraversesSymbolicLink { path, symlink } => write!( |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
101 f, |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
102 "Audit failed for '{}': traverses symbolic link '{}'.", |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
103 path, symlink |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
104 ), |
46444
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
105 HgPathError::NotFsCompliant(path) => write!( |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
106 f, |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
107 "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:
44191
diff
changeset
|
108 filesystem path.", |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
109 path |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
110 ), |
46444
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
111 HgPathError::NotUnderRoot { path, root } => write!( |
6c778d20c8c2
rust: replace ToString impls with Display
Simon Sapin <simon.sapin@octobus.net>
parents:
46435
diff
changeset
|
112 f, |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
113 "Audit failed for '{}': not under root {}.", |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
114 path.display(), |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
115 root.display() |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
116 ), |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
117 } |
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 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
|
122 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
|
123 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
|
124 } |
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 /// 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
|
128 /// - 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
|
129 /// - `/` 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
|
130 /// - 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
|
131 /// - 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
|
132 /// - 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
|
133 /// - 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
|
134 /// |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
135 /// 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
|
136 /// 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
|
137 /// 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
|
138 /// operation. |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
139 /// |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
140 /// 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
|
141 /// 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
|
142 /// 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
|
143 /// 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
|
144 /// 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
|
145 /// character encoding will be determined on a per-repository basis. |
43908
4b3c8df189bc
rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents:
43833
diff
changeset
|
146 #[derive(Eq, Ord, PartialEq, PartialOrd, Hash)] |
49004
9dcfd1d05e6e
rust-hgpath: add `repr(transparent)` to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48311
diff
changeset
|
147 #[repr(transparent)] |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
148 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
|
149 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
|
150 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
151 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
152 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
|
153 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
|
154 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
|
155 } |
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 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
|
157 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
|
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 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
|
160 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
|
161 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
162 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
|
163 HgPathBuf { |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
164 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
|
165 } |
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 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
|
168 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
|
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 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
|
171 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
|
172 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
173 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
|
174 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
|
175 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
176 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
|
177 &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
|
178 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
179 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
|
180 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
|
181 } |
44266
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
182 pub fn starts_with(&self, needle: impl AsRef<Self>) -> bool { |
43833
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
183 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:
43827
diff
changeset
|
184 } |
44266
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
185 pub fn trim_trailing_slash(&self) -> &Self { |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
186 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:
44265
diff
changeset
|
187 &self.inner[..self.inner.len() - 1] |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
188 } else { |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
189 &self.inner[..] |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
190 }) |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
191 } |
44267
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
192 /// 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:
44266
diff
changeset
|
193 /// at the rightmost `/`, if any. |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
194 /// |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
195 /// # Examples: |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
196 /// |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
197 /// ``` |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
198 /// use hg::utils::hg_path::HgPath; |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
199 /// |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
200 /// let path = HgPath::new(b"cool/hg/path").split_filename(); |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
201 /// 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:
44266
diff
changeset
|
202 /// |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
203 /// let path = HgPath::new(b"pathwithoutsep").split_filename(); |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
204 /// assert_eq!(path, (HgPath::new(b""), HgPath::new(b"pathwithoutsep"))); |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
205 /// ``` |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
206 pub fn split_filename(&self) -> (&Self, &Self) { |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
207 match &self.inner.iter().rposition(|c| *c == b'/') { |
49930
e98fd81bb151
rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49631
diff
changeset
|
208 None => (HgPath::new(""), self), |
44267
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
209 Some(size) => ( |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
210 HgPath::new(&self.inner[..*size]), |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
211 HgPath::new(&self.inner[*size + 1..]), |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
212 ), |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
213 } |
0e9ac3968b56
rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44266
diff
changeset
|
214 } |
48311
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
215 |
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
216 pub fn join(&self, path: &HgPath) -> HgPathBuf { |
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
217 let mut buf = self.to_owned(); |
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
218 buf.push(path); |
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
219 buf |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
220 } |
47099
3da19db33cbc
dirstate-tree: Add map `get` and `contains_key` methods
Simon Sapin <simon.sapin@octobus.net>
parents:
46444
diff
changeset
|
221 |
3da19db33cbc
dirstate-tree: Add map `get` and `contains_key` methods
Simon Sapin <simon.sapin@octobus.net>
parents:
46444
diff
changeset
|
222 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:
46444
diff
changeset
|
223 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:
46444
diff
changeset
|
224 } |
3da19db33cbc
dirstate-tree: Add map `get` and `contains_key` methods
Simon Sapin <simon.sapin@octobus.net>
parents:
46444
diff
changeset
|
225 |
47192
1249eb9cc332
dirstate-tree: Refactor DirstateMap::drop_file to be recursive
Simon Sapin <simon.sapin@octobus.net>
parents:
47113
diff
changeset
|
226 /// 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:
47113
diff
changeset
|
227 /// 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:
47113
diff
changeset
|
228 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:
47113
diff
changeset
|
229 match self.inner.split_2(b'/') { |
1249eb9cc332
dirstate-tree: Refactor DirstateMap::drop_file to be recursive
Simon Sapin <simon.sapin@octobus.net>
parents:
47113
diff
changeset
|
230 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:
47113
diff
changeset
|
231 None => (self, None), |
1249eb9cc332
dirstate-tree: Refactor DirstateMap::drop_file to be recursive
Simon Sapin <simon.sapin@octobus.net>
parents:
47113
diff
changeset
|
232 } |
1249eb9cc332
dirstate-tree: Refactor DirstateMap::drop_file to be recursive
Simon Sapin <simon.sapin@octobus.net>
parents:
47113
diff
changeset
|
233 } |
1249eb9cc332
dirstate-tree: Refactor DirstateMap::drop_file to be recursive
Simon Sapin <simon.sapin@octobus.net>
parents:
47113
diff
changeset
|
234 |
44266
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
235 pub fn parent(&self) -> &Self { |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
236 let inner = self.as_bytes(); |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
237 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:
44265
diff
changeset
|
238 Some(pos) => &inner[..pos], |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
239 None => &[], |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
240 }) |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
241 } |
43833
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
242 /// 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:
43827
diff
changeset
|
243 /// 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:
43827
diff
changeset
|
244 /// `b'/'`), returns `None`. |
44266
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
245 pub fn relative_to(&self, base: impl AsRef<Self>) -> Option<&Self> { |
43833
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
246 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:
43827
diff
changeset
|
247 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:
43827
diff
changeset
|
248 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:
43827
diff
changeset
|
249 } |
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
250 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:
43827
diff
changeset
|
251 if is_dir && self.starts_with(base) { |
44266
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
252 Some(Self::new(&self.inner[base.len()..])) |
43833
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
253 } 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:
43827
diff
changeset
|
254 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:
43827
diff
changeset
|
255 } |
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
256 } |
44136
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
257 |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
258 #[cfg(windows)] |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
259 /// 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:
43908
diff
changeset
|
260 /// |
44191
732098027b34
rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents:
44136
diff
changeset
|
261 /// Split a pathname into drive/UNC sharepoint and relative path |
732098027b34
rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents:
44136
diff
changeset
|
262 /// 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:
44136
diff
changeset
|
263 /// be empty. |
44136
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
264 /// |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
265 /// If you assign |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
266 /// result = split_drive(p) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
267 /// 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:
43908
diff
changeset
|
268 /// 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:
43908
diff
changeset
|
269 /// |
44191
732098027b34
rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents:
44136
diff
changeset
|
270 /// 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:
44136
diff
changeset
|
271 /// everything up to and including the colon. |
44136
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
272 /// 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:
43908
diff
changeset
|
273 /// |
44191
732098027b34
rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents:
44136
diff
changeset
|
274 /// 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:
44136
diff
changeset
|
275 /// 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:
44136
diff
changeset
|
276 /// separator character. |
732098027b34
rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents:
44136
diff
changeset
|
277 /// e.g. split_drive("//host/computer/dir") returns ("//host/computer", |
732098027b34
rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents:
44136
diff
changeset
|
278 /// "/dir") |
44136
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
279 /// |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
280 /// 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:
43908
diff
changeset
|
281 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:
43908
diff
changeset
|
282 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:
43908
diff
changeset
|
283 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:
43908
diff
changeset
|
284 |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
285 if self.len() < 2 { |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
286 (HgPath::new(b""), &self) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
287 } 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:
43908
diff
changeset
|
288 && is_sep(bytes[1]) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
289 && (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:
43908
diff
changeset
|
290 { |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
291 // Is a UNC path: |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
292 // 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:
43908
diff
changeset
|
293 // \\machine\mountpoint\directory\etc\... |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
294 // directory ^^^^^^^^^^^^^^^ |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
295 |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
296 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:
43908
diff
changeset
|
297 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:
43908
diff
changeset
|
298 i + 2 |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
299 } else { |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
300 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:
43908
diff
changeset
|
301 }; |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
302 |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
303 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:
43908
diff
changeset
|
304 .iter() |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
305 .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:
43908
diff
changeset
|
306 { |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
307 // 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:
43908
diff
changeset
|
308 // (after the initial two) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
309 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:
43908
diff
changeset
|
310 Some(i) => { |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
311 let (a, b) = |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
312 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:
43908
diff
changeset
|
313 (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:
43908
diff
changeset
|
314 } |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
315 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:
43908
diff
changeset
|
316 } |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
317 } 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:
43908
diff
changeset
|
318 // Drive path c:\directory |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
319 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:
43908
diff
changeset
|
320 (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:
43908
diff
changeset
|
321 } else { |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
322 (HgPath::new(b""), &self) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
323 } |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
324 } |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
325 |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
326 #[cfg(unix)] |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
327 /// 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:
43908
diff
changeset
|
328 pub fn split_drive(&self) -> (&HgPath, &HgPath) { |
49930
e98fd81bb151
rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49631
diff
changeset
|
329 (HgPath::new(b""), self) |
44136
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
330 } |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
331 |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
332 /// 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
|
333 /// 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
|
334 /// 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
|
335 pub fn check_state(&self) -> Result<(), HgPathError> { |
44973
26114bd6ec60
rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44267
diff
changeset
|
336 if self.is_empty() { |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
337 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
|
338 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
339 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
|
340 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
|
341 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
342 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
|
343 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
|
344 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
345 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
|
346 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
|
347 0 => { |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
348 return Err(HgPathError::ContainsNullByte { |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
349 bytes: bytes.to_vec(), |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
350 null_byte_index: index, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
351 }) |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
352 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
353 b'/' => { |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
354 if previous_byte.is_some() && previous_byte == Some(b'/') { |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
355 return Err(HgPathError::ConsecutiveSlashes { |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
356 bytes: bytes.to_vec(), |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
357 second_slash_index: index, |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
358 }); |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
359 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
360 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
361 _ => (), |
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 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
|
364 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
365 Ok(()) |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
366 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
367 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
368 #[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
|
369 /// 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
|
370 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
|
371 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
|
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 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
374 |
43908
4b3c8df189bc
rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents:
43833
diff
changeset
|
375 impl fmt::Debug for HgPath { |
4b3c8df189bc
rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents:
43833
diff
changeset
|
376 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:
43833
diff
changeset
|
377 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:
43833
diff
changeset
|
378 } |
4b3c8df189bc
rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents:
43833
diff
changeset
|
379 } |
4b3c8df189bc
rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents:
43833
diff
changeset
|
380 |
43827
c27e688fcdc3
rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43250
diff
changeset
|
381 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
|
382 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
|
383 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
|
384 } |
c27e688fcdc3
rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43250
diff
changeset
|
385 } |
c27e688fcdc3
rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43250
diff
changeset
|
386 |
46435
2e2033081274
rust: replace trivial `impl From ?` with `#[derive(derive_more::From)]`
Simon Sapin <simon.sapin@octobus.net>
parents:
45538
diff
changeset
|
387 #[derive( |
2e2033081274
rust: replace trivial `impl From ?` with `#[derive(derive_more::From)]`
Simon Sapin <simon.sapin@octobus.net>
parents:
45538
diff
changeset
|
388 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:
45538
diff
changeset
|
389 )] |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
390 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
|
391 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
|
392 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
393 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
394 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
|
395 pub fn new() -> Self { |
44973
26114bd6ec60
rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44267
diff
changeset
|
396 Default::default() |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
397 } |
48311
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
398 |
49930
e98fd81bb151
rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49631
diff
changeset
|
399 pub fn push<T: ?Sized + AsRef<HgPath>>(&mut self, other: &T) { |
48311
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
400 if !self.inner.is_empty() && self.inner.last() != Some(&b'/') { |
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
401 self.inner.push(b'/'); |
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
402 } |
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
403 self.inner.extend(other.as_ref().bytes()) |
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
404 } |
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
405 |
6d69e83e6b6e
rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
47192
diff
changeset
|
406 pub fn push_byte(&mut self, byte: u8) { |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
407 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
|
408 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
409 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
|
410 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
|
411 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
412 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
|
413 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
|
414 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
415 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
416 |
43908
4b3c8df189bc
rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents:
43833
diff
changeset
|
417 impl fmt::Debug for HgPathBuf { |
4b3c8df189bc
rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents:
43833
diff
changeset
|
418 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:
43833
diff
changeset
|
419 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:
43833
diff
changeset
|
420 } |
4b3c8df189bc
rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents:
43833
diff
changeset
|
421 } |
4b3c8df189bc
rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents:
43833
diff
changeset
|
422 |
43827
c27e688fcdc3
rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43250
diff
changeset
|
423 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
|
424 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
|
425 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
|
426 } |
c27e688fcdc3
rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43250
diff
changeset
|
427 } |
c27e688fcdc3
rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43250
diff
changeset
|
428 |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
429 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
|
430 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
|
431 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
432 #[inline] |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
433 fn deref(&self) -> &HgPath { |
49930
e98fd81bb151
rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49631
diff
changeset
|
434 HgPath::new(&self.inner) |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
435 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
436 } |
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 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
|
439 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
|
440 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
|
441 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
442 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
443 |
49930
e98fd81bb151
rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49631
diff
changeset
|
444 impl From<HgPathBuf> for Vec<u8> { |
e98fd81bb151
rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49631
diff
changeset
|
445 fn from(val: HgPathBuf) -> Self { |
e98fd81bb151
rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49631
diff
changeset
|
446 val.inner |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
447 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
448 } |
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 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
|
451 fn borrow(&self) -> &HgPath { |
49930
e98fd81bb151
rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49631
diff
changeset
|
452 HgPath::new(self.as_bytes()) |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
453 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
454 } |
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 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
|
457 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
|
458 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
459 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
|
460 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
|
461 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
462 } |
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 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
|
465 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
|
466 self |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
467 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
468 } |
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 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
|
471 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
|
472 self |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
473 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
474 } |
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 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
|
477 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
|
478 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
|
479 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
480 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
481 |
50414
d27b6fc7c1bf
rustdoc: summary line for hg_path_to_os_string
Georges Racinet <georges.racinet@octobus.net>
parents:
50412
diff
changeset
|
482 /// Create a new [`OsString`] from types referenceable as [`HgPath`]. |
d27b6fc7c1bf
rustdoc: summary line for hg_path_to_os_string
Georges Racinet <georges.racinet@octobus.net>
parents:
50412
diff
changeset
|
483 /// |
50412
331a3cbe1c9e
rustdoc: fixed warnings about links
Georges Racinet <georges.racinet@octobus.net>
parents:
49930
diff
changeset
|
484 /// TODO: Once <https://www.mercurial-scm.org/wiki/WindowsUTF8Plan> is |
42956
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 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
|
488 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
|
489 ) -> 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
|
490 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
|
491 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
|
492 #[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
|
493 { |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
494 use std::os::unix::ffi::OsStrExt; |
49930
e98fd81bb151
rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49631
diff
changeset
|
495 os_str = std::ffi::OsStr::from_bytes(hg_path.as_ref().as_bytes()); |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
496 } |
43250
98d996a138de
rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42956
diff
changeset
|
497 // TODO Handle other platforms |
98d996a138de
rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42956
diff
changeset
|
498 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding). |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
499 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
|
500 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
501 |
50414
d27b6fc7c1bf
rustdoc: summary line for hg_path_to_os_string
Georges Racinet <georges.racinet@octobus.net>
parents:
50412
diff
changeset
|
502 /// Create a new [`PathBuf`] from types referenceable as [`HgPath`]. |
42956
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 |
50414
d27b6fc7c1bf
rustdoc: summary line for hg_path_to_os_string
Georges Racinet <georges.racinet@octobus.net>
parents:
50412
diff
changeset
|
509 /// Create a new [`HgPathBuf`] from types referenceable as [`OsStr`]. |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
510 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
|
511 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
|
512 ) -> 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
|
513 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
|
514 #[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
|
515 { |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
516 use std::os::unix::ffi::OsStrExt; |
49930
e98fd81bb151
rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49631
diff
changeset
|
517 buf = HgPathBuf::from_bytes(os_string.as_ref().as_bytes()); |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
518 } |
43250
98d996a138de
rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42956
diff
changeset
|
519 // TODO Handle other platforms |
98d996a138de
rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42956
diff
changeset
|
520 // 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:
42956
diff
changeset
|
521 |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
522 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
|
523 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
|
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 |
50414
d27b6fc7c1bf
rustdoc: summary line for hg_path_to_os_string
Georges Racinet <georges.racinet@octobus.net>
parents:
50412
diff
changeset
|
526 /// Create a new [`HgPathBuf`] from types referenceable as [`Path`]. |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
527 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
|
528 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
|
529 ) -> 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
|
530 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
|
531 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
|
532 #[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
|
533 { |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
534 use std::os::unix::ffi::OsStrExt; |
49930
e98fd81bb151
rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49631
diff
changeset
|
535 buf = HgPathBuf::from_bytes(os_str.as_bytes()); |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
536 } |
43250
98d996a138de
rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42956
diff
changeset
|
537 // TODO Handle other platforms |
98d996a138de
rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42956
diff
changeset
|
538 // 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:
42956
diff
changeset
|
539 |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
540 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
|
541 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
|
542 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
543 |
45538
2d5dfc8fed55
hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
544 impl TryFrom<PathBuf> for HgPathBuf { |
2d5dfc8fed55
hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
545 type Error = HgPathError; |
2d5dfc8fed55
hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
546 fn try_from(path: PathBuf) -> Result<Self, Self::Error> { |
2d5dfc8fed55
hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
547 path_to_hg_path_buf(path) |
2d5dfc8fed55
hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
548 } |
2d5dfc8fed55
hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
549 } |
2d5dfc8fed55
hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
550 |
47113
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
551 impl From<HgPathBuf> for Cow<'_, HgPath> { |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
552 fn from(path: HgPathBuf) -> Self { |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
553 Cow::Owned(path) |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
554 } |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
555 } |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
556 |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
557 impl<'a> From<&'a HgPath> for Cow<'a, HgPath> { |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
558 fn from(path: &'a HgPath) -> Self { |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
559 Cow::Borrowed(path) |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
560 } |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
561 } |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
562 |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
563 impl<'a> From<&'a HgPathBuf> for Cow<'a, HgPath> { |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
564 fn from(path: &'a HgPathBuf) -> Self { |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
565 Cow::Borrowed(&**path) |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
566 } |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
567 } |
be579775c2d9
dirstate-tree: Add the new `status()` algorithm
Simon Sapin <simon.sapin@octobus.net>
parents:
47099
diff
changeset
|
568 |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
569 #[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
|
570 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
|
571 use super::*; |
44266
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
572 use pretty_assertions::assert_eq; |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
573 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
574 #[test] |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
575 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
|
576 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
|
577 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
|
578 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
|
579 ); |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
580 assert_eq!( |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
581 Err(HgPathError::ConsecutiveSlashes { |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
582 bytes: b"a/b//c".to_vec(), |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
583 second_slash_index: 4 |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
584 }), |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
585 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
|
586 ); |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
587 assert_eq!( |
44265
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
588 Err(HgPathError::ContainsNullByte { |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
589 bytes: b"a/b/\0c".to_vec(), |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
590 null_byte_index: 4 |
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44191
diff
changeset
|
591 }), |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
592 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
|
593 ); |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
594 // 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
|
595 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
|
596 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
|
597 // 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
|
598 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
|
599 // 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
|
600 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
|
601 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
|
602 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
603 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
604 #[test] |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
605 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
|
606 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
|
607 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
|
608 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
|
609 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
|
610 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
|
611 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
612 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
|
613 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
|
614 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
|
615 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
|
616 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
|
617 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
618 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
|
619 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
|
620 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
|
621 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
|
622 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
|
623 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
|
624 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
|
625 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
626 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
|
627 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
|
628 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
|
629 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
|
630 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
|
631 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
|
632 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
|
633 |
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 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
|
635 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
|
636 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
|
637 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
|
638 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
|
639 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
640 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
|
641 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
|
642 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
|
643 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
644 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
|
645 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
|
646 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
|
647 } |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
648 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
649 #[test] |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
650 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
|
651 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
|
652 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
|
653 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
654 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
|
655 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
|
656 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
657 // 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
|
658 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
|
659 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
|
660 |
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
661 // 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
|
662 // 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
|
663 // 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
|
664 // 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
|
665 // 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
|
666 // `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
|
667 // 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
|
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 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
|
671 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
|
672 } |
43833
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
673 |
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
674 #[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:
43827
diff
changeset
|
675 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:
43827
diff
changeset
|
676 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:
43827
diff
changeset
|
677 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:
43827
diff
changeset
|
678 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:
43827
diff
changeset
|
679 |
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
680 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:
43827
diff
changeset
|
681 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:
43827
diff
changeset
|
682 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:
43827
diff
changeset
|
683 |
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
684 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:
43827
diff
changeset
|
685 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:
43827
diff
changeset
|
686 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:
43827
diff
changeset
|
687 |
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
688 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:
43827
diff
changeset
|
689 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:
43827
diff
changeset
|
690 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:
43827
diff
changeset
|
691 |
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
692 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:
43827
diff
changeset
|
693 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:
43827
diff
changeset
|
694 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:
43827
diff
changeset
|
695 |
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
696 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:
43827
diff
changeset
|
697 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:
43827
diff
changeset
|
698 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:
43827
diff
changeset
|
699 |
4f1543a2f5c3
rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43827
diff
changeset
|
700 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:
43827
diff
changeset
|
701 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:
43827
diff
changeset
|
702 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:
43827
diff
changeset
|
703 } |
44136
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
704 |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
705 #[test] |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
706 #[cfg(unix)] |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
707 fn test_split_drive() { |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
708 // 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:
43908
diff
changeset
|
709 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
710 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:
43908
diff
changeset
|
711 (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:
43908
diff
changeset
|
712 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
713 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
714 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:
43908
diff
changeset
|
715 (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:
43908
diff
changeset
|
716 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
717 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
718 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:
43908
diff
changeset
|
719 (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:
43908
diff
changeset
|
720 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
721 // 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:
43908
diff
changeset
|
722 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
723 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:
43908
diff
changeset
|
724 (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:
43908
diff
changeset
|
725 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
726 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
727 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:
43908
diff
changeset
|
728 (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:
43908
diff
changeset
|
729 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
730 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
731 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:
43908
diff
changeset
|
732 ( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
733 HgPath::new(b""), |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
734 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:
43908
diff
changeset
|
735 ) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
736 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
737 } |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
738 |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
739 #[test] |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
740 #[cfg(windows)] |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
741 fn test_split_drive() { |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
742 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
743 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:
43908
diff
changeset
|
744 (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:
43908
diff
changeset
|
745 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
746 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
747 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:
43908
diff
changeset
|
748 (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:
43908
diff
changeset
|
749 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
750 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
751 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:
43908
diff
changeset
|
752 ( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
753 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:
43908
diff
changeset
|
754 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:
43908
diff
changeset
|
755 ) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
756 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
757 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
758 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:
43908
diff
changeset
|
759 ( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
760 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:
43908
diff
changeset
|
761 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:
43908
diff
changeset
|
762 ) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
763 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
764 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
765 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:
43908
diff
changeset
|
766 ( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
767 HgPath::new(br""), |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
768 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:
43908
diff
changeset
|
769 ) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
770 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
771 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
772 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:
43908
diff
changeset
|
773 ( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
774 HgPath::new(br""), |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
775 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:
43908
diff
changeset
|
776 ) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
777 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
778 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
779 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:
43908
diff
changeset
|
780 ( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
781 HgPath::new(br""), |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
782 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:
43908
diff
changeset
|
783 ) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
784 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
785 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
786 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:
43908
diff
changeset
|
787 ( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
788 HgPath::new(br""), |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
789 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:
43908
diff
changeset
|
790 ) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
791 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
792 // 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:
43908
diff
changeset
|
793 assert_eq!( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
794 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:
43908
diff
changeset
|
795 ( |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
796 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:
43908
diff
changeset
|
797 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:
43908
diff
changeset
|
798 ) |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
799 ); |
baa4e7fdfd47
rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43908
diff
changeset
|
800 } |
44266
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
801 |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
802 #[test] |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
803 fn test_parent() { |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
804 let path = HgPath::new(b""); |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
805 assert_eq!(path.parent(), path); |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
806 |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
807 let path = HgPath::new(b"a"); |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
808 assert_eq!(path.parent(), HgPath::new(b"")); |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
809 |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
810 let path = HgPath::new(b"a/b"); |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
811 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:
44265
diff
changeset
|
812 |
9ab4830e9e3d
rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44265
diff
changeset
|
813 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:
44265
diff
changeset
|
814 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:
44265
diff
changeset
|
815 } |
42956
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
816 } |