rust/hg-core/src/dirstate/dirs_multiset.rs
author Pierre-Yves David <pierre-yves.david@octobus.net>
Mon, 30 Aug 2021 18:45:54 +0200
changeset 47972 e02f9af7aed1
parent 47343 ed1583a845d2
child 48044 f2a9db29cb2d
permissions -rw-r--r--
pathutil: replace the `skip` argument of `dirs` with a boolean It is ever only used for `r` file. So we make it a boolean this will give use more versatility later as we will stop storing the state explicitly. Differential Revision: https://phab.mercurial-scm.org/D11383
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
// dirs_multiset.rs
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
//
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
//
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
// This software may be used and distributed according to the terms of the
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
// GNU General Public License version 2 or any later version.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
//! A multiset of directory names.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     9
//!
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    10
//! Used to counts the references to directories in a manifest or dirstate.
47343
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
    11
use crate::dirstate_tree::on_disk::DirstateV2ParseError;
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    12
use crate::{
44314
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
    13
    dirstate::EntryState,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
    14
    utils::{
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
    15
        files,
44329
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    16
        hg_path::{HgPath, HgPathBuf, HgPathError},
44314
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
    17
    },
47343
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
    18
    DirstateEntry, DirstateError, DirstateMapError, FastHashMap,
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    19
};
44314
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
    20
use std::collections::{hash_map, hash_map::Entry, HashMap, HashSet};
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    21
42891
a03a29462c0a rust-dirstate: specify concrete return type of DirsMultiset::iter()
Yuya Nishihara <yuya@tcha.org>
parents: 42851
diff changeset
    22
// could be encapsulated if we care API stability more seriously
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
    23
