##// END OF EJS Templates
rust-cross-platform: remove `unimplemented!` to get compile-time errors...
Raphaël Gomès -
r43525:60ee5842 default draft
parent child Browse files
Show More
@@ -1,104 +1,101 b''
1 // files.rs
1 // files.rs
2 //
2 //
3 // Copyright 2019
3 // Copyright 2019
4 // Raphaël Gomès <rgomes@octobus.net>,
4 // Raphaël Gomès <rgomes@octobus.net>,
5 // Yuya Nishihara <yuya@tcha.org>
5 // Yuya Nishihara <yuya@tcha.org>
6 //
6 //
7 // This software may be used and distributed according to the terms of the
7 // This software may be used and distributed according to the terms of the
8 // GNU General Public License version 2 or any later version.
8 // GNU General Public License version 2 or any later version.
9
9
10 //! Functions for fiddling with files.
10 //! Functions for fiddling with files.
11
11
12 use crate::utils::hg_path::{HgPath, HgPathBuf};
12 use crate::utils::hg_path::{HgPath, HgPathBuf};
13 use std::iter::FusedIterator;
13 use std::iter::FusedIterator;
14
14
15 use std::path::Path;
15 use std::path::Path;
16
16
17 pub fn get_path_from_bytes(bytes: &[u8]) -> &Path {
17 pub fn get_path_from_bytes(bytes: &[u8]) -> &Path {
18 let os_str;
18 let os_str;
19 #[cfg(unix)]
19 #[cfg(unix)]
20 {
20 {
21 use std::os::unix::ffi::OsStrExt;
21 use std::os::unix::ffi::OsStrExt;
22 os_str = std::ffi::OsStr::from_bytes(bytes);
22 os_str = std::ffi::OsStr::from_bytes(bytes);
23 }
23 }
24 #[cfg(windows)]
24 // TODO Handle other platforms
25 {
25 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
26 // TODO: convert from Windows MBCS (ANSI encoding) to WTF8.
26 // Perhaps, the return type would have to be Result<PathBuf>.
27 // Perhaps, the return type would have to be Result<PathBuf>.
28 unimplemented!()
29 }
30
27
31 Path::new(os_str)
28 Path::new(os_str)
32 }
29 }
33
30
34 /// An iterator over repository path yielding itself and its ancestors.
31 /// An iterator over repository path yielding itself and its ancestors.
35 #[derive(Copy, Clone, Debug)]
32 #[derive(Copy, Clone, Debug)]
36 pub struct Ancestors<'a> {
33 pub struct Ancestors<'a> {
37 next: Option<&'a HgPath>,
34 next: Option<&'a HgPath>,
38 }
35 }
39
36
40 impl<'a> Iterator for Ancestors<'a> {
37 impl<'a> Iterator for Ancestors<'a> {
41 type Item = &'a HgPath;
38 type Item = &'a HgPath;
42
39
43 fn next(&mut self) -> Option<Self::Item> {
40 fn next(&mut self) -> Option<Self::Item> {
44 let next = self.next;
41 let next = self.next;
45 self.next = match self.next {
42 self.next = match self.next {
46 Some(s) if s.is_empty() => None,
43 Some(s) if s.is_empty() => None,
47 Some(s) => {
44 Some(s) => {
48 let p = s.bytes().rposition(|c| *c == b'/').unwrap_or(0);
45 let p = s.bytes().rposition(|c| *c == b'/').unwrap_or(0);
49 Some(HgPath::new(&s.as_bytes()[..p]))
46 Some(HgPath::new(&s.as_bytes()[..p]))
50 }
47 }
51 None => None,
48 None => None,
52 };
49 };
53 next
50 next
54 }
51 }
55 }
52 }
56
53
57 impl<'a> FusedIterator for Ancestors<'a> {}
54 impl<'a> FusedIterator for Ancestors<'a> {}
58
55
59 /// Returns an iterator yielding ancestor directories of the given repository
56 /// Returns an iterator yielding ancestor directories of the given repository
60 /// path.
57 /// path.
61 ///
58 ///
62 /// The path is separated by '/', and must not start with '/'.
59 /// The path is separated by '/', and must not start with '/'.
63 ///
60 ///
64 /// The path itself isn't included unless it is b"" (meaning the root
61 /// The path itself isn't included unless it is b"" (meaning the root
65 /// directory.)
62 /// directory.)
66 pub fn find_dirs<'a>(path: &'a HgPath) -> Ancestors<'a> {
63 pub fn find_dirs<'a>(path: &'a HgPath) -> Ancestors<'a> {
67 let mut dirs = Ancestors { next: Some(path) };
64 let mut dirs = Ancestors { next: Some(path) };
68 if !path.is_empty() {
65 if !path.is_empty() {
69 dirs.next(); // skip itself
66 dirs.next(); // skip itself
70 }
67 }
71 dirs
68 dirs
72 }
69 }
73
70
74 /// TODO more than ASCII?
71 /// TODO more than ASCII?
75 pub fn normalize_case(path: &HgPath) -> HgPathBuf {
72 pub fn normalize_case(path: &HgPath) -> HgPathBuf {
76 #[cfg(windows)] // NTFS compares via upper()
73 #[cfg(windows)] // NTFS compares via upper()
77 return path.to_ascii_uppercase();
74 return path.to_ascii_uppercase();
78 #[cfg(unix)]
75 #[cfg(unix)]
79 path.to_ascii_lowercase()
76 path.to_ascii_lowercase()
80 }
77 }
81
78
82 #[cfg(test)]
79 #[cfg(test)]
83 mod tests {
80 mod tests {
84 use super::*;
81 use super::*;
85
82
86 #[test]
83 #[test]
87 fn find_dirs_some() {
84 fn find_dirs_some() {
88 let mut dirs = super::find_dirs(HgPath::new(b"foo/bar/baz"));
85 let mut dirs = super::find_dirs(HgPath::new(b"foo/bar/baz"));
89 assert_eq!(dirs.next(), Some(HgPath::new(b"foo/bar")));
86 assert_eq!(dirs.next(), Some(HgPath::new(b"foo/bar")));
90 assert_eq!(dirs.next(), Some(HgPath::new(b"foo")));
87 assert_eq!(dirs.next(), Some(HgPath::new(b"foo")));
91 assert_eq!(dirs.next(), Some(HgPath::new(b"")));
88 assert_eq!(dirs.next(), Some(HgPath::new(b"")));
92 assert_eq!(dirs.next(), None);
89 assert_eq!(dirs.next(), None);
93 assert_eq!(dirs.next(), None);
90 assert_eq!(dirs.next(), None);
94 }
91 }
95
92
96 #[test]
93 #[test]
97 fn find_dirs_empty() {
94 fn find_dirs_empty() {
98 // looks weird, but mercurial.util.finddirs(b"") yields b""
95 // looks weird, but mercurial.util.finddirs(b"") yields b""
99 let mut dirs = super::find_dirs(HgPath::new(b""));
96 let mut dirs = super::find_dirs(HgPath::new(b""));
100 assert_eq!(dirs.next(), Some(HgPath::new(b"")));
97 assert_eq!(dirs.next(), Some(HgPath::new(b"")));
101 assert_eq!(dirs.next(), None);
98 assert_eq!(dirs.next(), None);
102 assert_eq!(dirs.next(), None);
99 assert_eq!(dirs.next(), None);
103 }
100 }
104 }
101 }
@@ -1,409 +1,402 b''
1 // hg_path.rs
1 // hg_path.rs
2 //
2 //
3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4 //
4 //
5 // This software may be used and distributed according to the terms of the
5 // This software may be used and distributed according to the terms of the
6 // GNU General Public License version 2 or any later version.
6 // GNU General Public License version 2 or any later version.
7
7
8 use std::borrow::Borrow;
8 use std::borrow::Borrow;
9 use std::ffi::{OsStr, OsString};
9 use std::ffi::{OsStr, OsString};
10 use std::ops::Deref;
10 use std::ops::Deref;
11 use std::path::{Path, PathBuf};
11 use std::path::{Path, PathBuf};
12
12
13 #[derive(Debug, Eq, PartialEq)]
13 #[derive(Debug, Eq, PartialEq)]
14 pub enum HgPathError {
14 pub enum HgPathError {
15 /// Bytes from the invalid `HgPath`
15 /// Bytes from the invalid `HgPath`
16 LeadingSlash(Vec<u8>),
16 LeadingSlash(Vec<u8>),
17 /// Bytes and index of the second slash
17 /// Bytes and index of the second slash
18 ConsecutiveSlashes(Vec<u8>, usize),
18 ConsecutiveSlashes(Vec<u8>, usize),
19 /// Bytes and index of the null byte
19 /// Bytes and index of the null byte
20 ContainsNullByte(Vec<u8>, usize),
20 ContainsNullByte(Vec<u8>, usize),
21 /// Bytes
21 /// Bytes
22 DecodeError(Vec<u8>),
22 DecodeError(Vec<u8>),
23 }
23 }
24
24
25 impl ToString for HgPathError {
25 impl ToString for HgPathError {
26 fn to_string(&self) -> String {
26 fn to_string(&self) -> String {
27 match self {
27 match self {
28 HgPathError::LeadingSlash(bytes) => {
28 HgPathError::LeadingSlash(bytes) => {
29 format!("Invalid HgPath '{:?}': has a leading slash.", bytes)
29 format!("Invalid HgPath '{:?}': has a leading slash.", bytes)
30 }
30 }
31 HgPathError::ConsecutiveSlashes(bytes, pos) => format!(
31 HgPathError::ConsecutiveSlashes(bytes, pos) => format!(
32 "Invalid HgPath '{:?}': consecutive slahes at pos {}.",
32 "Invalid HgPath '{:?}': consecutive slahes at pos {}.",
33 bytes, pos
33 bytes, pos
34 ),
34 ),
35 HgPathError::ContainsNullByte(bytes, pos) => format!(
35 HgPathError::ContainsNullByte(bytes, pos) => format!(
36 "Invalid HgPath '{:?}': contains null byte at pos {}.",
36 "Invalid HgPath '{:?}': contains null byte at pos {}.",
37 bytes, pos
37 bytes, pos
38 ),
38 ),
39 HgPathError::DecodeError(bytes) => {
39 HgPathError::DecodeError(bytes) => {
40 format!("Invalid HgPath '{:?}': could not be decoded.", bytes)
40 format!("Invalid HgPath '{:?}': could not be decoded.", bytes)
41 }
41 }
42 }
42 }
43 }
43 }
44 }
44 }
45
45
46 impl From<HgPathError> for std::io::Error {
46 impl From<HgPathError> for std::io::Error {
47 fn from(e: HgPathError) -> Self {
47 fn from(e: HgPathError) -> Self {
48 std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())
48 std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())
49 }
49 }
50 }
50 }
51
51
52 /// This is a repository-relative path (or canonical path):
52 /// This is a repository-relative path (or canonical path):
53 /// - no null characters
53 /// - no null characters
54 /// - `/` separates directories
54 /// - `/` separates directories
55 /// - no consecutive slashes
55 /// - no consecutive slashes
56 /// - no leading slash,
56 /// - no leading slash,
57 /// - no `.` nor `..` of special meaning
57 /// - no `.` nor `..` of special meaning
58 /// - stored in repository and shared across platforms
58 /// - stored in repository and shared across platforms
59 ///
59 ///
60 /// Note: there is no guarantee of any `HgPath` being well-formed at any point
60 /// Note: there is no guarantee of any `HgPath` being well-formed at any point
61 /// in its lifetime for performance reasons and to ease ergonomics. It is
61 /// in its lifetime for performance reasons and to ease ergonomics. It is
62 /// however checked using the `check_state` method before any file-system
62 /// however checked using the `check_state` method before any file-system
63 /// operation.
63 /// operation.
64 ///
64 ///
65 /// This allows us to be encoding-transparent as much as possible, until really
65 /// This allows us to be encoding-transparent as much as possible, until really
66 /// needed; `HgPath` can be transformed into a platform-specific path (`OsStr`
66 /// needed; `HgPath` can be transformed into a platform-specific path (`OsStr`
67 /// or `Path`) whenever more complex operations are needed:
67 /// or `Path`) whenever more complex operations are needed:
68 /// On Unix, it's just byte-to-byte conversion. On Windows, it has to be
68 /// On Unix, it's just byte-to-byte conversion. On Windows, it has to be
69 /// decoded from MBCS to WTF-8. If WindowsUTF8Plan is implemented, the source
69 /// decoded from MBCS to WTF-8. If WindowsUTF8Plan is implemented, the source
70 /// character encoding will be determined on a per-repository basis.
70 /// character encoding will be determined on a per-repository basis.
71 //
71 //
72 // FIXME: (adapted from a comment in the stdlib)
72 // FIXME: (adapted from a comment in the stdlib)
73 // `HgPath::new()` current implementation relies on `Slice` being
73 // `HgPath::new()` current implementation relies on `Slice` being
74 // layout-compatible with `[u8]`.
74 // layout-compatible with `[u8]`.
75 // When attribute privacy is implemented, `Slice` should be annotated as
75 // When attribute privacy is implemented, `Slice` should be annotated as
76 // `#[repr(transparent)]`.
76 // `#[repr(transparent)]`.
77 // Anyway, `Slice` representation and layout are considered implementation
77 // Anyway, `Slice` representation and layout are considered implementation
78 // detail, are not documented and must not be relied upon.
78 // detail, are not documented and must not be relied upon.
79 #[derive(Eq, Ord, PartialEq, PartialOrd, Debug, Hash)]
79 #[derive(Eq, Ord, PartialEq, PartialOrd, Debug, Hash)]
80 pub struct HgPath {
80 pub struct HgPath {
81 inner: [u8],
81 inner: [u8],
82 }
82 }
83
83
84 impl HgPath {
84 impl HgPath {
85 pub fn new<S: AsRef<[u8]> + ?Sized>(s: &S) -> &Self {
85 pub fn new<S: AsRef<[u8]> + ?Sized>(s: &S) -> &Self {
86 unsafe { &*(s.as_ref() as *const [u8] as *const Self) }
86 unsafe { &*(s.as_ref() as *const [u8] as *const Self) }
87 }
87 }
88 pub fn is_empty(&self) -> bool {
88 pub fn is_empty(&self) -> bool {
89 self.inner.is_empty()
89 self.inner.is_empty()
90 }
90 }
91 pub fn len(&self) -> usize {
91 pub fn len(&self) -> usize {
92 self.inner.len()
92 self.inner.len()
93 }
93 }
94 fn to_hg_path_buf(&self) -> HgPathBuf {
94 fn to_hg_path_buf(&self) -> HgPathBuf {
95 HgPathBuf {
95 HgPathBuf {
96 inner: self.inner.to_owned(),
96 inner: self.inner.to_owned(),
97 }
97 }
98 }
98 }
99 pub fn bytes(&self) -> std::slice::Iter<u8> {
99 pub fn bytes(&self) -> std::slice::Iter<u8> {
100 self.inner.iter()
100 self.inner.iter()
101 }
101 }
102 pub fn to_ascii_uppercase(&self) -> HgPathBuf {
102 pub fn to_ascii_uppercase(&self) -> HgPathBuf {
103 HgPathBuf::from(self.inner.to_ascii_uppercase())
103 HgPathBuf::from(self.inner.to_ascii_uppercase())
104 }
104 }
105 pub fn to_ascii_lowercase(&self) -> HgPathBuf {
105 pub fn to_ascii_lowercase(&self) -> HgPathBuf {
106 HgPathBuf::from(self.inner.to_ascii_lowercase())
106 HgPathBuf::from(self.inner.to_ascii_lowercase())
107 }
107 }
108 pub fn as_bytes(&self) -> &[u8] {
108 pub fn as_bytes(&self) -> &[u8] {
109 &self.inner
109 &self.inner
110 }
110 }
111 pub fn contains(&self, other: u8) -> bool {
111 pub fn contains(&self, other: u8) -> bool {
112 self.inner.contains(&other)
112 self.inner.contains(&other)
113 }
113 }
114 pub fn join<T: ?Sized + AsRef<HgPath>>(&self, other: &T) -> HgPathBuf {
114 pub fn join<T: ?Sized + AsRef<HgPath>>(&self, other: &T) -> HgPathBuf {
115 let mut inner = self.inner.to_owned();
115 let mut inner = self.inner.to_owned();
116 if inner.len() != 0 && inner.last() != Some(&b'/') {
116 if inner.len() != 0 && inner.last() != Some(&b'/') {
117 inner.push(b'/');
117 inner.push(b'/');
118 }
118 }
119 inner.extend(other.as_ref().bytes());
119 inner.extend(other.as_ref().bytes());
120 HgPathBuf::from_bytes(&inner)
120 HgPathBuf::from_bytes(&inner)
121 }
121 }
122 /// Checks for errors in the path, short-circuiting at the first one.
122 /// Checks for errors in the path, short-circuiting at the first one.
123 /// This generates fine-grained errors useful for debugging.
123 /// This generates fine-grained errors useful for debugging.
124 /// To simply check if the path is valid during tests, use `is_valid`.
124 /// To simply check if the path is valid during tests, use `is_valid`.
125 pub fn check_state(&self) -> Result<(), HgPathError> {
125 pub fn check_state(&self) -> Result<(), HgPathError> {
126 if self.len() == 0 {
126 if self.len() == 0 {
127 return Ok(());
127 return Ok(());
128 }
128 }
129 let bytes = self.as_bytes();
129 let bytes = self.as_bytes();
130 let mut previous_byte = None;
130 let mut previous_byte = None;
131
131
132 if bytes[0] == b'/' {
132 if bytes[0] == b'/' {
133 return Err(HgPathError::LeadingSlash(bytes.to_vec()));
133 return Err(HgPathError::LeadingSlash(bytes.to_vec()));
134 }
134 }
135 for (index, byte) in bytes.iter().enumerate() {
135 for (index, byte) in bytes.iter().enumerate() {
136 match byte {
136 match byte {
137 0 => {
137 0 => {
138 return Err(HgPathError::ContainsNullByte(
138 return Err(HgPathError::ContainsNullByte(
139 bytes.to_vec(),
139 bytes.to_vec(),
140 index,
140 index,
141 ))
141 ))
142 }
142 }
143 b'/' => {
143 b'/' => {
144 if previous_byte.is_some() && previous_byte == Some(b'/') {
144 if previous_byte.is_some() && previous_byte == Some(b'/') {
145 return Err(HgPathError::ConsecutiveSlashes(
145 return Err(HgPathError::ConsecutiveSlashes(
146 bytes.to_vec(),
146 bytes.to_vec(),
147 index,
147 index,
148 ));
148 ));
149 }
149 }
150 }
150 }
151 _ => (),
151 _ => (),
152 };
152 };
153 previous_byte = Some(*byte);
153 previous_byte = Some(*byte);
154 }
154 }
155 Ok(())
155 Ok(())
156 }
156 }
157
157
158 #[cfg(test)]
158 #[cfg(test)]
159 /// Only usable during tests to force developers to handle invalid states
159 /// Only usable during tests to force developers to handle invalid states
160 fn is_valid(&self) -> bool {
160 fn is_valid(&self) -> bool {
161 self.check_state().is_ok()
161 self.check_state().is_ok()
162 }
162 }
163 }
163 }
164
164
165 #[derive(Eq, Ord, Clone, PartialEq, PartialOrd, Debug, Hash)]
165 #[derive(Eq, Ord, Clone, PartialEq, PartialOrd, Debug, Hash)]
166 pub struct HgPathBuf {
166 pub struct HgPathBuf {
167 inner: Vec<u8>,
167 inner: Vec<u8>,
168 }
168 }
169
169
170 impl HgPathBuf {
170 impl HgPathBuf {
171 pub fn new() -> Self {
171 pub fn new() -> Self {
172 Self { inner: Vec::new() }
172 Self { inner: Vec::new() }
173 }
173 }
174 pub fn push(&mut self, byte: u8) {
174 pub fn push(&mut self, byte: u8) {
175 self.inner.push(byte);
175 self.inner.push(byte);
176 }
176 }
177 pub fn from_bytes(s: &[u8]) -> HgPathBuf {
177 pub fn from_bytes(s: &[u8]) -> HgPathBuf {
178 HgPath::new(s).to_owned()
178 HgPath::new(s).to_owned()
179 }
179 }
180 pub fn into_vec(self) -> Vec<u8> {
180 pub fn into_vec(self) -> Vec<u8> {
181 self.inner
181 self.inner
182 }
182 }
183 pub fn as_ref(&self) -> &[u8] {
183 pub fn as_ref(&self) -> &[u8] {
184 self.inner.as_ref()
184 self.inner.as_ref()
185 }
185 }
186 }
186 }
187
187
188 impl Deref for HgPathBuf {
188 impl Deref for HgPathBuf {
189 type Target = HgPath;
189 type Target = HgPath;
190
190
191 #[inline]
191 #[inline]
192 fn deref(&self) -> &HgPath {
192 fn deref(&self) -> &HgPath {
193 &HgPath::new(&self.inner)
193 &HgPath::new(&self.inner)
194 }
194 }
195 }
195 }
196
196
197 impl From<Vec<u8>> for HgPathBuf {
197 impl From<Vec<u8>> for HgPathBuf {
198 fn from(vec: Vec<u8>) -> Self {
198 fn from(vec: Vec<u8>) -> Self {
199 Self { inner: vec }
199 Self { inner: vec }
200 }
200 }
201 }
201 }
202
202
203 impl<T: ?Sized + AsRef<HgPath>> From<&T> for HgPathBuf {
203 impl<T: ?Sized + AsRef<HgPath>> From<&T> for HgPathBuf {
204 fn from(s: &T) -> HgPathBuf {
204 fn from(s: &T) -> HgPathBuf {
205 s.as_ref().to_owned()
205 s.as_ref().to_owned()
206 }
206 }
207 }
207 }
208
208
209 impl Into<Vec<u8>> for HgPathBuf {
209 impl Into<Vec<u8>> for HgPathBuf {
210 fn into(self) -> Vec<u8> {
210 fn into(self) -> Vec<u8> {
211 self.inner
211 self.inner
212 }
212 }
213 }
213 }
214
214
215 impl Borrow<HgPath> for HgPathBuf {
215 impl Borrow<HgPath> for HgPathBuf {
216 fn borrow(&self) -> &HgPath {
216 fn borrow(&self) -> &HgPath {
217 &HgPath::new(self.as_bytes())
217 &HgPath::new(self.as_bytes())
218 }
218 }
219 }
219 }
220
220
221 impl ToOwned for HgPath {
221 impl ToOwned for HgPath {
222 type Owned = HgPathBuf;
222 type Owned = HgPathBuf;
223
223
224 fn to_owned(&self) -> HgPathBuf {
224 fn to_owned(&self) -> HgPathBuf {
225 self.to_hg_path_buf()
225 self.to_hg_path_buf()
226 }
226 }
227 }
227 }
228
228
229 impl AsRef<HgPath> for HgPath {
229 impl AsRef<HgPath> for HgPath {
230 fn as_ref(&self) -> &HgPath {
230 fn as_ref(&self) -> &HgPath {
231 self
231 self
232 }
232 }
233 }
233 }
234
234
235 impl AsRef<HgPath> for HgPathBuf {
235 impl AsRef<HgPath> for HgPathBuf {
236 fn as_ref(&self) -> &HgPath {
236 fn as_ref(&self) -> &HgPath {
237 self
237 self
238 }
238 }
239 }
239 }
240
240
241 impl Extend<u8> for HgPathBuf {
241 impl Extend<u8> for HgPathBuf {
242 fn extend<T: IntoIterator<Item = u8>>(&mut self, iter: T) {
242 fn extend<T: IntoIterator<Item = u8>>(&mut self, iter: T) {
243 self.inner.extend(iter);
243 self.inner.extend(iter);
244 }
244 }
245 }
245 }
246
246
247 /// TODO: Once https://www.mercurial-scm.org/wiki/WindowsUTF8Plan is
247 /// TODO: Once https://www.mercurial-scm.org/wiki/WindowsUTF8Plan is
248 /// implemented, these conversion utils will have to work differently depending
248 /// implemented, these conversion utils will have to work differently depending
249 /// on the repository encoding: either `UTF-8` or `MBCS`.
249 /// on the repository encoding: either `UTF-8` or `MBCS`.
250
250
251 pub fn hg_path_to_os_string<P: AsRef<HgPath>>(
251 pub fn hg_path_to_os_string<P: AsRef<HgPath>>(
252 hg_path: P,
252 hg_path: P,
253 ) -> Result<OsString, HgPathError> {
253 ) -> Result<OsString, HgPathError> {
254 hg_path.as_ref().check_state()?;
254 hg_path.as_ref().check_state()?;
255 let os_str;
255 let os_str;
256 #[cfg(unix)]
256 #[cfg(unix)]
257 {
257 {
258 use std::os::unix::ffi::OsStrExt;
258 use std::os::unix::ffi::OsStrExt;
259 os_str = std::ffi::OsStr::from_bytes(&hg_path.as_ref().as_bytes());
259 os_str = std::ffi::OsStr::from_bytes(&hg_path.as_ref().as_bytes());
260 }
260 }
261 #[cfg(windows)]
261 // TODO Handle other platforms
262 {
262 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
263 // TODO: convert from Windows MBCS (ANSI encoding) to WTF8.
264 unimplemented!();
265 }
266 Ok(os_str.to_os_string())
263 Ok(os_str.to_os_string())
267 }
264 }
268
265
269 pub fn hg_path_to_path_buf<P: AsRef<HgPath>>(
266 pub fn hg_path_to_path_buf<P: AsRef<HgPath>>(
270 hg_path: P,
267 hg_path: P,
271 ) -> Result<PathBuf, HgPathError> {
268 ) -> Result<PathBuf, HgPathError> {
272 Ok(Path::new(&hg_path_to_os_string(hg_path)?).to_path_buf())
269 Ok(Path::new(&hg_path_to_os_string(hg_path)?).to_path_buf())
273 }
270 }
274
271
275 pub fn os_string_to_hg_path_buf<S: AsRef<OsStr>>(
272 pub fn os_string_to_hg_path_buf<S: AsRef<OsStr>>(
276 os_string: S,
273 os_string: S,
277 ) -> Result<HgPathBuf, HgPathError> {
274 ) -> Result<HgPathBuf, HgPathError> {
278 let buf;
275 let buf;
279 #[cfg(unix)]
276 #[cfg(unix)]
280 {
277 {
281 use std::os::unix::ffi::OsStrExt;
278 use std::os::unix::ffi::OsStrExt;
282 buf = HgPathBuf::from_bytes(&os_string.as_ref().as_bytes());
279 buf = HgPathBuf::from_bytes(&os_string.as_ref().as_bytes());
283 }
280 }
284 #[cfg(windows)]
281 // TODO Handle other platforms
285 {
282 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
286 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
283
287 unimplemented!();
288 }
289 buf.check_state()?;
284 buf.check_state()?;
290 Ok(buf)
285 Ok(buf)
291 }
286 }
292
287
293 pub fn path_to_hg_path_buf<P: AsRef<Path>>(
288 pub fn path_to_hg_path_buf<P: AsRef<Path>>(
294 path: P,
289 path: P,
295 ) -> Result<HgPathBuf, HgPathError> {
290 ) -> Result<HgPathBuf, HgPathError> {
296 let buf;
291 let buf;
297 let os_str = path.as_ref().as_os_str();
292 let os_str = path.as_ref().as_os_str();
298 #[cfg(unix)]
293 #[cfg(unix)]
299 {
294 {
300 use std::os::unix::ffi::OsStrExt;
295 use std::os::unix::ffi::OsStrExt;
301 buf = HgPathBuf::from_bytes(&os_str.as_bytes());
296 buf = HgPathBuf::from_bytes(&os_str.as_bytes());
302 }
297 }
303 #[cfg(windows)]
298 // TODO Handle other platforms
304 {
299 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
305 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
300
306 unimplemented!();
307 }
308 buf.check_state()?;
301 buf.check_state()?;
309 Ok(buf)
302 Ok(buf)
310 }
303 }
311
304
312 #[cfg(test)]
305 #[cfg(test)]
313 mod tests {
306 mod tests {
314 use super::*;
307 use super::*;
315
308
316 #[test]
309 #[test]
317 fn test_path_states() {
310 fn test_path_states() {
318 assert_eq!(
311 assert_eq!(
319 Err(HgPathError::LeadingSlash(b"/".to_vec())),
312 Err(HgPathError::LeadingSlash(b"/".to_vec())),
320 HgPath::new(b"/").check_state()
313 HgPath::new(b"/").check_state()
321 );
314 );
322 assert_eq!(
315 assert_eq!(
323 Err(HgPathError::ConsecutiveSlashes(b"a/b//c".to_vec(), 4)),
316 Err(HgPathError::ConsecutiveSlashes(b"a/b//c".to_vec(), 4)),
324 HgPath::new(b"a/b//c").check_state()
317 HgPath::new(b"a/b//c").check_state()
325 );
318 );
326 assert_eq!(
319 assert_eq!(
327 Err(HgPathError::ContainsNullByte(b"a/b/\0c".to_vec(), 4)),
320 Err(HgPathError::ContainsNullByte(b"a/b/\0c".to_vec(), 4)),
328 HgPath::new(b"a/b/\0c").check_state()
321 HgPath::new(b"a/b/\0c").check_state()
329 );
322 );
330 // TODO test HgPathError::DecodeError for the Windows implementation.
323 // TODO test HgPathError::DecodeError for the Windows implementation.
331 assert_eq!(true, HgPath::new(b"").is_valid());
324 assert_eq!(true, HgPath::new(b"").is_valid());
332 assert_eq!(true, HgPath::new(b"a/b/c").is_valid());
325 assert_eq!(true, HgPath::new(b"a/b/c").is_valid());
333 // Backslashes in paths are not significant, but allowed
326 // Backslashes in paths are not significant, but allowed
334 assert_eq!(true, HgPath::new(br"a\b/c").is_valid());
327 assert_eq!(true, HgPath::new(br"a\b/c").is_valid());
335 // Dots in paths are not significant, but allowed
328 // Dots in paths are not significant, but allowed
336 assert_eq!(true, HgPath::new(b"a/b/../c/").is_valid());
329 assert_eq!(true, HgPath::new(b"a/b/../c/").is_valid());
337 assert_eq!(true, HgPath::new(b"./a/b/../c/").is_valid());
330 assert_eq!(true, HgPath::new(b"./a/b/../c/").is_valid());
338 }
331 }
339
332
340 #[test]
333 #[test]
341 fn test_iter() {
334 fn test_iter() {
342 let path = HgPath::new(b"a");
335 let path = HgPath::new(b"a");
343 let mut iter = path.bytes();
336 let mut iter = path.bytes();
344 assert_eq!(Some(&b'a'), iter.next());
337 assert_eq!(Some(&b'a'), iter.next());
345 assert_eq!(None, iter.next_back());
338 assert_eq!(None, iter.next_back());
346 assert_eq!(None, iter.next());
339 assert_eq!(None, iter.next());
347
340
348 let path = HgPath::new(b"a");
341 let path = HgPath::new(b"a");
349 let mut iter = path.bytes();
342 let mut iter = path.bytes();
350 assert_eq!(Some(&b'a'), iter.next_back());
343 assert_eq!(Some(&b'a'), iter.next_back());
351 assert_eq!(None, iter.next_back());
344 assert_eq!(None, iter.next_back());
352 assert_eq!(None, iter.next());
345 assert_eq!(None, iter.next());
353
346
354 let path = HgPath::new(b"abc");
347 let path = HgPath::new(b"abc");
355 let mut iter = path.bytes();
348 let mut iter = path.bytes();
356 assert_eq!(Some(&b'a'), iter.next());
349 assert_eq!(Some(&b'a'), iter.next());
357 assert_eq!(Some(&b'c'), iter.next_back());
350 assert_eq!(Some(&b'c'), iter.next_back());
358 assert_eq!(Some(&b'b'), iter.next_back());
351 assert_eq!(Some(&b'b'), iter.next_back());
359 assert_eq!(None, iter.next_back());
352 assert_eq!(None, iter.next_back());
360 assert_eq!(None, iter.next());
353 assert_eq!(None, iter.next());
361
354
362 let path = HgPath::new(b"abc");
355 let path = HgPath::new(b"abc");
363 let mut iter = path.bytes();
356 let mut iter = path.bytes();
364 assert_eq!(Some(&b'a'), iter.next());
357 assert_eq!(Some(&b'a'), iter.next());
365 assert_eq!(Some(&b'b'), iter.next());
358 assert_eq!(Some(&b'b'), iter.next());
366 assert_eq!(Some(&b'c'), iter.next());
359 assert_eq!(Some(&b'c'), iter.next());
367 assert_eq!(None, iter.next_back());
360 assert_eq!(None, iter.next_back());
368 assert_eq!(None, iter.next());
361 assert_eq!(None, iter.next());
369
362
370 let path = HgPath::new(b"abc");
363 let path = HgPath::new(b"abc");
371 let iter = path.bytes();
364 let iter = path.bytes();
372 let mut vec = Vec::new();
365 let mut vec = Vec::new();
373 vec.extend(iter);
366 vec.extend(iter);
374 assert_eq!(vec![b'a', b'b', b'c'], vec);
367 assert_eq!(vec![b'a', b'b', b'c'], vec);
375
368
376 let path = HgPath::new(b"abc");
369 let path = HgPath::new(b"abc");
377 let mut iter = path.bytes();
370 let mut iter = path.bytes();
378 assert_eq!(Some(2), iter.rposition(|c| *c == b'c'));
371 assert_eq!(Some(2), iter.rposition(|c| *c == b'c'));
379
372
380 let path = HgPath::new(b"abc");
373 let path = HgPath::new(b"abc");
381 let mut iter = path.bytes();
374 let mut iter = path.bytes();
382 assert_eq!(None, iter.rposition(|c| *c == b'd'));
375 assert_eq!(None, iter.rposition(|c| *c == b'd'));
383 }
376 }
384
377
385 #[test]
378 #[test]
386 fn test_join() {
379 fn test_join() {
387 let path = HgPathBuf::from_bytes(b"a").join(HgPath::new(b"b"));
380 let path = HgPathBuf::from_bytes(b"a").join(HgPath::new(b"b"));
388 assert_eq!(b"a/b", path.as_bytes());
381 assert_eq!(b"a/b", path.as_bytes());
389
382
390 let path = HgPathBuf::from_bytes(b"a/").join(HgPath::new(b"b/c"));
383 let path = HgPathBuf::from_bytes(b"a/").join(HgPath::new(b"b/c"));
391 assert_eq!(b"a/b/c", path.as_bytes());
384 assert_eq!(b"a/b/c", path.as_bytes());
392
385
393 // No leading slash if empty before join
386 // No leading slash if empty before join
394 let path = HgPathBuf::new().join(HgPath::new(b"b/c"));
387 let path = HgPathBuf::new().join(HgPath::new(b"b/c"));
395 assert_eq!(b"b/c", path.as_bytes());
388 assert_eq!(b"b/c", path.as_bytes());
396
389
397 // The leading slash is an invalid representation of an `HgPath`, but
390 // The leading slash is an invalid representation of an `HgPath`, but
398 // it can happen. This creates another invalid representation of
391 // it can happen. This creates another invalid representation of
399 // consecutive bytes.
392 // consecutive bytes.
400 // TODO What should be done in this case? Should we silently remove
393 // TODO What should be done in this case? Should we silently remove
401 // the extra slash? Should we change the signature to a problematic
394 // the extra slash? Should we change the signature to a problematic
402 // `Result<HgPathBuf, HgPathError>`, or should we just keep it so and
395 // `Result<HgPathBuf, HgPathError>`, or should we just keep it so and
403 // let the error happen upon filesystem interaction?
396 // let the error happen upon filesystem interaction?
404 let path = HgPathBuf::from_bytes(b"a/").join(HgPath::new(b"/b"));
397 let path = HgPathBuf::from_bytes(b"a/").join(HgPath::new(b"/b"));
405 assert_eq!(b"a//b", path.as_bytes());
398 assert_eq!(b"a//b", path.as_bytes());
406 let path = HgPathBuf::from_bytes(b"a").join(HgPath::new(b"/b"));
399 let path = HgPathBuf::from_bytes(b"a").join(HgPath::new(b"/b"));
407 assert_eq!(b"a//b", path.as_bytes());
400 assert_eq!(b"a//b", path.as_bytes());
408 }
401 }
409 }
402 }
General Comments 0
You need to be logged in to leave comments. Login now