rust/hg-core/src/utils/hg_path.rs
author Raphaël Gomès <rgomes@octobus.net>
Fri, 13 Mar 2020 00:41:22 +0100
changeset 44562 ece43c79333e
parent 44267 0e9ac3968b56
child 44973 26114bd6ec60
permissions -rw-r--r--
rust-core: add missing `Debug` traits Turns out you need them when trying to use `hg-core` as a library. Who knew. Differential Revision: https://phab.mercurial-scm.org/D8278
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>),
44265
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    18
    ConsecutiveSlashes {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    19
        bytes: Vec<u8>,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    20
        second_slash_index: usize,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    21
    },
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    22
    ContainsNullByte {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    23
        bytes: Vec<u8>,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    24
        null_byte_index: usize,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    25
    },
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    26
    /// Bytes
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    27
    DecodeError(Vec<u8>),
44265
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    28
    /// The rest come from audit errors
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    29
    EndsWithSlash(HgPathBuf),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    30
    ContainsIllegalComponent(HgPathBuf),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    31
    /// Path is inside the `.hg` folder
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    32
    InsideDotHg(HgPathBuf),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    33
    IsInsideNestedRepo {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    34
        path: HgPathBuf,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    35
        nested_repo: HgPathBuf,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    36
    },
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    37
    TraversesSymbolicLink {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    38
        path: HgPathBuf,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    39
        symlink: HgPathBuf,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    40
    },
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    41
    NotFsCompliant(HgPathBuf),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    42
    /// `path` is the smallest invalid path
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    43
    NotUnderRoot {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    44
        path: PathBuf,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    45
        root: PathBuf,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    46
    },
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    47
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    48
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    49
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
    50
    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
    51
        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
    52
            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
    53
                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
    54
            }
44265
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    55
            HgPathError::ConsecutiveSlashes {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    56
                bytes,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    57
                second_slash_index: pos,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    58
            } => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    59
                "Invalid HgPath '{:?}': consecutive slashes at pos {}.",
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
                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
    61
            ),
44265
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    62
            HgPathError::ContainsNullByte {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    63
                bytes,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    64
                null_byte_index: pos,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    65
            } => format!(
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    66
                "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
    67
                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
    68
            ),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    69
            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
    70
                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
    71
            }
44265
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    72
            HgPathError::EndsWithSlash(path) => {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    73
                format!("Audit failed for '{}': ends with a slash.", path)
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    74
            }
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    75
            HgPathError::ContainsIllegalComponent(path) => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    76
                "Audit failed for '{}': contains an illegal component.",
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    77
                path
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    78
            ),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    79
            HgPathError::InsideDotHg(path) => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    80
                "Audit failed for '{}': is inside the '.hg' folder.",
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    81
                path
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    82
            ),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    83
            HgPathError::IsInsideNestedRepo {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    84
                path,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    85
                nested_repo: nested,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    86
            } => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    87
                "Audit failed for '{}': is inside a nested repository '{}'.",
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    88
                path, nested
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    89
            ),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    90
            HgPathError::TraversesSymbolicLink { path, symlink } => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    91
                "Audit failed for '{}': traverses symbolic link '{}'.",
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    92
                path, symlink
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    93
            ),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    94
            HgPathError::NotFsCompliant(path) => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    95
                "Audit failed for '{}': cannot be turned into a \
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    96
                 filesystem path.",
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    97
                path
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    98
            ),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
    99
            HgPathError::NotUnderRoot { path, root } => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   100
                "Audit failed for '{}': not under root {}.",
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   101
                path.display(),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   102
                root.display()
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   103
            ),
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   104
        }
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
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   107
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   108
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
   109
    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
   110
        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
   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
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   113
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   114
/// 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
   115
///     - 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
   116
///     - `/` 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
   117
///     - 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
   118
///     - 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
   119
///     - 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
   120
///     - 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
   121
///
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   122
/// 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
   123
/// 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
   124
/// 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
   125
/// operation.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   126
///
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   127
/// This 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
   128
/// 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
   129
/// 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
   130
/// 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
   131
/// 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
   132
/// 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
   133
//
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   134
// 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
   135
// `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
   136
// 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
   137
// 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
   138
// `#[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
   139
// 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
   140
// 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
   141
