D6773: rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths

Alphare (Raphaël Gomès) phabricator at mercurial-scm.org
Fri Aug 30 05:27:15 EDT 2019


Alphare updated this revision to Diff 16338.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D6773?vs=16335&id=16338

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D6773/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D6773

AFFECTED FILES
  rust/hg-core/src/utils.rs
  rust/hg-core/src/utils/hg_path.rs

CHANGE DETAILS

diff --git a/rust/hg-core/src/utils/hg_path.rs b/rust/hg-core/src/utils/hg_path.rs
new file mode 100644
--- /dev/null
+++ b/rust/hg-core/src/utils/hg_path.rs
@@ -0,0 +1,349 @@
+use std::borrow::Borrow;
+use std::convert::TryInto;
+use std::ffi::OsString;
+use std::iter::FusedIterator;
+use std::ops::{Deref, Index, Range, RangeFrom, RangeFull, RangeTo};
+use std::path::{Path, PathBuf};
+
+/// This is a repository-relative path (or canonical path):
+///     - posix-like even on Windows,
+///     - no leading slash,
+///     - no "." nor ".." of special meaning,
+///     - stored in repository and shared across platforms.
+///
+/// This allows us to be encoding-transparent as much as possible, until really
+/// needed; `HgPath` can be transformed into a platform-specific path (`OsStr`
+/// or `Path`) whenever more complex operations are needed:
+/// On Unix, it's just byte-to-byte conversion. On Windows, it has to be
+/// decoded from MBCS to WTF-8. If WindowsUTF8Plan is implemented, the source
+/// character encoding will be determined per repository basis.
+#[derive(Eq, Ord, PartialEq, PartialOrd, Debug, Hash)]
+pub struct HgPath {
+    inner: [u8],
+}
+
+impl HgPath {
+    pub fn new<S: AsRef<[u8]> + ?Sized>(s: &S) -> &Self {
+        unsafe { &*(s.as_ref() as *const [u8] as *const Self) }
+    }
+    pub fn is_empty(&self) -> bool {
+        self.inner.len() == 0
+    }
+    pub fn len(&self) -> usize {
+        self.inner.len()
+    }
+    fn to_hg_path_buf(&self) -> HgPathBuf {
+        HgPathBuf {
+            inner: self.inner.to_owned(),
+        }
+    }
+    fn from_inner(inner: &[u8]) -> &Self {
+        unsafe { &*(inner as *const [u8] as *const HgPath) }
+    }
+    pub fn iter(&self) -> HgPathIterator {
+        HgPathIterator { path: &self }
+    }
+    pub fn to_ascii_uppercase(&self) -> HgPathBuf {
+        HgPathBuf::from(self.inner.to_ascii_uppercase())
+    }
+    pub fn to_ascii_lowercase(&self) -> HgPathBuf {
+        HgPathBuf::from(self.inner.to_ascii_lowercase())
+    }
+    pub fn bytes(&self) -> &[u8] {
+        unsafe { &*(&self.inner as *const _ as *const [u8]) }
+    }
+}
+
+impl Index<usize> for HgPath {
+    type Output = u8;
+
+    fn index(&self, i: usize) -> &Self::Output {
+        &self.inner[i]
+    }
+}
+
+impl Index<RangeFull> for HgPath {
+    type Output = HgPath;
+
+    #[inline]
+    fn index(&self, _index: RangeFull) -> &HgPath {
+        &self
+    }
+}
+
+impl Index<RangeTo<usize>> for HgPath {
+    type Output = HgPath;
+
+    #[inline]
+    fn index(&self, range_to: RangeTo<usize>) -> &HgPath {
+        HgPath::new(&self.inner[range_to])
+    }
+}
+
+impl Index<RangeFrom<usize>> for HgPath {
+    type Output = HgPath;
+
+    #[inline]
+    fn index(&self, range_from: RangeFrom<usize>) -> &HgPath {
+        HgPath::new(&self.inner[range_from])
+    }
+}
+impl Index<Range<usize>> for HgPath {
+    type Output = HgPath;
+
+    #[inline]
+    fn index(&self, range: Range<usize>) -> &HgPath {
+        HgPath::new(&self.inner[range])
+    }
+}
+
+impl Index<usize> for HgPathBuf {
+    type Output = u8;
+
+    fn index(&self, i: usize) -> &Self::Output {
+        &self.inner[i]
+    }
+}
+
+impl Index<RangeFull> for HgPathBuf {
+    type Output = HgPath;
+
+    #[inline]
+    fn index(&self, _index: RangeFull) -> &HgPath {
+        HgPath::from_inner(self.inner.as_slice())
+    }
+}
+
+impl Index<RangeTo<usize>> for HgPathBuf {
+    type Output = HgPath;
+
+    #[inline]
+    fn index(&self, range_to: RangeTo<usize>) -> &HgPath {
+        &HgPath::new(&self.inner[range_to])
+    }
+}
+
+impl Index<RangeFrom<usize>> for HgPathBuf {
+    type Output = HgPath;
+
+    #[inline]
+    fn index(&self, range_from: RangeFrom<usize>) -> &HgPath {
+        HgPath::new(&self.inner[range_from])
+    }
+}
+
+#[derive(Debug)]
+pub struct HgPathIterator<'a> {
+    path: &'a HgPath,
+}
+
+impl<'a> Iterator for HgPathIterator<'a> {
+    type Item = u8;
+    fn next(&mut self) -> Option<Self::Item> {
+        if self.path.len() == 0 {
+            None
+        } else {
+            let ret = self.path[0];
+            self.path = &self.path[1..self.path.len()];
+            Some(ret)
+        }
+    }
+}
+
+impl<'a> ExactSizeIterator for HgPathIterator<'a> {
+    // We can easily calculate the remaining number of iterations.
+    fn len(&self) -> usize {
+        self.path.len()
+    }
+}
+
+impl<'a> DoubleEndedIterator for HgPathIterator<'a> {
+    fn next_back(&mut self) -> Option<Self::Item> {
+        if self.path.len() == 0 {
+            None
+        } else {
+            let back_position = self.path.len() - 1;
+            let ret = self.path[back_position];
+            self.path = &self.path[..back_position];
+            Some(ret)
+        }
+    }
+}
+
+impl<'a> FusedIterator for HgPathIterator<'a> {}
+
+#[derive(Eq, Ord, Clone, PartialEq, PartialOrd, Debug, Hash)]
+pub struct HgPathBuf {
+    inner: Vec<u8>,
+}
+
+impl HgPathBuf {
+    pub fn new() -> Self {
+        Self { inner: Vec::new() }
+    }
+    pub fn push(&mut self, byte: u8) {
+        self.inner.push(byte)
+    }
+    pub fn from_bytes(s: &[u8]) -> HgPathBuf {
+        HgPath::new(s).to_owned()
+    }
+    pub fn into_vec(self) -> Vec<u8> {
+        self.inner
+    }
+    pub fn as_vec(&self) -> &Vec<u8> {
+        &self.inner
+    }
+    pub fn as_ref(&self) -> &[u8] {
+        self.inner.as_ref()
+    }
+}
+
+impl Deref for HgPathBuf {
+    type Target = HgPath;
+
+    #[inline]
+    fn deref(&self) -> &HgPath {
+        &self[..]
+    }
+}
+
+impl From<Vec<u8>> for HgPathBuf {
+    fn from(vec: Vec<u8>) -> Self {
+        Self { inner: vec }
+    }
+}
+
+impl<T: ?Sized + AsRef<HgPath>> From<&T> for HgPathBuf {
+    fn from(s: &T) -> HgPathBuf {
+        s.as_ref().to_owned()
+    }
+}
+
+impl Into<Vec<u8>> for HgPathBuf {
+    fn into(self) -> Vec<u8> {
+        self.inner
+    }
+}
+
+impl Borrow<HgPath> for HgPathBuf {
+    fn borrow(&self) -> &HgPath {
+        &self[..]
+    }
+}
+
+impl ToOwned for HgPath {
+    type Owned = HgPathBuf;
+    fn to_owned(&self) -> HgPathBuf {
+        self.to_hg_path_buf()
+    }
+}
+
+impl AsRef<HgPath> for HgPath {
+    fn as_ref(&self) -> &HgPath {
+        self
+    }
+}
+
+impl AsRef<HgPath> for HgPathBuf {
+    fn as_ref(&self) -> &HgPath {
+        self
+    }
+}
+
+impl Extend<u8> for HgPathBuf {
+    fn extend<T: IntoIterator<Item = u8>>(&mut self, iter: T) {
+        self.inner.extend(iter)
+    }
+}
+
+impl TryInto<PathBuf> for HgPathBuf {
+    type Error = std::io::Error;
+
+    fn try_into(self) -> Result<PathBuf, Self::Error> {
+        let os_str;
+        #[cfg(unix)]
+        {
+            use std::os::unix::ffi::OsStrExt;
+            os_str = std::ffi::OsStr::from_bytes(&self.inner);
+        }
+        #[cfg(windows)]
+        {
+            // TODO: convert from Windows MBCS (ANSI encoding) to WTF8.
+            // Perhaps, the return type would have to be Result<PathBuf>.
+            unimplemented!();
+        }
+
+        Ok(Path::new(os_str).to_path_buf())
+    }
+}
+
+impl TryInto<OsString> for HgPathBuf {
+    type Error = std::io::Error;
+
+    fn try_into(self) -> Result<OsString, Self::Error> {
+        let os_str;
+        #[cfg(unix)]
+        {
+            use std::os::unix::ffi::OsStrExt;
+            os_str = std::ffi::OsStr::from_bytes(&self.inner);
+        }
+        #[cfg(windows)]
+        {
+            // TODO: convert from Windows MBCS (ANSI encoding) to WTF8.
+            // Perhaps, the return type would have to be Result<PathBuf>.
+            unimplemented!()
+        }
+
+        Ok(os_str.to_os_string())
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_iter() {
+        let path = HgPath::new(b"a");
+        let mut iter = path.iter();
+        assert_eq!(Some(b'a'), iter.next());
+        assert_eq!(None, iter.next_back());
+        assert_eq!(None, iter.next());
+
+        let path = HgPath::new(b"a");
+        let mut iter = path.iter();
+        assert_eq!(Some(b'a'), iter.next_back());
+        assert_eq!(None, iter.next_back());
+        assert_eq!(None, iter.next());
+
+        let path = HgPath::new(b"abc");
+        let mut iter = path.iter();
+        assert_eq!(Some(b'a'), iter.next());
+        assert_eq!(Some(b'c'), iter.next_back());
+        assert_eq!(Some(b'b'), iter.next_back());
+        assert_eq!(None, iter.next_back());
+        assert_eq!(None, iter.next());
+
+        let path = HgPath::new(b"abc");
+        let mut iter = path.iter();
+        assert_eq!(Some(b'a'), iter.next());
+        assert_eq!(Some(b'b'), iter.next());
+        assert_eq!(Some(b'c'), iter.next());
+        assert_eq!(None, iter.next_back());
+        assert_eq!(None, iter.next());
+
+        let path = HgPath::new(b"abc");
+        let iter = path.iter();
+        let mut vec = Vec::new();
+        vec.extend(iter);
+        assert_eq!(vec![b'a', b'b', b'c'], vec);
+
+        let path = HgPath::new(b"abc");
+        let mut iter = path.iter();
+        assert_eq!(Some(2), iter.rposition(|c| c == b'c'));
+
+        let path = HgPath::new(b"abc");
+        let mut iter = path.iter();
+        assert_eq!(None, iter.rposition(|c| c == b'd'));
+    }
+}
diff --git a/rust/hg-core/src/utils.rs b/rust/hg-core/src/utils.rs
--- a/rust/hg-core/src/utils.rs
+++ b/rust/hg-core/src/utils.rs
@@ -8,6 +8,7 @@
 //! Contains useful functions, traits, structs, etc. for use in core.
 
 pub mod files;
+pub mod hg_path;
 
 /// Replaces the `from` slice with the `to` slice inside the `buf` slice.
 ///



To: Alphare, #hg-reviewers
Cc: durin42, kevincox, mercurial-devel


More information about the Mercurial-devel mailing list