rust/hg-core/src/dirstate/dirs_multiset.rs
author Simon Sapin <simon.sapin@octobus.net>
Wed, 19 May 2021 13:15:00 +0200
changeset 47340 4ee9f419c52e
parent 47138 cd8ca38fccff
child 47343 ed1583a845d2
permissions -rw-r--r--
rust: Return owned instead of borrowed DirstateEntry in DirstateMap APIs This will enable the tree-based DirstateMap to not always have an actual DirstateEntry in memory for all nodes, but construct it on demand. Differential Revision: https://phab.mercurial-scm.org/D10746
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.
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    11
use crate::{
44314
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
    12
    dirstate::EntryState,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
    13
    utils::{
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
    14
        files,
44329
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    15
        hg_path::{HgPath, HgPathBuf, HgPathError},
44314
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
    16
    },
47107
787ff5d21bcd dirstate-tree: Make Rust DirstateMap bindings go through a trait object
Simon Sapin <simon.sapin@octobus.net>
parents: 46890
diff changeset
    17
    DirstateEntry, DirstateMapError, FastHashMap,
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    18
};
44314
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
    19
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
    20
42891
a03a29462c0a rust-dirstate: specify concrete return type of DirsMultiset::iter()
Yuya Nishihara <yuya@tcha.org>
parents: 42851
diff changeset
    21
// 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
    22
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
    23
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    24
#[derive(PartialEq, Debug)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    25
pub struct DirsMultiset {
43844
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 43808
diff changeset
    26
    inner: FastHashMap<HgPathBuf, u32>,
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    27
}
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
impl DirsMultiset {
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    30
    /// Initializes the multiset from a dirstate.
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    31
    ///
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    32
    /// 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
    33
    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
    34
        dirstate: I,
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    35
        skip_state: Option<EntryState>,
47138
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47107
diff changeset
    36
    ) -> Result<Self, DirstateMapError>
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47107
diff changeset
    37
    where
47340
4ee9f419c52e rust: Return owned instead of borrowed DirstateEntry in DirstateMap APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47138
diff changeset
    38
        I: IntoIterator<Item = (P, DirstateEntry)>,
47138
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47107
diff changeset
    39
        P: AsRef<HgPath>,
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47107
diff changeset
    40
    {
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    41
        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
    42
            inner: FastHashMap::default(),
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    43
        };
47138
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47107
diff changeset
    44
        for (filename, entry) in dirstate {
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47107
diff changeset
    45
            let filename = filename.as_ref();
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    46
            // This `if` is optimized out of the loop
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    47
            if let Some(skip) = skip_state {
47138
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47107
diff changeset
    48
                if skip != entry.state {
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    49
                    multiset.add_path(filename)?;
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    50
                }
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    51
            } else {
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    52
                multiset.add_path(filename)?;
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
            }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    54
        }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    56
        Ok(multiset)
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
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    59
    /// 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
    60
    pub fn from_manifest(
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    61
        manifest: &[impl AsRef<HgPath>],
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    62
    ) -> Result<Self, DirstateMapError> {
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    63
        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
    64
            inner: FastHashMap::default(),
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    65
        };
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    66
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
    67
        for filename in manifest {
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    68
            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
    69
        }
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    70
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
    71
        Ok(multiset)
42818
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    72
    }
2e1f74cc3350 rust-dirstate: split DirsMultiset constructor per input type
Yuya Nishihara <yuya@tcha.org>
parents: 42766
diff changeset
    73
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    74
    /// 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
    75
    ///
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    76
    /// 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
    77
    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
    78
        &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
    79
        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
    80
    ) -> 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
    81
        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
    82
            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
    83
                // 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
    84
                // 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
    85
                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
    86
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    87
                return Err(DirstateMapError::InvalidPath(
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    88
                    HgPathError::ConsecutiveSlashes {
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    89
                        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
    90
                        second_slash_index,
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    91
                    },
934a79697c36 rust-dirs-multiset: improve temporary error message
Raphaël Gomès <rgomes@octobus.net>
parents: 44314
diff changeset
    92
                ));
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
    93
            }
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    94
            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
    95
                *val += 1;
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    96
                break;
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    97
            }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    98
            self.inner.insert(subpath.to_owned(), 1);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    99
        }
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
   100
        Ok(())