#[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
   142
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
   143
    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
   144
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   145
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   146
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
   147
    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
   148
        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
   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
    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
   151
        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
   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
    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
   154
        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
   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
    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
   157
        HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   158
            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
   159
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   160
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   161
    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
   162
        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
   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
    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
   165
        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
   166
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   167
    pub fn 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
   168
        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
   169
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   170
    pub fn 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
   171
        &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
   172
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   173
    pub fn 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
   174
        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
   175
    }
44266
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   176
    pub fn starts_with(&self, needle: impl AsRef<Self>) -> bool {
43833
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Raphaël Gomès <rgomes@octobus.net>
parents: 43827
diff changeset
   177
        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
   178
    }
44266
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   179
    pub fn trim_trailing_slash(&self) -> &Self {
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   180
        Self::new(if self.inner.last() == Some(&b'/') {
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   181
            &self.inner[..self.inner.len() - 1]
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   182
        } else {
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   183
            &self.inner[..]
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   184
        })
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   185
    }
44267
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   186
    /// Returns a tuple of slices `(base, filename)` resulting from the split
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   187
    /// at the rightmost `/`, if any.
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   188
    ///
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   189
    /// # Examples:
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   190
    ///
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   191
    /// ```
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   192
    /// use hg::utils::hg_path::HgPath;
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   193
    ///
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   194
    /// let path = HgPath::new(b"cool/hg/path").split_filename();
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   195
    /// assert_eq!(path, (HgPath::new(b"cool/hg"), HgPath::new(b"path")));
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   196
    ///
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   197
    /// let path = HgPath::new(b"pathwithoutsep").split_filename();
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   198
    /// assert_eq!(path, (HgPath::new(b""), HgPath::new(b"pathwithoutsep")));
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   199
    /// ```
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   200
    pub fn split_filename(&self) -> (&Self, &Self) {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   201
        match &self.inner.iter().rposition(|c| *c == b'/') {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   202
            None => (HgPath::new(""), &self),
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   203
            Some(size) => (
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   204
                HgPath::new(&self.inner[..*size]),
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   205
                HgPath::new(&self.inner[*size + 1..]),
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   206
            ),
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   207
        }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 44266
diff changeset
   208
    }
44266
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   209
    pub fn join<T: ?Sized + AsRef<Self>>(&self, other: &T) -> HgPathBuf {
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   210
        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
   211
        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
   212
            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
   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
        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
   215
        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
   216
    }
44266
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   217
    pub fn parent(&self) -> &Self {
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   218
        let inner = self.as_bytes();
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   219
        HgPath::new(match inner.iter().rposition(|b| *b == b'/') {
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   220
            Some(pos) => &inner[..pos],
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   221
            None => &[],
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   222
        })
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   223
    }
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
   224
    /// 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
   225
    /// 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
   226
    /// `b'/'`), returns `None`.
44266
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   227
    pub fn relative_to(&self, base: impl AsRef<Self>) -> Option<&Self> {
43833
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Raphaël Gomès <rgomes@octobus.net>
parents: 43827
diff changeset
   228
        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
   229
        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
   230
            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
   231
        }
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
   232
        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
   233
        if is_dir && self.starts_with(base) {
44266
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   234
            Some(Self::new(&self.inner[base.len()..]))
43833
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Raphaël Gomès <rgomes@octobus.net>
parents: 43827
diff changeset
   235
        } 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
   236
            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
   237
        }
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
   238
    }
44136
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   239
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   240
    #[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
   241
    /// 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
   242
    ///
44191
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44136
diff changeset
   243
    /// Split a pathname into drive/UNC sharepoint and relative path
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44136
diff changeset
   244
    /// specifiers. Returns a 2-tuple (drive_or_unc, path); either part may
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44136
diff changeset
   245
    /// be empty.
44136
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   246
    ///
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   247
    /// 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
   248
    ///  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
   249
    /// 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
   250
    ///  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
   251
    ///
44191
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44136
diff changeset
   252
    /// If the path contained a drive letter, drive_or_unc will contain
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44136
diff changeset
   253
    /// everything up to and including the colon.
44136
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   254
    /// 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
   255
    ///
44191
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44136
diff changeset
   256
    /// If the path contained a UNC path, the drive_or_unc will contain the
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44136
diff changeset
   257
    /// host name and share up to but not including the fourth directory
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44136
diff changeset
   258
    /// separator character.
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44136
diff changeset
   259
    /// e.g. split_drive("//host/computer/dir") returns ("//host/computer",
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44136
diff changeset
   260
    /// "/dir")
