rust/hg-core/src/utils/hg_path.rs
author Raphaël Gomès <rgomes@octobus.net>
Tue, 14 Jan 2020 18:03:28 +0100
changeset 44136 baa4e7fdfd47
parent 43908 4b3c8df189bc
child 44191 732098027b34
permissions -rw-r--r--
rust-utils: add Rust implementation of Python's "os.path.splitdrive" I also wrote the NT version although I didn't mean to at first, so I thought I would keep it, so that any further effort to get the Rust code working on Windows is a little easier. Differential Revision: https://phab.mercurial-scm.org/D7864
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
use std::borrow::Borrow;
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::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
    10
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
    11
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
    12
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
    13
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    14
#[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
    15
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
    16
    /// 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
    17
    LeadingSlash(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
    18
    /// Bytes and index of the second slash
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    19
    ConsecutiveSlashes(Vec<u8>, usize),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    20
    /// Bytes and index of the null byte
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    21
    ContainsNullByte(Vec<u8>, usize),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    22
    /// Bytes
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    23
    DecodeError(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
    24
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    25
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    26
impl ToString for HgPathError {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    27
    fn to_string(&self) -> String {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    28
        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
    29
            HgPathError::LeadingSlash(bytes) => {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    30
                format!("Invalid HgPath '{:?}': has a leading slash.", bytes)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    31
            }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    32
            HgPathError::ConsecutiveSlashes(bytes, pos) => format!(
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    33
                "Invalid HgPath '{:?}': consecutive slahes 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
    34
                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
    35
            ),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    36
            HgPathError::ContainsNullByte(bytes, pos) => format!(
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    37
                "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
    38
                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
    39
            ),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    40
            HgPathError::DecodeError(bytes) => {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    41
                format!("Invalid HgPath '{:?}': could not be decoded.", bytes)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    42
            }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    43
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    44
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    45
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    46
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    47
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
    48
    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
    49
        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
    50
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    51
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    52
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
/// 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
    54
///     - 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
    55
///     - `/` 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
    56
///     - 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
    57
///     - 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
    58
///     - 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
    59
///     - 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
    60
///
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    61
/// 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
    62
/// 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
    63
/// 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
    64
/// operation.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    65
///
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    66
/// 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
    67
/// 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
    68
/// 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
    69
/// 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
    70
/// 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
    71
/// character encoding will be determined on a per-repository basis.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    72
//
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    73
// FIXME: (adapted from a comment in the stdlib)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    74
// `HgPath::new()` current implementation relies on `Slice` being
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    75
// layout-compatible with `[u8]`.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    76
// When attribute privacy is implemented, `Slice` should be annotated as
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    77
// `#[repr(transparent)]`.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    78
// Anyway, `Slice` representation and layout are considered implementation
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    79
// detail, are not documented and must not be relied upon.
43908
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
    80
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash)]
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    81
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
    82
    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
    83
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    84
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    85
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
    86
    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
    87
        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
    88
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    89
    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
    90
        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
    91
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    92
    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
    93
        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
    94
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    95
    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
    96
        HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    97
            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
    98
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    99
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   100
    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
   101
        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
   102
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   103
    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
   104
        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
   105
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   106
    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
   107
        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
   108
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   109
    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
   110
        &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
   111
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   112
    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
   113
        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
   114
    }
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
   115
    pub fn starts_with(&self, needle: impl AsRef<HgPath>) -> bool {
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
   116
        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
   117
    }
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   118
    pub fn join<T: ?Sized + AsRef<HgPath>>(&self, other: &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
   119
        let mut 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
   120
        if inner.len() != 0 && inner.last() != Some(&b'/') {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   121
            inner.push(b'/');
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   122
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   123
        inner.extend(other.as_ref().bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   124
        HgPathBuf::from_bytes(&inner)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   125
    }
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
   126
    /// 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
   127
    /// 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
   128
    /// `b'/'`), returns `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
   129
    pub fn relative_to(&self, base: impl AsRef<HgPath>) -> Option<&HgPath> {
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
   130
        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
   131
        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
   132
            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
   133
        }
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
   134
        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
   135
        if is_dir && self.starts_with(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
   136
            Some(HgPath::new(&self.inner[base.len()..]))
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
   137
        } 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
   138
            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
   139
        }
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
   140
    }
44136
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   141
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   142
    #[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
   143
    /// 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
   144
    ///
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   145
    /// Split a pathname into drive/UNC sharepoint and relative path specifiers.
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   146
    /// Returns a 2-tuple (drive_or_unc, path); either part may be empty.
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   147
    ///
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   148
    /// 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
   149
    ///  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
   150
    /// 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
   151
    ///  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
   152
    ///
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   153
    /// If the path contained a drive letter, drive_or_unc will contain everything
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   154
    /// up to and including the colon.
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   155
    /// 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
   156
    ///
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   157
    /// If the path contained a UNC path, the drive_or_unc will contain the host
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   158
    /// name and share up to but not including the fourth directory separator
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   159
    /// character.
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   160
    /// e.g. split_drive("//host/computer/dir") returns ("//host/computer", "/dir")
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   161
    ///
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   162
    /// 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
   163
    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
   164
        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
   165
        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
   166
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   167
        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
   168
            (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
   169
        } 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
   170
            && 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
   171
            && (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
   172
        {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   173
            // 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
   174
            // 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
   175
            // \\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
   176
            //           directory ^^^^^^^^^^^^^^^
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   177
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   178
            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
   179
            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
   180
                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
   181
            } else {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   182
                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
   183
            };
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   184
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   185
            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
   186
                .iter()
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   187
                .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
   188
            {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   189
                // 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
   190
                // (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
   191
                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
   192
                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
   193
                    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
   194
                        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
   195
                    (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
   196
                }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   197
                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
   198
            }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   199
        } 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
   200
            // 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
   201
            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
   202
            (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
   203
        } else {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   204
            (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
   205
        }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   206
    }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   207
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   208
    #[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
   209
    /// 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
   210
    pub fn split_drive(&self) -> (&HgPath, &HgPath) {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   211
        (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
   212
    }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   213
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   214
    /// 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
   215
    /// 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
   216
    /// 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
   217
    pub fn check_state(&self) -> Result<(), HgPathError> {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   218
        if self.len() == 0 {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   219
            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
   220
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   221
        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
   222
        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
   223
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   224
        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
   225
            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
   226
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   227
        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
   228
            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
   229
                0 => {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   230
                    return Err(HgPathError::ContainsNullByte(
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   231
                        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
   232
                        index,
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   233
                    ))
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   234
                }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   235
                b'/' => {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   236
                    if previous_byte.is_some() && previous_byte == Some(b'/') {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   237
                        return Err(HgPathError::ConsecutiveSlashes(
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   238
                            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
   239
                            index,
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   240
                        ));
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   241
                    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   242
                }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   243
                _ => (),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   244
            };
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   245
            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
   246
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   247
        Ok(())
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   248
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   249
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   250
    #[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
   251
    /// 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
   252
    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
   253
        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
   254
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   255
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   256
43908
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   257
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
   258
    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
   259
        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
   260
    }
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   261
}
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   262
43827
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   263
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
   264
    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
   265
        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
   266
    }
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   267
}
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   268
43908
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   269
#[derive(Eq, Ord, Clone, PartialEq, PartialOrd, Hash)]
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   270
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
   271
    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
   272
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   273
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   274
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
   275
    pub fn new() -> Self {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   276
        Self { inner: 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
   277
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   278
    pub fn push(&mut self, byte: u8) {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   279
        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
   280
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   281
    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
   282
        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
   283
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   284
    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
   285
        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
   286
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   287
    pub fn as_ref(&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
   288
        self.inner.as_ref()
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   289
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   290
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   291
43908
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   292
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
   293
    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
   294
        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
   295
    }
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   296
}
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   297
43827
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   298
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
   299
    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
   300
        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
   301
    }
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   302
}
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   303
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   304
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
   305
    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
   306
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   307
    #[inline]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   308
    fn deref(&self) -> &HgPath {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   309
        &HgPath::new(&self.inner)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   310
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   311
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   312
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   313
impl From<Vec<u8>> for HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   314
    fn from(vec: Vec<u8>) -> Self {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   315
        Self { inner: vec }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   316
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   317
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   318
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   319
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
   320
    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
   321
        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
   322
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   323
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   324
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   325
impl Into<Vec<u8>> for HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   326
    fn into(self) -> Vec<u8> {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   327
        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
   328
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   329
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   330
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   331
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
   332
    fn borrow(&self) -> &HgPath {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   333
        &HgPath::new(self.as_bytes())
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   334
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   335
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   336
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   337
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
   338
    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
   339
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   340
    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
   341
        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
   342
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   343
}
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
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
   346
    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
   347
        self
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   348
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   349
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   350
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   351
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
   352
    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
   353
        self
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   354
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   355
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   356
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   357
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
   358
    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
   359
        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
   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
/// TODO: Once https://www.mercurial-scm.org/wiki/WindowsUTF8Plan is
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   364
/// 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
   365
/// 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
   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
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
   368
    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
   369
) -> 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
   370
    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
   371
    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
   372
    #[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
   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
        use std::os::unix::ffi::OsStrExt;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   375
        os_str = std::ffi::OsStr::from_bytes(&hg_path.as_ref().as_bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   376
    }
43250
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
   377
    // 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
   378
    // 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
   379
    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
   380
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   381
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   382
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
   383
    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
   384
) -> 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
   385
    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
   386
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   387
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   388
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
   389
    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
   390
) -> 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
   391
    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
   392
    #[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
   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
        use std::os::unix::ffi::OsStrExt;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   395
        buf = HgPathBuf::from_bytes(&os_string.as_ref().as_bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   396
    }