42543
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
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   103
    /// 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
   104
    ///
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   105
    /// 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
   106
    /// removed.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   107
    /// 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
   108
    pub fn delete_path(
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   109
        &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
   110
        path: impl AsRef<HgPath>,
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   111
    ) -> 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
   112
        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
   113
            match self.inner.entry(subpath.to_owned()) {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   114
                Entry::Occupied(mut entry) => {
44998
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44329
diff changeset
   115
                    let val = *entry.get();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   116
                    if val > 1 {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   117
                        entry.insert(val - 1);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   118
                        break;
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   119
                    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   120
                    entry.remove();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   121
                }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   122
                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
   123
                    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
   124
                        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
   125
                    ))
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   126
                }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   127
            };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   128
        }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   129
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   130
        Ok(())
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   131
    }
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   132
43849
088ba9d94079 rust-dirs-multiset: use `AsRef` instead of concrete types when possible
Raphaël Gomès <rgomes@octobus.net>
parents: 43844
diff changeset
   133
    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
   134
        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
   135
    }
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   136
42891
a03a29462c0a rust-dirstate: specify concrete return type of DirsMultiset::iter()
Yuya Nishihara <yuya@tcha.org>
parents: 42851
diff changeset
   137
    pub fn iter(&self) -> DirsMultisetIter {
42766
849e744b925d rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 42765
diff changeset
   138
        self.inner.keys()
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
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   141
    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
   142
        self.inner.len()
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   143
    }
44998
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44329
diff changeset
   144
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44329
diff changeset
   145
    pub fn is_empty(&self) -> bool {
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44329
diff changeset
   146
        self.len() == 0
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44329
diff changeset
   147
    }
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   148
}
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   149
44314
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   150
/// 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
   151
/// 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
   152
/// 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
   153
#[derive(PartialEq, Debug)]
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   154
pub struct DirsChildrenMultiset<'a> {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   155
    inner: FastHashMap<&'a HgPath, HashSet<&'a HgPath>>,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   156
    only_include: Option<HashSet<&'a HgPath>>,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   157
}
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   158
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   159
impl<'a> DirsChildrenMultiset<'a> {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   160
    pub fn new(
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   161
        paths: impl Iterator<Item = &'a HgPathBuf>,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   162
        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
   163
    ) -> Self {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   164
        let mut new = Self {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   165
            inner: HashMap::default(),
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   166
            only_include: only_include
44998
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44329
diff changeset
   167
                .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
   168
        };
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   169
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   170
        for path in paths {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   171
            new.add_path(path)
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
        new
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   175
    }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   176
    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
   177
        if path.as_ref().is_empty() {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   178
            return;
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
        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
   181
        {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   182
            if !self.is_dir_included(directory) {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   183
                continue;
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   184
            }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   185
            self.inner
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   186
                .entry(directory)
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   187
                .and_modify(|e| {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   188
                    e.insert(basename);
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   189
                })
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   190
                .or_insert_with(|| {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   191
                    let mut set = HashSet::new();
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   192
                    set.insert(basename);
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   193
                    set
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   194
                });
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   195
        }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   196
    }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   197
    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
   198
        match &self.only_include {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   199
            None => false,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   200
            Some(i) => i.contains(dir.as_ref()),
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   201
        }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   202
    }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   203
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   204
    pub fn get(
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   205
        &self,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   206
        path: impl AsRef<HgPath>,
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   207
    ) -> Option<&HashSet<&'a HgPath>> {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   208
        self.inner.get(path.as_ref())
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   209
    }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   210
}
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 43896
diff changeset
   211
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   212
#[cfg(test)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   213
mod tests {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   214
    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
   215
    use crate::StateMap;
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   216
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   217
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   218
    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
   219
        let manifest: Vec<HgPathBuf> = vec![];
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   220
        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
   221
        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
   222
        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
   223
            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
   224
            map.delete_path(&path)
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   225
        );
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   226
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   227
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   228
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   229
    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
   230
        let mut map =
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   231
            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
   232
        let path = HgPath::new(b"");
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   233
        assert_eq!(Ok(()), map.delete_path(path));
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   234
        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
   235
            Err(DirstateMapError::PathNotFound(path.to_owned())),
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   236
            map.delete_path(path)
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   237
        );
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   238
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   239
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   240
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   241
    fn test_delete_path_successful() {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   242
        let mut map = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   243
            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
   244
                .iter()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   245
                .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
   246
                .collect(),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   247
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   248
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   249
        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
   250
        eprintln!("{:?}", map);
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   251
        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
   252
        eprintln!("{:?}", map);
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   253
        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
   254
            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
   255
                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
            ))),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   257
            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
   258
        );
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   259
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   260
        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
   261
        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
   262
        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
   263
        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
   264
        eprintln!("{:?}", map);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   265
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   266
        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
   267
        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
   268
            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
   269
                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
   270
            ))),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   271
            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
   272
        );
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   273
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   274
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   275
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   276
    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
   277
        let manifest: Vec<HgPathBuf> = vec![];
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   278
        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
   279
        let path = HgPath::new(b"");