44136
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   261
    ///
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   262
    /// 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
   263
    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
   264
        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
   265
        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
   266
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   267
        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
   268
            (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
   269
        } 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
   270
            && 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
   271
            && (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
   272
        {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   273
            // 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
   274
            // 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
   275
            // \\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
   276
            //           directory ^^^^^^^^^^^^^^^
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   277
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   278
            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
   279
            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
   280
                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
   281
            } else {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   282
                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
   283
            };
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   284
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   285
            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
   286
                .iter()
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   287
                .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
   288
            {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   289
                // 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
   290
                // (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
   291
                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
   292
                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
   293
                    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
   294
                        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
   295
                    (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
   296
                }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   297
                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
   298
            }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   299
        } else 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
   300
            // 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
   301
            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
   302
            (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
   303
        } else {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   304
            (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
   305
        }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   306
    }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   307
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   308
    #[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
   309
    /// 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
   310
    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
   311
        (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
   312
    }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
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
    /// 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
   315
    /// 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
   316
    /// 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
   317
    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
   318
        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
   319
            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
   320
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   321
        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
   322
        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
   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
        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
   325
            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
   326
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   327
        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
   328
            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
   329
                0 => {
44265
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   330
                    return Err(HgPathError::ContainsNullByte {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   331
                        bytes: bytes.to_vec(),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   332
                        null_byte_index: index,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   333
                    })
42956
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
                b'/' => {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   336
                    if previous_byte.is_some() && previous_byte == Some(b'/') {
44265
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   337
                        return Err(HgPathError::ConsecutiveSlashes {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   338
                            bytes: bytes.to_vec(),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   339
                            second_slash_index: index,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   340
                        });
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   341
                    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   342
                }
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
            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
   346
        }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   347
        Ok(())
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
    #[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
   351
    /// 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
   352
    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
   353
        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
   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
43908
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   357
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
   358
    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
   359
        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
   360
    }
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   361
}
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   362
43827
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   363
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
   364
    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
   365
        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
   366
    }
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   367
}
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   368
43908
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   369
#[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
   370
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
   371
    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
   372
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   373
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   374
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
   375
    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
   376
        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
   377
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   378
    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
   379
        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
   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
    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
   382
        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
   383
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   384
    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
   385
        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
   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
    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
   388
        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
   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
43908
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   392
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
   393
    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
   394
        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
   395
    }
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   396
}
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43833
diff changeset
   397
43827
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   398
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
   399
    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
   400
        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
   401
    }
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   402
}
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Raphaël Gomès <rgomes@octobus.net>
parents: 43250
diff changeset
   403
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   404
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
   405
    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
   406
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   407
    #[inline]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   408
    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
   409
        &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
   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
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   412
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   413
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
   414
    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
   415
        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
   416
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   417
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   418
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   419
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
   420
    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
   421
        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
   422
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   423
}
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
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
   426
    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
   427
        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
   428
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   429
}
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
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
   432
    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
   433
        &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
   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
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   436
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   437
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
   438
    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
   439
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   440
    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
   441
        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
   442
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   443
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   444
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   445
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
   446
    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
   447
        self
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   448
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   449
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   450
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   451
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
   452
    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
   453
        self
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   454
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   455
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   456
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   457
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
   458
    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
   459
        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
   460
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   461
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   462
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   463
/// 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
   464
/// 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
   465
/// 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
   466
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   467
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
   468
    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
   469
) -> 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
   470
    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
   471
    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
   472
    #[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
   473
    {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   474
        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
   475
        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
   476
    }
43250
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
   477
    // 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
   478
    // 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
   479
    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
   480
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   481
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   482
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
   483
    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
   484
) -> 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
   485
    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
   486
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   487
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   488
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
   489
    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
   490
) -> 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
   491
    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
   492
    #[cfg(unix)]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   493
    {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   494
        use std::os::unix::ffi::OsStrExt;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   495
        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
   496
    }
43250
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
   497
    // TODO Handle other platforms
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
   498
    // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
   499
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   500
    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
   501
    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
   502
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   503
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   504
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
   505
    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
   506
) -> 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
   507
    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
   508
    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
   509
    #[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
   510
    {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   511
        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
   512
        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
   513
    }
43250
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
   514
    // 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
   515
    // 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
   516
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   517
    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
   518
    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
   519
}
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   520
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   521
#[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
   522
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
   523
    use super::*;