43250
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
   397
    // 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
   398
    // 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
   399
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   400
    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
   401
    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
   402
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   403
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   404
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
   405
    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
   406
) -> 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
   407
    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
   408
    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
   409
    #[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
   410
    {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   411
        use std::os::unix::ffi::OsStrExt;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   412
        buf = HgPathBuf::from_bytes(&os_str.as_bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   413
    }
43250
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
   414
    // 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
   415
    // 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
   416
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   417
    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
   418
    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
   419
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   420
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   421
#[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
   422
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
   423
    use super::*;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   424
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   425
    #[test]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   426
    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
   427
        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
   428
            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
   429
            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
   430
        );
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   431
        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
   432
            Err(HgPathError::ConsecutiveSlashes(b"a/b//c".to_vec(), 4)),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   433
            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
   434
        );
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   435
        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
   436
            Err(HgPathError::ContainsNullByte(b"a/b/\0c".to_vec(), 4)),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   437
            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
   438
        );
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   439
        // 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
   440
        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
   441
        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
   442
        // 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
   443
        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
   444
        // 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
   445
        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
   446
        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
   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
    #[test]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   450
    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
   451
        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
   452
        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
   453
        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
   454
        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
   455
        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
   456
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   457
        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
   458
        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
   459
        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
   460
        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
   461
        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
   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
        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
   464
        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
   465
        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
   466
        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
   467
        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
   468
        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
   469
        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
   470
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   471
        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
   472
        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
   473
        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
   474
        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
   475
        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
   476
        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
   477
        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
   478
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   479
        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
   480
        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
   481
        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
   482
        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
   483
        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
   484
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   485
        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
   486
        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
   487
        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
   488
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   489
        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
   490
        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
   491
        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
   492
    }
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
    #[test]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   495
    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
   496
        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
   497
        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
   498
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   499
        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
   500
        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
   501
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   502
        // 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
   503
        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
   504
        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
   505
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   506
        // 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
   507
        // 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
   508
        // 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
   509
        // 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
   510
        // 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
   511
        // `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
   512
        // 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
   513
        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
   514
        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
   515
        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
   516
        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
   517
    }
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
   518
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
   519
    #[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
   520
    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
   521
        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
   522
        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
   523
        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
   524
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
   525
        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
   526
        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
   527
        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
   528
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
   529
        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
   530
        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
   531
        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
   532
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
   533
        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
   534
        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
   535
        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
   536
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
   537
        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
   538
        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
   539
        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
   540
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
   541
        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
   542
        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
   543
        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
   544
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
   545
        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
   546
        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
   547
        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
   548
    }