43896
d8a96cebf75d rust-warnings: fix warnings in tests
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   280
        map.add_path(path).unwrap();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   281
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   282
        assert_eq!(1, map.len());
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   283
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   284
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   285
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   286
    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
   287
        let manifest: Vec<HgPathBuf> = vec![];
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   288
        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
   289
43896
d8a96cebf75d rust-warnings: fix warnings in tests
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   290
        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
   291
        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
   292
        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
   293
        assert_eq!(2, map.len());
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   294
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   295
        // Non directory should be ignored
43896
d8a96cebf75d rust-warnings: fix warnings in tests
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   296
        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
   297
        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
   298
        assert_eq!(2, map.len());
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   299
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   300
        // 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
   301
        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
   302
        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
   303
        assert_eq!(2, map.len());
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   304
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   305
        // Duplicate path works
43896
d8a96cebf75d rust-warnings: fix warnings in tests
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   306
        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
   307
        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
   308
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   309
        // 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
   310
        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
   311
        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
   312
        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
   313
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   314
        // 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
   315
        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
   316
        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
   317
        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
   318
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/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!(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
   321
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   322
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   323
            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
   324
                .iter()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   325
                .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
   326
                .collect(),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   327
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   328
        assert_eq!(map, expected);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   329
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   330
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   331
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   332
    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
   333
        let manifest: Vec<HgPathBuf> = vec![];
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   334
        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
   335
        let expected = DirsMultiset {
43844
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 43808
diff changeset
   336
            inner: FastHashMap::default(),
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   337
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   338
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   339
45613
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 44998
diff changeset
   340
        let new =
47340
4ee9f419c52e rust: Return owned instead of borrowed DirstateEntry in DirstateMap APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47138
diff changeset
   341
            DirsMultiset::from_dirstate(StateMap::default(), None).unwrap();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   342
        let expected = DirsMultiset {
43844
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 43808
diff changeset
   343
            inner: FastHashMap::default(),
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   344
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   345
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   346
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   347
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   348
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   349
    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
   350
        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
   351
            .iter()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   352
            .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
   353
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   354
        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
   355
            .iter()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   356
            .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
   357
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   358
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   359
        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
   360
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   361
            inner: expected_inner,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   362
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   363
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   364
47107
787ff5d21bcd dirstate-tree: Make Rust DirstateMap bindings go through a trait object
Simon Sapin <simon.sapin@octobus.net>
parents: 46890
diff changeset
   365
        let input_map: HashMap<_, _> = ["b/x", "a/c", "a/d/x"]
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   366
            .iter()
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   367
            .map(|f| {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   368
                (
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   369
                    HgPathBuf::from_bytes(f.as_bytes()),
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   370
                    DirstateEntry {
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   371
                        state: EntryState::Normal,
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   372
                        mode: 0,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   373
                        mtime: 0,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   374
                        size: 0,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   375
                    },
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   376
                )
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   377
            })
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   378
            .collect();
45613
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 44998
diff changeset
   379
        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
   380
            .iter()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   381
            .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
   382
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   383
47340
4ee9f419c52e rust: Return owned instead of borrowed DirstateEntry in DirstateMap APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47138
diff changeset
   384
        let new = DirsMultiset::from_dirstate(input_map, None).unwrap();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   385
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   386
            inner: expected_inner,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   387
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   388
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   389
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   390
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   391
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   392
    fn test_dirsmultiset_new_skip() {
47107
787ff5d21bcd dirstate-tree: Make Rust DirstateMap bindings go through a trait object
Simon Sapin <simon.sapin@octobus.net>
parents: 46890
diff changeset
   393
        let input_map: HashMap<_, _> = [
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   394
            ("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
   395
            ("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
   396
            ("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
   397
            ("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
   398
        ]
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   399
        .iter()
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   400
        .map(|(f, state)| {
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   401
            (
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   402
                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
   403
                DirstateEntry {
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   404
                    state: *state,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   405
                    mode: 0,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   406
                    mtime: 0,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   407
                    size: 0,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   408
                },
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   409
            )
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   410
        })
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   411
        .collect();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   412
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   413
        // "a" incremented with "a/c" and "a/d/"
45613
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 44998
diff changeset
   414
        let expected_inner = [("", 1), ("a", 2)]
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   415
            .iter()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42891
diff changeset
   416
            .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
   417
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   418
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   419
        let new =
47340
4ee9f419c52e rust: Return owned instead of borrowed DirstateEntry in DirstateMap APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47138
diff changeset
   420
            DirsMultiset::from_dirstate(input_map, Some(EntryState::Normal))
43874
bc7d8f45c3b6 rust-dirs: handle forgotten `Result`s
Raphaël Gomès <rgomes@octobus.net>
parents: 43849
diff changeset
   421
                .unwrap();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   422
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   423
            inner: expected_inner,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   424
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   425
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   426
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   427
}