rust/hg-core/src/utils/hg_path.rs
author Raphaël Gomès <rgomes@octobus.net>
Fri, 29 Nov 2019 17:15:24 +0100
changeset 43827 c27e688fcdc3
parent 43250 98d996a138de
child 43833 4f1543a2f5c3
permissions -rw-r--r--
rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf` This is useful when debugging, to get a human readable output instead of an array of `u8`. Differential Revision: https://phab.mercurial-scm.org/D7523
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.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    80
#[derive(Eq, Ord, PartialEq, PartialOrd, Debug, Hash)]
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
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   115
    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
   116
        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
   117
        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
   118
            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
   119
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   120
        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
   121
        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
   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
    /// 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
   124
    /// 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
   125
    /// 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
   126
    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
   127
        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
   128
            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
   129
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   130
        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
   131
        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
   132
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   133
        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
   134
            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
   135
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   136
        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
   137
            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
   138
                0 => {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   139
                    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
   140
                        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
   141
                        index,
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   142
                    ))
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   143
                }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   144
                b'/' => {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   145
                    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
   146
                        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
   147
                            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
   148
                            index,
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   149
                        ));
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   150
                    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   151
                }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   152
                _ => (),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   153
            };
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   154
            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
   155
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   156
        Ok(())
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   157
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   158
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   159
    #[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
   160
    /// 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
   161
    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
   162
        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
   163
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   164
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   165
43827
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   166
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
   167
    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
   168
        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
   169
    }
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   170
}
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   171
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   172
#[derive(Eq, Ord, Clone, PartialEq, PartialOrd, Debug, Hash)]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   173
pub 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
   174
    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
   175
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   176
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   177
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
   178
    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
   179
        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
   180
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   181
    pub fn 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
   182
        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
   183
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   184
    pub fn 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
   185
        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
   186
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   187
    pub fn 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
   188
        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
   189
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   190
    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
   191
        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
   192
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   193
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   194
43827
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   195
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
   196
    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
   197
        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
   198
    }
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   199
}
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   200
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   201
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
   202
    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
   203
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   204
    #[inline]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   205
    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
   206
        &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
   207
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   208
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   209
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   210
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
   211
    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
   212
        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
   213
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   214
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   215
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   216
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
   217
    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
   218
        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
   219
    }
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
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   222
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
   223
    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
   224
        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
   225
    }
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
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   228
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
   229
    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
   230
        &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
   231
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   232
}
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
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
   235
    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
   236
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   237
    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
   238
        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
   239
    }
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
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
   243
    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
   244
        self
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   245
    }
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
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   248
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
   249
    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
   250
        self
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   251
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   252
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   253
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   254
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
   255
    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
   256
        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
   257
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   258
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   259
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   260
/// 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
   261
/// 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
   262
/// 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
   263
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   264
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
   265
    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
   266
) -> 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
   267
    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
   268
    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
   269
    #[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
   270
    {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   271
        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
   272
        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
   273
    }
43250
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
   274
    // 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
   275
    // 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
   276
    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
   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
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   279
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
   280
    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
   281
) -> 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
   282
    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
   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
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   285
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
   286
    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
   287
) -> 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
   288
    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
   289
    #[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
   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
        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
   292
        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
   293
    }
43250
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
   294
    // 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
   295
    // 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
   296
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   297
    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
   298
    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
   299
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   300
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   301
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
   302
    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
   303
) -> 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
   304
    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
   305
    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
   306
    #[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
   307
    {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   308
        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
   309
        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
   310
    }
43250
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
   311
    // 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
   312
    // 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
   313
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   314
    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
   315
    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
   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
#[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
   319
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
   320
    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
   321
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   322
    #[test]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   323
    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
   324
        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
   325
            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
   326
            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
   327
        );
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   328
        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
   329
            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
   330
            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
   331
        );
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   332
        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
   333
            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
   334
            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
   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
        // 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
   337
        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
   338
        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
   339
        // 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
   340
        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
   341
        // 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
   342
        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
   343
        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
   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
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   346
    #[test]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   347
    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
   348
        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
   349
        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
   350
        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
   351
        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
   352
        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
   353
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   354
        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
   355
        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
   356
        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
   357
        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
   358
        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
   359
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   360
        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
   361
        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
   362
        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
   363
        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
   364
        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
   365
        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
   366
        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
   367
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   368
        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
   369
        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
   370
        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
   371
        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
   372
        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
   373
        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
   374
        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
   375
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   376
        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
   377
        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
   378
        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
   379
        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
   380
        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
   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
        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
   383
        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
   384
        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
   385
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   386
        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
   387
        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
   388
        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
   389
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   390
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   391
    #[test]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   392
    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
   393
        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
   394
        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
   395
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   396
        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
   397
        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
   398
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   399
        // 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
   400
        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
   401
        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
   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
        // 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
   404
        // 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
   405
        // 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
   406
        // 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
   407
        // 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
   408
        // `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
   409
        // 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
   410
        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
   411
        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
   412
        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
   413
        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
   414
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   415
}