44136
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   549
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   550
    #[test]
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   551
    #[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
   552
    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
   553
        // 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
   554
        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
   555
            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
   556
            (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
   557
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   558
        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
   559
            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
   560
            (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
   561
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   562
        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
   563
            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
   564
            (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
   565
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   566
        // 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
   567
        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
   568
            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
   569
            (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
   570
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   571
        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
   572
            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
   573
            (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
   574
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   575
        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
   576
            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
   577
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   578
                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
   579
                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
   580
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   581
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   582
    }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   583
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   584
    #[test]
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   585
    #[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
   586
    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
   587
        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
   588
            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
   589
            (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
   590
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   591
        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
   592
            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
   593
            (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
   594
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   595
        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
   596
            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
   597
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   598
                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
   599
                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
   600
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   601
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   602
        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
   603
            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
   604
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   605
                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
   606
                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
   607
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   608
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   609
        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
   610
            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
   611
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   612
                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
   613
                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
   614
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   615
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   616
        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
   617
            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
   618
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   619
                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
   620
                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
   621
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   622
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   623
        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
   624
            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
   625
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   626
                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
   627
                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
   628
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   629
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   630
        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
   631
            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
   632
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   633
                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
   634
                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
   635
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   636
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   637
        // 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
   638
        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
   639
            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
   640
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   641
                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
   642
                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
   643
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   644
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   645
    }
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   646
}