pub type DirsMultisetIter<'a> = hash_map::Keys<'a, HgPathBuf, u32>;
42891
a03a29462c0a rust-dirstate: specify concrete return type of DirsMultiset::iter()
Yuya Nishihara <yuya@tcha.org>
parents: 42851
diff changeset
    24
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    25
#[derive(PartialEq, Debug)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    26
pub struct DirsMultiset {
43844
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 43808
diff changeset
    27
    inner: FastHashMap<HgPathBuf, u32>,
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    28
}
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    29
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    30
impl DirsMultiset {
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    31
    /// Initializes the multiset from a dirstate.
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    32
    ///
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    33
    /// If `skip_state` is provided, skips dirstate entries with equal state.
47340
4ee9f419c52e rust: Return owned instead of borrowed DirstateEntry in DirstateMap APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47138
diff changeset
    34
    pub fn from_dirstate<I, P>(
47138
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47107
diff changeset
    35
        dirstate: I,
47972
e02f9af7aed1 pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47343
diff changeset
    36
        only_tracked: bool,
47343
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
    37
    ) -> Result<Self, DirstateError>
47138
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47107
diff changeset
    38
    where
47343
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
    39
        I: IntoIterator<
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
    40
            Item = Result<(P, DirstateEntry), DirstateV2ParseError>,
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
    41
        >,
47138
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47107
diff changeset
    42
        P: AsRef<HgPath>,
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47107
diff changeset
    43
    {
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    44
        let mut multiset = DirsMultiset {
43844
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 43808
diff changeset
    45
            inner: FastHashMap::default(),
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    46
        };
47343
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
    47
        for item in dirstate {
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
    48
            let (filename, entry) = item?;
47138
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47107
diff changeset
    49
            let filename = filename.as_ref();
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    50
            // This `if` is optimized out of the loop
47972
e02f9af7aed1 pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47343
diff changeset
    51
            if only_tracked {
e02f9af7aed1 pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47343
diff changeset
    52
                if entry.state != EntryState::Removed {
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    53
                    multiset.add_path(filename)?;
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    54
                }
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    55
            } else {
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    56
                multiset.add_path(filename)?;
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
            }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    58
        }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    59
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    60
        Ok(multiset)
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    61
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    62
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    63
    /// Initializes the multiset from a manifest.
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    64
    pub fn from_manifest(
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    65
        manifest: &[impl AsRef<HgPath>],
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    66
    ) -> Result<Self, DirstateMapError> {
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    67
        let mut multiset = DirsMultiset {
43844
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 43808
diff changeset
    68
            inner: FastHashMap::default(),
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    69
        };
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    70
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
    71
        for filename in manifest {
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    72
            multiset.add_path(filename.as_ref())?;
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    73
        }
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    74
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    75
        Ok(multiset)
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    76
    }
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    77
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    78
    /// Increases the count of deepest directory contained in the path.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    79
    ///
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    80
    /// If the directory is not yet in the map, adds its parents.
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
    81
    pub fn add_path(
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
    82
        &mut self,
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
    83
        path: impl AsRef<HgPath>,
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
    84
    ) -> Result<(), DirstateMapError> {
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
    85
        for subpath in files::find_dirs(path.as_ref()) {
43808
1fe2e574616e rust-dirs: address failing tests for `dirs` impl with a temporary fix
Raphaël Gomès <rgomes@octobus.net>
parents: 42960
diff changeset
    86
            if subpath.as_bytes().last() == Some(&b'/') {
1fe2e574616e rust-dirs: address failing tests for `dirs` impl with a temporary fix
Raphaël Gomès <rgomes@octobus.net>
parents: 42960
diff changeset
    87
                // TODO Remove this once PathAuditor is certified
1fe2e574616e rust-dirs: address failing tests for `dirs` impl with a temporary fix
Raphaël Gomès <rgomes@octobus.net>
parents: 42960
diff changeset
    88
                // as the only entrypoint for path data
44329
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    89
                let second_slash_index = subpath.len() - 1;
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    90
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    91
                return Err(DirstateMapError::InvalidPath(
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    92
                    HgPathError::ConsecutiveSlashes {
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    93
                        bytes: path.as_ref().as_bytes().to_owned(),
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    94
                        second_slash_index,
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    95
                    },
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    96
                ));
43808
1fe2e574616e rust-dirs: address failing tests for `dirs` impl with a temporary fix
Raphaël Gomès <rgomes@octobus.net>
parents: 42960
diff changeset
    97
            }
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    98
            if let Some(val) = self.inner.get_mut(subpath) {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    99
                *val += 1;
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   100
                break;
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   101
            }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   102
            self.inner.insert(subpath.to_owned(), 1);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   103
        }
43808
1fe2e574616e rust-dirs: address failing tests for `dirs` impl with a temporary fix
Raphaël Gomès <rgomes@octobus.net>
parents: 42960
diff changeset
   104
        Ok(())
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   105
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   106
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   107
    /// Decreases the count of deepest directory contained in the path.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   108
    ///
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   109
    /// If it is the only reference, decreases all parents until one is
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   110
    /// removed.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   111
    /// If the directory is not in the map, something horrible has happened.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   112
    pub fn delete_path(
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   113
        &mut self,
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
   114
        path: impl AsRef<HgPath>,
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   115
    ) -> Result<(), DirstateMapError> {
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
   116
        for subpath in files::find_dirs(path.as_ref()) {
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   117
            match self.inner.entry(subpath.to_owned()) {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   118
                Entry::Occupied(mut entry) => {
44998
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44329
diff changeset
   119
                    let val = *entry.get();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   120
                    if val > 1 {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   121
                        entry.insert(val - 1);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   122
                        break;
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   123
                    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   124
                    entry.remove();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   125
                }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   126
                Entry::Vacant(_) => {
42560
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42543
diff changeset
   127
                    return Err(DirstateMapError::PathNotFound(
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
   128
                        path.as_ref().to_owned(),
42560
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42543
diff changeset
   129
                    ))
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   130
                }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   131
            };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   132
        }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   133
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   134
        Ok(())
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   135
    }
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   136
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
   137
    pub fn contains(&self, key: impl AsRef<HgPath>) -> bool {
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
   138
        self.inner.contains_key(key.as_ref())
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   139
    }
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   140
42891
a03a29462c0a rust-dirstate: specify concrete return type of DirsMultiset::iter()
Yuya Nishihara <yuya@tcha.org>
parents: 42851
diff changeset
   141
    pub fn iter(&self) -> DirsMultisetIter {
42766
849e744b925d rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 42765
diff changeset
   142
        self.inner.keys()
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   143
    }
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   144
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   145
    pub fn len(&self) -> usize {
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   146
        self.inner.len()
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   147
    }
44998
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44329
diff changeset
   148
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44329
diff changeset
   149
    pub fn is_empty(&self) -> bool {
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44329
diff changeset
   150
        self.len() == 0
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44329
diff changeset
   151
    }
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   152
}
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   153
44314
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   154
/// This is basically a reimplementation of `DirsMultiset` that stores the
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   155
/// children instead of just a count of them, plus a small optional
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   156
/// optimization to avoid some directories we don't need.
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   157
#[derive(PartialEq, Debug)]
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   158
pub struct DirsChildrenMultiset<'a> {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   159
    inner: FastHashMap<&'a HgPath, HashSet<&'a HgPath>>,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   160
    only_include: Option<HashSet<&'a HgPath>>,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   161
}
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   162
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   163
impl<'a> DirsChildrenMultiset<'a> {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   164
    pub fn new(
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   165
        paths: impl Iterator<Item = &'a HgPathBuf>,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   166
        only_include: Option<&'a HashSet<impl AsRef<HgPath> + 'a>>,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   167
    ) -> Self {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   168
        let mut new = Self {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   169
            inner: HashMap::default(),
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   170
            only_include: only_include
44998
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44329
diff changeset
   171
                .map(|s| s.iter().map(AsRef::as_ref).collect()),
44314
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   172
        };
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   173
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   174
        for path in paths {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   175
            new.add_path(path)
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   176
        }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   177
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   178
        new
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   179
    }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   180
    fn add_path(&mut self, path: &'a (impl AsRef<HgPath> + 'a)) {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   181
        if path.as_ref().is_empty() {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   182
            return;
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   183
        }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   184
        for (directory, basename) in files::find_dirs_with_base(path.as_ref())
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   185
        {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   186
            if !self.is_dir_included(directory) {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   187
                continue;
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   188
            }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   189
            self.inner
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   190
                .entry(directory)
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   191
                .and_modify(|e| {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   192
                    e.insert(basename);
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   193
                })
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   194
                .or_insert_with(|| {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   195
                    let mut set = HashSet::new();
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   196
                    set.insert(basename);
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   197
                    set
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   198
                });
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   199
        }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   200
    }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   201
    fn is_dir_included(&self, dir: impl AsRef<HgPath>) -> bool {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   202
        match &self.only_include {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   203
            None => false,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   204
            Some(i) => i.contains(dir.as_ref()),
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   205
        }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   206
    }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   207
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   208
    pub fn get(
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   209
        &self,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   210
        path: impl AsRef<HgPath>,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   211
    ) -> Option<&HashSet<&'a HgPath>> {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   212
        self.inner.get(path.as_ref())
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   213
    }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   214
}
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   215
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   216
#[cfg(test)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   217
mod tests {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   218
    use super::*;
47107
787ff5d21bcd dirstate-tree: Make Rust DirstateMap bindings go through a trait object
Simon Sapin <simon.sapin@octobus.net>
parents: 46890
diff changeset
   219
    use crate::StateMap;
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   220
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   221
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   222
    fn test_delete_path_path_not_found() {
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
   223
        let manifest: Vec<HgPathBuf> = vec![];
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   224
        let mut map = DirsMultiset::from_manifest(&manifest).unwrap();
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   225
        let path = HgPathBuf::from_bytes(b"doesnotexist/");
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   226
        assert_eq!(
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   227
            Err(DirstateMapError::PathNotFound(path.to_owned())),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   228
            map.delete_path(&path)
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   229
        );
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   230
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   231
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   232
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   233
    fn test_delete_path_empty_path() {
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   234
        let mut map =
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   235
            DirsMultiset::from_manifest(&vec![HgPathBuf::new()]).unwrap();
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   236
        let path = HgPath::new(b"");
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   237
        assert_eq!(Ok(()), map.delete_path(path));
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   238
        assert_eq!(
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   239
            Err(DirstateMapError::PathNotFound(path.to_owned())),
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   240
            map.delete_path(path)
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   241
        );
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   242
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   243
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   244
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   245
    fn test_delete_path_successful() {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   246
        let mut map = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   247
            inner: [("", 5), ("a", 3), ("a/b", 2), ("a/c", 1)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   248
                .iter()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   249
                .map(|(k, v)| (HgPathBuf::from_bytes(k.as_bytes()), *v))
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   250
                .collect(),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   251
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   252
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   253
        assert_eq!(Ok(()), map.delete_path(HgPath::new(b"a/b/")));
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   254
        eprintln!("{:?}", map);
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   255
        assert_eq!(Ok(()), map.delete_path(HgPath::new(b"a/b/")));
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   256
        eprintln!("{:?}", map);
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   257
        assert_eq!(
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   258
            Err(DirstateMapError::PathNotFound(HgPathBuf::from_bytes(
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   259
                b"a/b/"
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   260
            ))),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   261
            map.delete_path(HgPath::new(b"a/b/"))
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   262
        );
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   263
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   264
        assert_eq!(2, *map.inner.get(HgPath::new(b"a")).unwrap());
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   265
        assert_eq!(1, *map.inner.get(HgPath::new(b"a/c")).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   266
        eprintln!("{:?}", map);
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   267
        assert_eq!(Ok(()), map.delete_path(HgPath::new(b"a/")));
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   268
        eprintln!("{:?}", map);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   269
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   270
        assert_eq!(Ok(()), map.delete_path(HgPath::new(b"a/c/")));
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   271
        assert_eq!(
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   272
            Err(DirstateMapError::PathNotFound(HgPathBuf::from_bytes(
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   273
                b"a/c/"
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   274
            ))),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   275
            map.delete_path(HgPath::new(b"a/c/"))
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   276
        );
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   277
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   278
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   279
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   280
    fn test_add_path_empty_path() {
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
   281
        let manifest: Vec<HgPathBuf> = vec![];
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   282
        let mut map = DirsMultiset::from_manifest(&manifest).unwrap();
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   283
        let path = HgPath::new(b"");
43896
d8a96cebf75d rust-warnings: fix warnings in tests
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   284
        map.add_path(path).unwrap();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   285
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   286
        assert_eq!(1, map.len());
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   287
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   288
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   289
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   290
    fn test_add_path_successful() {
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
   291
        let manifest: Vec<HgPathBuf> = vec![];
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   292
        let mut map = DirsMultiset::from_manifest(&manifest).unwrap();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   293
43896
d8a96cebf75d rust-warnings: fix warnings in tests
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   294
        map.add_path(HgPath::new(b"a/")).unwrap();
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   295
        assert_eq!(1, *map.inner.get(HgPath::new(b"a")).unwrap());
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   296
        assert_eq!(1, *map.inner.get(HgPath::new(b"")).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   297
        assert_eq!(2, map.len());
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   298
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   299
        // Non directory should be ignored
43896
d8a96cebf75d rust-warnings: fix warnings in tests
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   300
        map.add_path(HgPath::new(b"a")).unwrap();
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   301
        assert_eq!(1, *map.inner.get(HgPath::new(b"a")).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   302
        assert_eq!(2, map.len());
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   303
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   304
        // Non directory will still add its base
43896
d8a96cebf75d rust-warnings: fix warnings in tests
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   305
        map.add_path(HgPath::new(b"a/b")).unwrap();
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   306
        assert_eq!(2, *map.inner.get(HgPath::new(b"a")).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   307
        assert_eq!(2, map.len());
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   308
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   309
        // Duplicate path works
43896
d8a96cebf75d rust-warnings: fix warnings in tests
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   310
        map.add_path(HgPath::new(b"a/")).unwrap();
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   311
        assert_eq!(3, *map.inner.get(HgPath::new(b"a")).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   312
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   313
        // Nested dir adds to its base
43896
d8a96cebf75d rust-warnings: fix warnings in tests
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   314
        map.add_path(HgPath::new(b"a/b/")).unwrap();
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   315
        assert_eq!(4, *map.inner.get(HgPath::new(b"a")).unwrap());
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   316
        assert_eq!(1, *map.inner.get(HgPath::new(b"a/b")).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   317
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   318
        // but not its base's base, because it already existed
43896
d8a96cebf75d rust-warnings: fix warnings in tests
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   319
        map.add_path(HgPath::new(b"a/b/c/")).unwrap();
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   320
        assert_eq!(4, *map.inner.get(HgPath::new(b"a")).unwrap());
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   321
        assert_eq!(2, *map.inner.get(HgPath::new(b"a/b")).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   322
43896
d8a96cebf75d rust-warnings: fix warnings in tests
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   323
        map.add_path(HgPath::new(b"a/c/")).unwrap();
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   324
        assert_eq!(1, *map.inner.get(HgPath::new(b"a/c")).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   325
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   326
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   327
            inner: [("", 2), ("a", 5), ("a/b", 2), ("a/b/c", 1), ("a/c", 1)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   328
                .iter()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   329
                .map(|(k, v)| (HgPathBuf::from_bytes(k.as_bytes()), *v))
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   330
                .collect(),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   331
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   332
        assert_eq!(map, expected);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   333
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   334
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   335
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   336
    fn test_dirsmultiset_new_empty() {
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
   337
        let manifest: Vec<HgPathBuf> = vec![];
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   338
        let new = DirsMultiset::from_manifest(&manifest).unwrap();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   339
        let expected = DirsMultiset {
43844
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 43808
diff changeset
   340
            inner: FastHashMap::default(),
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   341
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   342
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   343
47343
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   344
        let new = DirsMultiset::from_dirstate(
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   345
            StateMap::default().into_iter().map(Ok),
47972
e02f9af7aed1 pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47343
diff changeset
   346
            false,
47343
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   347
        )
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   348
        .unwrap();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   349
        let expected = DirsMultiset {
43844
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 43808
diff changeset
   350
            inner: FastHashMap::default(),
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   351
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   352
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   353
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   354
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   355
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   356
    fn test_dirsmultiset_new_no_skip() {
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
   357
        let input_vec: Vec<HgPathBuf> = ["a/", "b/", "a/c", "a/d/"]
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   358
            .iter()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   359
            .map(|e| HgPathBuf::from_bytes(e.as_bytes()))
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   360
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   361
        let expected_inner = [("", 2), ("a", 3), ("b", 1), ("a/d", 1)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   362
            .iter()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   363
            .map(|(k, v)| (HgPathBuf::from_bytes(k.as_bytes()), *v))
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   364
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   365
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   366
        let new = DirsMultiset::from_manifest(&input_vec).unwrap();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   367
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   368
            inner: expected_inner,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   369
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   370
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   371
47343
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   372
        let input_map = ["b/x", "a/c", "a/d/x"].iter().map(|f| {
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   373
            Ok((
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   374
                HgPathBuf::from_bytes(f.as_bytes()),
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   375
                DirstateEntry {
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   376
                    state: EntryState::Normal,
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   377
                    mode: 0,
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   378
                    mtime: 0,
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   379
                    size: 0,
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   380
                },
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   381
            ))
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   382
        });
45613
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 44998
diff changeset
   383
        let expected_inner = [("", 2), ("a", 2), ("b", 1), ("a/d", 1)]
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   384
            .iter()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   385
            .map(|(k, v)| (HgPathBuf::from_bytes(k.as_bytes()), *v))
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   386
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   387
47972
e02f9af7aed1 pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47343
diff changeset
   388
        let new = DirsMultiset::from_dirstate(input_map, false).unwrap();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   389
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   390
            inner: expected_inner,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   391
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   392
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   393
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   394
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   395
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   396
    fn test_dirsmultiset_new_skip() {
47343
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   397
        let input_map = [
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   398
            ("a/", EntryState::Normal),
45613
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 44998
diff changeset
   399
            ("a/b", EntryState::Normal),
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   400
            ("a/c", EntryState::Removed),
45613
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 44998
diff changeset
   401
            ("a/d", EntryState::Merged),
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   402
        ]
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   403
        .iter()
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   404
        .map(|(f, state)| {
47343
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   405
            Ok((
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   406
                HgPathBuf::from_bytes(f.as_bytes()),
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   407
                DirstateEntry {
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   408
                    state: *state,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   409
                    mode: 0,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   410
                    mtime: 0,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   411
                    size: 0,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   412
                },
47343
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   413
            ))
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47340
diff changeset
   414
        });
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   415
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   416
        // "a" incremented with "a/c" and "a/d/"
47972
e02f9af7aed1 pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47343
diff changeset
   417
        let expected_inner = [("", 1), ("a", 3)]
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   418
            .iter()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   419
            .map(|(k, v)| (HgPathBuf::from_bytes(k.as_bytes()), *v))
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   420
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   421
47972
e02f9af7aed1 pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47343
diff changeset
   422
        let new = DirsMultiset::from_dirstate(input_map, true).unwrap();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   423
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   424
            inner: expected_inner,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   425
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   426
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   427
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   428
}