44266
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   524
    use pretty_assertions::assert_eq;
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   525
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   526
    #[test]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   527
    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
   528
        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
   529
            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
   530
            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
   531
        );
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   532
        assert_eq!(
44265
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   533
            Err(HgPathError::ConsecutiveSlashes {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   534
                bytes: b"a/b//c".to_vec(),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   535
                second_slash_index: 4
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   536
            }),
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   537
            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
   538
        );
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   539
        assert_eq!(
44265
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   540
            Err(HgPathError::ContainsNullByte {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   541
                bytes: b"a/b/\0c".to_vec(),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   542
                null_byte_index: 4
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44191
diff changeset
   543
            }),
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   544
            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
   545
        );
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   546
        // 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
   547
        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
   548
        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
   549
        // 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
   550
        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
   551
        // 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
   552
        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
   553
        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
   554
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   555
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   556
    #[test]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   557
    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
   558
        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
   559
        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
   560
        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
   561
        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
   562
        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
   563
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   564
        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
   565
        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
   566
        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
   567
        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
   568
        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
   569
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   570
        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
   571
        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
   572
        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
   573
        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
   574
        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
   575
        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
   576
        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
   577
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   578
        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
   579
        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
   580
        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
   581
        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
   582
        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
   583
        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
   584
        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
   585
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   586
        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
   587
        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
   588
        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
   589
        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
   590
        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
   591
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   592
        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
   593
        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
   594
        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
   595
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   596
        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
   597
        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
   598
        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
   599
    }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   600
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   601
    #[test]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   602
    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
   603
        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
   604
        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
   605
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   606
        let path = 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
   607
        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
   608
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   609
        // 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
   610
        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
   611
        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
   612
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   613
        // 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
   614
        // 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
   615
        // 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
   616
        // 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
   617
        // 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
   618
        // `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
   619
        // 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
   620
        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
   621
        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
   622
        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
   623
        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
   624
    }
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
   625
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
   626
    #[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
   627
    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
   628
        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
   629
        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
   630
        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
   631
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
   632
        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
   633
        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
   634
        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
   635
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
   636
        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
   637
        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
   638
        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
   639
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
   640
        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
   641
        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
   642
        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
   643
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
   644
        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
   645
        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
   646
        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
   647
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
   648
        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
   649
        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
   650
        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
   651
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
   652
        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
   653
        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
   654
        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
   655
    }
44136
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   656
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   657
    #[test]
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   658
    #[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
   659
    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
   660
        // 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
   661
        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
   662
            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
   663
            (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
   664
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   665
        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
   666
            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
   667
            (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
   668
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   669
        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
   670
            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
   671
            (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
   672
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   673
        // 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
   674
        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
   675
            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
   676
            (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
   677
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   678
        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
   679
            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
   680
            (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
   681
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   682
        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
   683
            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
   684
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   685
                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
   686
                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
   687
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   688
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   689
    }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   690
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   691
    #[test]
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   692
    #[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
   693
    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
   694
        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
   695
            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
   696
            (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
   697
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   698
        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
   699
            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
   700
            (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
   701
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   702
        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
   703
            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
   704
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   705
                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
   706
                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
   707
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   708
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   709
        assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   710
            HgPath::new(br"//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
   711
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   712
                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
   713
                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
   714
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   715
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   716
        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
   717
            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
   718
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   719
                HgPath::new(br""),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   720
                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
   721
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   722
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   723
        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
   724
            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
   725
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   726
                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
   727
                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
   728
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   729
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   730
        assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   731
            HgPath::new(br"\\conky\\mountpoint\foo\bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   732
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   733
                HgPath::new(br""),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   734
                HgPath::new(br"\\conky\\mountpoint\foo\bar")
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   735
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   736
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   737
        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
   738
            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
   739
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   740
                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
   741
                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
   742
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   743
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   744
        // 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
   745
        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
   746
            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
   747
            (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   748
                HgPath::new(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
   749
                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
   750
            )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   751
        );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Raphaël Gomès <rgomes@octobus.net>
parents: 43908
diff changeset
   752
    }
44266
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   753
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   754
    #[test]
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   755
    fn test_parent() {
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   756
        let path = HgPath::new(b"");
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   757
        assert_eq!(path.parent(), path);
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   758
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   759
        let path = HgPath::new(b"a");
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   760
        assert_eq!(path.parent(), HgPath::new(b""));
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   761
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   762
        let path = HgPath::new(b"a/b");
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   763
        assert_eq!(path.parent(), HgPath::new(b"a"));
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   764
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   765
        let path = HgPath::new(b"a/other/b");
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   766
        assert_eq!(path.parent(), HgPath::new(b"a/other"));
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   767
    }
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   768
}