rust/hg-core/src/matchers.rs
author Raphaël Gomès <rgomes@octobus.net>
Wed, 06 May 2020 14:33:46 +0200
changeset 44803 de0fb4463a3d
parent 44802 e0414fcd35e0
child 44832 ad1ec40975aa
permissions -rw-r--r--
rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher` This is purely for future reference, I don't think this is a problem right now, since the `Display` is *only* used to ease debugging and has no real users. Differential Revision: https://phab.mercurial-scm.org/D8492
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
// matchers.rs
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
//
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
//
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
// This software may be used and distributed according to the terms of the
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
// GNU General Public License version 2 or any later version.
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
//! Structs and types for matching files and directories.
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     9
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
    10
#[cfg(feature = "with-re2")]
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
    11
use crate::re2::Re2;
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
    12
use crate::{
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    13
    dirstate::dirs_multiset::DirsChildrenMultiset,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    14
    filepatterns::{
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    15
        build_single_regex, filter_subincludes, get_patterns_from_file,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    16
        PatternFileWarning, PatternResult, SubInclude,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    17
    },
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    18
    utils::{
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    19
        files::find_dirs,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    20
        hg_path::{HgPath, HgPathBuf},
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    21
        Escaped,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    22
    },
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    23
    DirsMultiset, DirstateMapError, FastHashMap, IgnorePattern, PatternError,
44520
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
    24
    PatternSyntax,
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
    25
};
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    26
44802
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
    27
use crate::filepatterns::normalize_path_bytes;
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
    28
use std::borrow::ToOwned;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    29
use std::collections::HashSet;
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
    30
use std::fmt::{Display, Error, Formatter};
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
    31
use std::iter::FromIterator;
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
    32
use std::ops::Deref;
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
    33
use std::path::{Path, PathBuf};
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    34
44784
83c97c0bd319 rust-matchers: add timing tracing to regex compilation
Raphaël Gomès <rgomes@octobus.net>
parents: 44597
diff changeset
    35
use micro_timer::timed;
83c97c0bd319 rust-matchers: add timing tracing to regex compilation
Raphaël Gomès <rgomes@octobus.net>
parents: 44597
diff changeset
    36
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
    37
#[derive(Debug, PartialEq)]
43832
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Raphaël Gomès <rgomes@octobus.net>
parents: 43611
diff changeset
    38
pub enum VisitChildrenSet<'a> {
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    39
    /// Don't visit anything
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    40
    Empty,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    41
    /// Only visit this directory
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    42
    This,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    43
    /// Visit this directory and these subdirectories
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    44
    /// TODO Should we implement a `NonEmptyHashSet`?
43832
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Raphaël Gomès <rgomes@octobus.net>
parents: 43611
diff changeset
    45
    Set(HashSet<&'a HgPath>),
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    46
    /// Visit this directory and all subdirectories
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    47
    Recursive,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    48
}
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    49
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    50
pub trait Matcher {
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    51
    /// Explicitly listed files
43832
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Raphaël Gomès <rgomes@octobus.net>
parents: 43611
diff changeset
    52
    fn file_set(&self) -> Option<&HashSet<&HgPath>>;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
    /// Returns whether `filename` is in `file_set`
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
    54
    fn exact_match(&self, filename: impl AsRef<HgPath>) -> bool;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
    /// Returns whether `filename` is matched by this matcher
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
    56
    fn matches(&self, filename: impl AsRef<HgPath>) -> bool;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
    /// Decides whether a directory should be visited based on whether it
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    58
    /// has potential matches in it or one of its subdirectories, and
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    59
    /// potentially lists which subdirectories of that directory should be
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
    /// visited. This is based on the match's primary, included, and excluded
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    61
    /// patterns.
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    62
    ///
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    63
    /// # Example
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    64
    ///
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    65
    /// Assume matchers `['path:foo/bar', 'rootfilesin:qux']`, we would
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    66
    /// return the following values (assuming the implementation of
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    67
    /// visit_children_set is capable of recognizing this; some implementations
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    68
    /// are not).
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    69
    ///
44006
72bced4f2936 rust-matchers: fixing cargo doc
Georges Racinet <georges.racinet@octobus.net>
parents: 43914
diff changeset
    70
    /// ```text
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    71
    /// ```ignore
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    72
    /// '' -> {'foo', 'qux'}
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    73
    /// 'baz' -> set()
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    74
    /// 'foo' -> {'bar'}
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    75
    /// // Ideally this would be `Recursive`, but since the prefix nature of
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    76
    /// // matchers is applied to the entire matcher, we have to downgrade this
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    77
    /// // to `This` due to the (yet to be implemented in Rust) non-prefix
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    78
    /// // `RootFilesIn'-kind matcher being mixed in.
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    79
    /// 'foo/bar' -> 'this'
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    80
    /// 'qux' -> 'this'
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    81
    /// ```
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    82
    /// # Important
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    83
    ///
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    84
    /// Most matchers do not know if they're representing files or
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    85
    /// directories. They see `['path:dir/f']` and don't know whether `f` is a
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    86
    /// file or a directory, so `visit_children_set('dir')` for most matchers
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    87
    /// will return `HashSet{ HgPath { "f" } }`, but if the matcher knows it's
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    88
    /// a file (like the yet to be implemented in Rust `ExactMatcher` does),
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    89
    /// it may return `VisitChildrenSet::This`.
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    90
    /// Do not rely on the return being a `HashSet` indicating that there are
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    91
    /// no files in this dir to investigate (or equivalently that if there are
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    92
    /// files to investigate in 'dir' that it will always return
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    93
    /// `VisitChildrenSet::This`).
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    94
    fn visit_children_set(
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    95
        &self,
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
    96
        directory: impl AsRef<HgPath>,
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
    97
    ) -> VisitChildrenSet;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    98
    /// Matcher will match everything and `files_set()` will be empty:
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    99
    /// optimization might be possible.
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   100
    fn matches_everything(&self) -> bool;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   101
    /// Matcher will match exactly the files in `files_set()`: optimization
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   102
    /// might be possible.
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   103
    fn is_exact(&self) -> bool;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   104
}
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   105
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   106
/// Matches everything.
43834
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 43832
diff changeset
   107
///```
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 43832
diff changeset
   108
/// use hg::{ matchers::{Matcher, AlwaysMatcher}, utils::hg_path::HgPath };
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 43832
diff changeset
   109
///
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 43832
diff changeset
   110
/// let matcher = AlwaysMatcher;
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 43832
diff changeset
   111
///
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   112
/// assert_eq!(matcher.matches(HgPath::new(b"whatever")), true);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   113
/// assert_eq!(matcher.matches(HgPath::new(b"b.txt")), true);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   114
/// assert_eq!(matcher.matches(HgPath::new(b"main.c")), true);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   115
/// assert_eq!(matcher.matches(HgPath::new(br"re:.*\.c$")), true);
43834
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 43832
diff changeset
   116
/// ```
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   117
#[derive(Debug)]
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   118
pub struct AlwaysMatcher;
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   119
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   120
impl Matcher for AlwaysMatcher {
43832
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Raphaël Gomès <rgomes@octobus.net>
parents: 43611
diff changeset
   121
    fn file_set(&self) -> Option<&HashSet<&HgPath>> {
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Raphaël Gomès <rgomes@octobus.net>
parents: 43611
diff changeset
   122
        None
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   123
    }
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   124
    fn exact_match(&self, _filename: impl AsRef<HgPath>) -> bool {
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   125
        false
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   126
    }
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   127
    fn matches(&self, _filename: impl AsRef<HgPath>) -> bool {
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   128
        true
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   129
    }
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   130
    fn visit_children_set(
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   131
        &self,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   132
        _directory: impl AsRef<HgPath>,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   133
    ) -> VisitChildrenSet {
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   134
        VisitChildrenSet::Recursive
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   135
    }
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   136
    fn matches_everything(&self) -> bool {
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   137
        true
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   138
    }
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   139
    fn is_exact(&self) -> bool {
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   140
        false
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43438
diff changeset
   141
    }
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   142
}
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   143
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   144
/// Matches the input files exactly. They are interpreted as paths, not
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   145
/// patterns.
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   146
///
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   147
///```
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   148
/// use hg::{ matchers::{Matcher, FileMatcher}, utils::hg_path::HgPath };
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   149
///
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   150
/// let files = [HgPath::new(b"a.txt"), HgPath::new(br"re:.*\.c$")];
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   151
/// let matcher = FileMatcher::new(&files).unwrap();
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   152
///
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   153
/// assert_eq!(matcher.matches(HgPath::new(b"a.txt")), true);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   154
/// assert_eq!(matcher.matches(HgPath::new(b"b.txt")), false);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   155
/// assert_eq!(matcher.matches(HgPath::new(b"main.c")), false);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   156
/// assert_eq!(matcher.matches(HgPath::new(br"re:.*\.c$")), true);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   157
/// ```
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   158
#[derive(Debug)]
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   159
pub struct FileMatcher<'a> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   160
    files: HashSet<&'a HgPath>,
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   161
    dirs: DirsMultiset,
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   162
}
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   163
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   164
impl<'a> FileMatcher<'a> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   165
    pub fn new(
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   166
        files: &'a [impl AsRef<HgPath>],
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   167
    ) -> Result<Self, DirstateMapError> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   168
        Ok(Self {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   169
            files: HashSet::from_iter(files.iter().map(|f| f.as_ref())),
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   170
            dirs: DirsMultiset::from_manifest(files)?,
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   171
        })
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   172
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   173
    fn inner_matches(&self, filename: impl AsRef<HgPath>) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   174
        self.files.contains(filename.as_ref())
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   175
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   176
}
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   177
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   178
impl<'a> Matcher for FileMatcher<'a> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   179
    fn file_set(&self) -> Option<&HashSet<&HgPath>> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   180
        Some(&self.files)
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   181
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   182
    fn exact_match(&self, filename: impl AsRef<HgPath>) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   183
        self.inner_matches(filename)
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   184
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   185
    fn matches(&self, filename: impl AsRef<HgPath>) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   186
        self.inner_matches(filename)
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   187
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   188
    fn visit_children_set(
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   189
        &self,
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   190
        directory: impl AsRef<HgPath>,
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   191
    ) -> VisitChildrenSet {
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   192
        if self.files.is_empty() || !self.dirs.contains(&directory) {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   193
            return VisitChildrenSet::Empty;
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   194
        }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   195
        let dirs_as_set = self.dirs.iter().map(|k| k.deref()).collect();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   196
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   197
        let mut candidates: HashSet<&HgPath> =
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   198
            self.files.union(&dirs_as_set).map(|k| *k).collect();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   199
        candidates.remove(HgPath::new(b""));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   200
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   201
        if !directory.as_ref().is_empty() {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   202
            let directory = [directory.as_ref().as_bytes(), b"/"].concat();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   203
            candidates = candidates
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   204
                .iter()
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   205
                .filter_map(|c| {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   206
                    if c.as_bytes().starts_with(&directory) {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   207
                        Some(HgPath::new(&c.as_bytes()[directory.len()..]))
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   208
                    } else {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   209
                        None
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   210
                    }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   211
                })
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   212
                .collect();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   213
        }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   214
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   215
        // `self.dirs` includes all of the directories, recursively, so if
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   216
        // we're attempting to match 'foo/bar/baz.txt', it'll have '', 'foo',
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   217
        // 'foo/bar' in it. Thus we can safely ignore a candidate that has a
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   218
        // '/' in it, indicating it's for a subdir-of-a-subdir; the immediate
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   219
        // subdir will be in there without a slash.
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   220
        VisitChildrenSet::Set(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   221
            candidates
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   222
                .iter()
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   223
                .filter_map(|c| {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   224
                    if c.bytes().all(|b| *b != b'/') {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   225
                        Some(*c)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   226
                    } else {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   227
                        None
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   228
                    }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   229
                })
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   230
                .collect(),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   231
        )
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   232
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   233
    fn matches_everything(&self) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   234
        false
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   235
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   236
    fn is_exact(&self) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   237
        true
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   238
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43863
diff changeset
   239
}
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   240
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   241
/// Matches files that are included in the ignore rules.
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   242
#[cfg_attr(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   243
    feature = "with-re2",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   244
    doc = r##"
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   245
```
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   246
use hg::{
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   247
    matchers::{IncludeMatcher, Matcher},
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   248
    IgnorePattern,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   249
    PatternSyntax,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   250
    utils::hg_path::HgPath
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   251
};
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   252
use std::path::Path;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   253
///
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   254
let ignore_patterns =
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   255
vec![IgnorePattern::new(PatternSyntax::RootGlob, b"this*", Path::new(""))];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   256
let (matcher, _) = IncludeMatcher::new(ignore_patterns, "").unwrap();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   257
///
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   258
assert_eq!(matcher.matches(HgPath::new(b"testing")), false);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   259
assert_eq!(matcher.matches(HgPath::new(b"this should work")), true);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   260
assert_eq!(matcher.matches(HgPath::new(b"this also")), true);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   261
assert_eq!(matcher.matches(HgPath::new(b"but not this")), false);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   262
```
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   263
"##
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   264
)]
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   265
pub struct IncludeMatcher<'a> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   266
    patterns: Vec<u8>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   267
    match_fn: Box<dyn for<'r> Fn(&'r HgPath) -> bool + 'a + Sync>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   268
    /// Whether all the patterns match a prefix (i.e. recursively)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   269
    prefix: bool,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   270
    roots: HashSet<HgPathBuf>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   271
    dirs: HashSet<HgPathBuf>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   272
    parents: HashSet<HgPathBuf>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   273
}
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   274
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   275
impl<'a> Matcher for IncludeMatcher<'a> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   276
    fn file_set(&self) -> Option<&HashSet<&HgPath>> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   277
        None
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   278
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   279
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   280
    fn exact_match(&self, _filename: impl AsRef<HgPath>) -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   281
        false
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   282
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   283
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   284
    fn matches(&self, filename: impl AsRef<HgPath>) -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   285
        (self.match_fn)(filename.as_ref())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   286
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   287
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   288
    fn visit_children_set(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   289
        &self,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   290
        directory: impl AsRef<HgPath>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   291
    ) -> VisitChildrenSet {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   292
        let dir = directory.as_ref();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   293
        if self.prefix && self.roots.contains(dir) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   294
            return VisitChildrenSet::Recursive;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   295
        }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   296
        if self.roots.contains(HgPath::new(b""))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   297
            || self.roots.contains(dir)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   298
            || self.dirs.contains(dir)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   299
            || find_dirs(dir).any(|parent_dir| self.roots.contains(parent_dir))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   300
        {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   301
            return VisitChildrenSet::This;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   302
        }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   303
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   304
        if self.parents.contains(directory.as_ref()) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   305
            let multiset = self.get_all_parents_children();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   306
            if let Some(children) = multiset.get(dir) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   307
                return VisitChildrenSet::Set(children.to_owned());
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   308
            }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   309
        }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   310
        VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   311
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   312
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   313
    fn matches_everything(&self) -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   314
        false
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   315
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   316
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   317
    fn is_exact(&self) -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   318
        false
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   319
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   320
}
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   321
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   322
#[cfg(feature = "with-re2")]
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   323
/// Returns a function that matches an `HgPath` against the given regex
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   324
/// pattern.
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   325
///
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   326
/// This can fail when the pattern is invalid or not supported by the
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   327
/// underlying engine `Re2`, for instance anything with back-references.
44784
83c97c0bd319 rust-matchers: add timing tracing to regex compilation
Raphaël Gomès <rgomes@octobus.net>
parents: 44597
diff changeset
   328
#[timed]
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   329
fn re_matcher(
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   330
    pattern: &[u8],
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   331
) -> PatternResult<impl Fn(&HgPath) -> bool + Sync> {
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   332
    let regex = Re2::new(pattern);
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   333
    let regex = regex.map_err(|e| PatternError::UnsupportedSyntax(e))?;
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   334
    Ok(move |path: &HgPath| regex.is_match(path.as_bytes()))
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   335
}
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   336
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   337
#[cfg(not(feature = "with-re2"))]
44593
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   338
/// Returns a function that matches an `HgPath` against the given regex
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   339
/// pattern.
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   340
///
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   341
/// This can fail when the pattern is invalid or not supported by the
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   342
/// underlying engine (the `regex` crate), for instance anything with
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   343
/// back-references.
44784
83c97c0bd319 rust-matchers: add timing tracing to regex compilation
Raphaël Gomès <rgomes@octobus.net>
parents: 44597
diff changeset
   344
#[timed]
44593
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   345
fn re_matcher(
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   346
    pattern: &[u8],
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   347
) -> PatternResult<impl Fn(&HgPath) -> bool + Sync> {
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   348
    use std::io::Write;
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   349
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   350
    let mut escaped_bytes = vec![];
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   351
    for byte in pattern {
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   352
        if *byte > 127 {
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   353
            write!(escaped_bytes, "\\x{:x}", *byte).unwrap();
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   354
        } else {
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   355
            escaped_bytes.push(*byte);
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   356
        }
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   357
    }
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   358
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   359
    // Avoid the cost of UTF8 checking
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   360
    //
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   361
    // # Safety
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   362
    // This is safe because we escaped all non-ASCII bytes.
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   363
    let pattern_string = unsafe { String::from_utf8_unchecked(escaped_bytes) };
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   364
    let re = regex::bytes::RegexBuilder::new(&pattern_string)
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   365
        .unicode(false)
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   366
        .build()
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   367
        .map_err(|e| PatternError::UnsupportedSyntax(e.to_string()))?;
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   368
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   369
    Ok(move |path: &HgPath| re.is_match(path.as_bytes()))
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   370
}
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44353
diff changeset
   371
44521
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44520
diff changeset
   372
/// Returns the regex pattern and a function that matches an `HgPath` against
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44520
diff changeset
   373
/// said regex formed by the given ignore patterns.
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44520
diff changeset
   374
fn build_regex_match<'a>(
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44520
diff changeset
   375
    ignore_patterns: &'a [&'a IgnorePattern],
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44520
diff changeset
   376
) -> PatternResult<(Vec<u8>, Box<dyn Fn(&HgPath) -> bool + Sync>)> {
44802
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   377
    let mut regexps = vec![];
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   378
    let mut exact_set = HashSet::new();
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   379
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   380
    for pattern in ignore_patterns {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   381
        if let Some(re) = build_single_regex(pattern)? {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   382
            regexps.push(re);
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   383
        } else {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   384
            let exact = normalize_path_bytes(&pattern.pattern);
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   385
            exact_set.insert(HgPathBuf::from_bytes(&exact));
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   386
        }
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   387
    }
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   388
44521
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44520
diff changeset
   389
    let full_regex = regexps.join(&b'|');
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44520
diff changeset
   390
44802
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   391
    // An empty pattern would cause the regex engine to incorrectly match the
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   392
    // (empty) root directory
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   393
    let func = if !(regexps.is_empty()) {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   394
        let matcher = re_matcher(&full_regex)?;
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   395
        let func = move |filename: &HgPath| {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   396
            exact_set.contains(filename) || matcher(filename)
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   397
        };
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   398
        Box::new(func) as Box<dyn Fn(&HgPath) -> bool + Sync>
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   399
    } else {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   400
        let func = move |filename: &HgPath| exact_set.contains(filename);
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   401
        Box::new(func) as Box<dyn Fn(&HgPath) -> bool + Sync>
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44784
diff changeset
   402
    };
44521
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44520
diff changeset
   403
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44520
diff changeset
   404
    Ok((full_regex, func))
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44520
diff changeset
   405
}
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44520
diff changeset
   406
44520
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   407
/// Returns roots and directories corresponding to each pattern.
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   408
///
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   409
/// This calculates the roots and directories exactly matching the patterns and
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   410
/// returns a tuple of (roots, dirs). It does not return other directories
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   411
/// which may also need to be considered, like the parent directories.
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   412
fn roots_and_dirs(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   413
    ignore_patterns: &[IgnorePattern],
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   414
) -> (Vec<HgPathBuf>, Vec<HgPathBuf>) {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   415
    let mut roots = Vec::new();
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   416
    let mut dirs = Vec::new();
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   417
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   418
    for ignore_pattern in ignore_patterns {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   419
        let IgnorePattern {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   420
            syntax, pattern, ..
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   421
        } = ignore_pattern;
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   422
        match syntax {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   423
            PatternSyntax::RootGlob | PatternSyntax::Glob => {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   424
                let mut root = vec![];
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   425
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   426
                for p in pattern.split(|c| *c == b'/') {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   427
                    if p.iter().any(|c| match *c {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   428
                        b'[' | b'{' | b'*' | b'?' => true,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   429
                        _ => false,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   430
                    }) {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   431
                        break;
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   432
                    }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   433
                    root.push(HgPathBuf::from_bytes(p));
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   434
                }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   435
                let buf =
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   436
                    root.iter().fold(HgPathBuf::new(), |acc, r| acc.join(r));
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   437
                roots.push(buf);
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   438
            }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   439
            PatternSyntax::Path | PatternSyntax::RelPath => {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   440
                let pat = HgPath::new(if pattern == b"." {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   441
                    &[] as &[u8]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   442
                } else {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   443
                    pattern
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   444
                });
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   445
                roots.push(pat.to_owned());
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   446
            }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   447
            PatternSyntax::RootFiles => {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   448
                let pat = if pattern == b"." {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   449
                    &[] as &[u8]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   450
                } else {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   451
                    pattern
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   452
                };
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   453
                dirs.push(HgPathBuf::from_bytes(pat));
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   454
            }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   455
            _ => {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   456
                roots.push(HgPathBuf::new());
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   457
            }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   458
        }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   459
    }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   460
    (roots, dirs)
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   461
}
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   462
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   463
/// Paths extracted from patterns
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   464
#[derive(Debug, PartialEq)]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   465
struct RootsDirsAndParents {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   466
    /// Directories to match recursively
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   467
    pub roots: HashSet<HgPathBuf>,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   468
    /// Directories to match non-recursively
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   469
    pub dirs: HashSet<HgPathBuf>,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   470
    /// Implicitly required directories to go to items in either roots or dirs
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   471
    pub parents: HashSet<HgPathBuf>,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   472
}
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   473
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   474
/// Extract roots, dirs and parents from patterns.
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   475
fn roots_dirs_and_parents(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   476
    ignore_patterns: &[IgnorePattern],
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   477
) -> PatternResult<RootsDirsAndParents> {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   478
    let (roots, dirs) = roots_and_dirs(ignore_patterns);
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   479
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   480
    let mut parents = HashSet::new();
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   481
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   482
    parents.extend(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   483
        DirsMultiset::from_manifest(&dirs)
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   484
            .map_err(|e| match e {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   485
                DirstateMapError::InvalidPath(e) => e,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   486
                _ => unreachable!(),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   487
            })?
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   488
            .iter()
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   489
            .map(|k| k.to_owned()),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   490
    );
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   491
    parents.extend(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   492
        DirsMultiset::from_manifest(&roots)
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   493
            .map_err(|e| match e {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   494
                DirstateMapError::InvalidPath(e) => e,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   495
                _ => unreachable!(),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   496
            })?
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   497
            .iter()
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   498
            .map(|k| k.to_owned()),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   499
    );
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   500
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   501
    Ok(RootsDirsAndParents {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   502
        roots: HashSet::from_iter(roots),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   503
        dirs: HashSet::from_iter(dirs),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   504
        parents,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   505
    })
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   506
}
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   507
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   508
/// Returns a function that checks whether a given file (in the general sense)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   509
/// should be matched.
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   510
fn build_match<'a, 'b>(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   511
    ignore_patterns: &'a [IgnorePattern],
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   512
    root_dir: impl AsRef<Path>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   513
) -> PatternResult<(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   514
    Vec<u8>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   515
    Box<dyn Fn(&HgPath) -> bool + 'b + Sync>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   516
    Vec<PatternFileWarning>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   517
)> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   518
    let mut match_funcs: Vec<Box<dyn Fn(&HgPath) -> bool + Sync>> = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   519
    // For debugging and printing
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   520
    let mut patterns = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   521
    let mut all_warnings = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   523
    let (subincludes, ignore_patterns) =
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   524
        filter_subincludes(ignore_patterns, root_dir)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   525
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   526
    if !subincludes.is_empty() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   527
        // Build prefix-based matcher functions for subincludes
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   528
        let mut submatchers = FastHashMap::default();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   529
        let mut prefixes = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   530
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   531
        for SubInclude { prefix, root, path } in subincludes.into_iter() {
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
   532
            let (match_fn, warnings) =
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
   533
                get_ignore_function(vec![path.to_path_buf()], root)?;
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   534
            all_warnings.extend(warnings);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   535
            prefixes.push(prefix.to_owned());
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   536
            submatchers.insert(prefix.to_owned(), match_fn);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   537
        }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   538
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   539
        let match_subinclude = move |filename: &HgPath| {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   540
            for prefix in prefixes.iter() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   541
                if let Some(rel) = filename.relative_to(prefix) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   542
                    if (submatchers.get(prefix).unwrap())(rel) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   543
                        return true;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   544
                    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   545
                }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   546
            }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   547
            false
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   548
        };
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   549
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   550
        match_funcs.push(Box::new(match_subinclude));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   551
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   552
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   553
    if !ignore_patterns.is_empty() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   554
        // Either do dumb matching if all patterns are rootfiles, or match
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   555
        // with a regex.
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   556
        if ignore_patterns
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   557
            .iter()
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   558
            .all(|k| k.syntax == PatternSyntax::RootFiles)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   559
        {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   560
            let dirs: HashSet<_> = ignore_patterns
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   561
                .iter()
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   562
                .map(|k| k.pattern.to_owned())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   563
                .collect();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   564
            let mut dirs_vec: Vec<_> = dirs.iter().cloned().collect();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   565
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   566
            let match_func = move |path: &HgPath| -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   567
                let path = path.as_bytes();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   568
                let i = path.iter().rfind(|a| **a == b'/');
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   569
                let dir = if let Some(i) = i {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   570
                    &path[..*i as usize]
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   571
                } else {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   572
                    b"."
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   573
                };
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   574
                dirs.contains(dir.deref())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   575
            };
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   576
            match_funcs.push(Box::new(match_func));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   577
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   578
            patterns.extend(b"rootfilesin: ");
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   579
            dirs_vec.sort();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   580
            patterns.extend(dirs_vec.escaped_bytes());
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   581
        } else {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   582
            let (new_re, match_func) = build_regex_match(&ignore_patterns)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   583
            patterns = new_re;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   584
            match_funcs.push(match_func)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   585
        }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   586
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   587
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   588
    Ok(if match_funcs.len() == 1 {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   589
        (patterns, match_funcs.remove(0), all_warnings)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   590
    } else {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   591
        (
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   592
            patterns,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   593
            Box::new(move |f: &HgPath| -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   594
                match_funcs.iter().any(|match_func| match_func(f))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   595
            }),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   596
            all_warnings,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   597
        )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   598
    })
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   599
}
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   600
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   601
/// Parses all "ignore" files with their recursive includes and returns a
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   602
/// function that checks whether a given file (in the general sense) should be
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   603
/// ignored.
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   604
pub fn get_ignore_function<'a>(
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
   605
    all_pattern_files: Vec<PathBuf>,
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   606
    root_dir: impl AsRef<Path>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   607
) -> PatternResult<(
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
   608
    Box<dyn for<'r> Fn(&'r HgPath) -> bool + Sync + 'a>,
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   609
    Vec<PatternFileWarning>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   610
)> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   611
    let mut all_patterns = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   612
    let mut all_warnings = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   613
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   614
    for pattern_file in all_pattern_files.into_iter() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   615
        let (patterns, warnings) =
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   616
            get_patterns_from_file(pattern_file, &root_dir)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   617
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
   618
        all_patterns.extend(patterns.to_owned());
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   619
        all_warnings.extend(warnings);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   620
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   621
    let (matcher, warnings) = IncludeMatcher::new(all_patterns, root_dir)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   622
    all_warnings.extend(warnings);
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
   623
    Ok((
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
   624
        Box::new(move |path: &HgPath| matcher.matches(path)),
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
   625
        all_warnings,
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
   626
    ))
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   627
}
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   628
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   629
impl<'a> IncludeMatcher<'a> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   630
    pub fn new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   631
        ignore_patterns: Vec<IgnorePattern>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   632
        root_dir: impl AsRef<Path>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   633
    ) -> PatternResult<(Self, Vec<PatternFileWarning>)> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   634
        let (patterns, match_fn, warnings) =
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   635
            build_match(&ignore_patterns, root_dir)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   636
        let RootsDirsAndParents {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   637
            roots,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   638
            dirs,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   639
            parents,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   640
        } = roots_dirs_and_parents(&ignore_patterns)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   641
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   642
        let prefix = ignore_patterns.iter().any(|k| match k.syntax {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   643
            PatternSyntax::Path | PatternSyntax::RelPath => true,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   644
            _ => false,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   645
        });
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   646
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   647
        Ok((
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   648
            Self {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   649
                patterns,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   650
                match_fn,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   651
                prefix,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   652
                roots,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   653
                dirs,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   654
                parents,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   655
            },
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   656
            warnings,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   657
        ))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   658
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   659
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   660
    fn get_all_parents_children(&self) -> DirsChildrenMultiset {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   661
        // TODO cache
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   662
        let thing = self
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   663
            .dirs
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   664
            .iter()
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   665
            .chain(self.roots.iter())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   666
            .chain(self.parents.iter());
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   667
        DirsChildrenMultiset::new(thing, Some(&self.parents))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   668
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   669
}
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   670
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   671
impl<'a> Display for IncludeMatcher<'a> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   672
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
44803
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44802
diff changeset
   673
        // XXX What about exact matches?
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44802
diff changeset
   674
        // I'm not sure it's worth it to clone the HashSet and keep it
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44802
diff changeset
   675
        // around just in case someone wants to display the matcher, plus
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44802
diff changeset
   676
        // it's going to be unreadable after a few entries, but we need to
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44802
diff changeset
   677
        // inform in this display that exact matches are being used and are
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44802
diff changeset
   678
        // (on purpose) missing from the `includes`.
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   679
        write!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   680
            f,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   681
            "IncludeMatcher(includes='{}')",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   682
            String::from_utf8_lossy(&self.patterns.escaped_bytes())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   683
        )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   684
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   685
}
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   686
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   687
#[cfg(test)]
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   688
mod tests {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   689
    use super::*;
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   690
    use pretty_assertions::assert_eq;
44520
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   691
    use std::path::Path;
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   692
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   693
    #[test]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   694
    fn test_roots_and_dirs() {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   695
        let pats = vec![
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   696
            IgnorePattern::new(PatternSyntax::Glob, b"g/h/*", Path::new("")),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   697
            IgnorePattern::new(PatternSyntax::Glob, b"g/h", Path::new("")),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   698
            IgnorePattern::new(PatternSyntax::Glob, b"g*", Path::new("")),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   699
        ];
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   700
        let (roots, dirs) = roots_and_dirs(&pats);
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   701
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   702
        assert_eq!(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   703
            roots,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   704
            vec!(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   705
                HgPathBuf::from_bytes(b"g/h"),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   706
                HgPathBuf::from_bytes(b"g/h"),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   707
                HgPathBuf::new()
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   708
            ),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   709
        );
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   710
        assert_eq!(dirs, vec!());
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   711
    }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   712
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   713
    #[test]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   714
    fn test_roots_dirs_and_parents() {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   715
        let pats = vec![
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   716
            IgnorePattern::new(PatternSyntax::Glob, b"g/h/*", Path::new("")),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   717
            IgnorePattern::new(PatternSyntax::Glob, b"g/h", Path::new("")),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   718
            IgnorePattern::new(PatternSyntax::Glob, b"g*", Path::new("")),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   719
        ];
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   720
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   721
        let mut roots = HashSet::new();
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   722
        roots.insert(HgPathBuf::from_bytes(b"g/h"));
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   723
        roots.insert(HgPathBuf::new());
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   724
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   725
        let dirs = HashSet::new();
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   726
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   727
        let mut parents = HashSet::new();
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   728
        parents.insert(HgPathBuf::new());
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   729
        parents.insert(HgPathBuf::from_bytes(b"g"));
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   730
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   731
        assert_eq!(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   732
            roots_dirs_and_parents(&pats).unwrap(),
44524
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Raphaël Gomès <rgomes@octobus.net>
parents: 44522
diff changeset
   733
            RootsDirsAndParents {
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Raphaël Gomès <rgomes@octobus.net>
parents: 44522
diff changeset
   734
                roots,
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Raphaël Gomès <rgomes@octobus.net>
parents: 44522
diff changeset
   735
                dirs,
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Raphaël Gomès <rgomes@octobus.net>
parents: 44522
diff changeset
   736
                parents
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Raphaël Gomès <rgomes@octobus.net>
parents: 44522
diff changeset
   737
            }
44520
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   738
        );
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44519
diff changeset
   739
    }
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   740
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   741
    #[test]
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   742
    fn test_filematcher_visit_children_set() {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   743
        // Visitchildrenset
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   744
        let files = vec![HgPath::new(b"dir/subdir/foo.txt")];
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   745
        let matcher = FileMatcher::new(&files).unwrap();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   746
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   747
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   748
        set.insert(HgPath::new(b"dir"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   749
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   750
            matcher.visit_children_set(HgPath::new(b"")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   751
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   752
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   753
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   754
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   755
        set.insert(HgPath::new(b"subdir"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   756
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   757
            matcher.visit_children_set(HgPath::new(b"dir")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   758
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   759
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   760
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   761
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   762
        set.insert(HgPath::new(b"foo.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   763
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   764
            matcher.visit_children_set(HgPath::new(b"dir/subdir")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   765
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   766
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   767
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   768
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   769
            matcher.visit_children_set(HgPath::new(b"dir/subdir/x")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   770
            VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   771
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   772
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   773
            matcher.visit_children_set(HgPath::new(b"dir/subdir/foo.txt")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   774
            VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   775
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   776
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   777
            matcher.visit_children_set(HgPath::new(b"folder")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   778
            VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   779
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   780
    }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   781
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   782
    #[test]
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   783
    fn test_filematcher_visit_children_set_files_and_dirs() {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   784
        let files = vec![
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   785
            HgPath::new(b"rootfile.txt"),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   786
            HgPath::new(b"a/file1.txt"),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   787
            HgPath::new(b"a/b/file2.txt"),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   788
            // No file in a/b/c
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   789
            HgPath::new(b"a/b/c/d/file4.txt"),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   790
        ];
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   791
        let matcher = FileMatcher::new(&files).unwrap();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   792
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   793
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   794
        set.insert(HgPath::new(b"a"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   795
        set.insert(HgPath::new(b"rootfile.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   796
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   797
            matcher.visit_children_set(HgPath::new(b"")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   798
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   799
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   800
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   801
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   802
        set.insert(HgPath::new(b"b"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   803
        set.insert(HgPath::new(b"file1.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   804
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   805
            matcher.visit_children_set(HgPath::new(b"a")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   806
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   807
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   808
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   809
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   810
        set.insert(HgPath::new(b"c"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   811
        set.insert(HgPath::new(b"file2.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   812
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   813
            matcher.visit_children_set(HgPath::new(b"a/b")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   814
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   815
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   816
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   817
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   818
        set.insert(HgPath::new(b"d"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   819
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   820
            matcher.visit_children_set(HgPath::new(b"a/b/c")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   821
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   822
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   823
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   824
        set.insert(HgPath::new(b"file4.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   825
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   826
            matcher.visit_children_set(HgPath::new(b"a/b/c/d")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   827
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   828
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   829
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   830
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   831
            matcher.visit_children_set(HgPath::new(b"a/b/c/d/e")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   832
            VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   833
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   834
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   835
            matcher.visit_children_set(HgPath::new(b"folder")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   836
            VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   837
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   838
    }
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   839
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   840
    #[cfg(feature = "with-re2")]
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   841
    #[test]
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   842
    fn test_includematcher() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   843
        // VisitchildrensetPrefix
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   844
        let (matcher, _) = IncludeMatcher::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   845
            vec![IgnorePattern::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   846
                PatternSyntax::RelPath,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   847
                b"dir/subdir",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   848
                Path::new(""),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   849
            )],
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   850
            "",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   851
        )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   852
        .unwrap();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   853
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   854
        let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   855
        set.insert(HgPath::new(b"dir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   856
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   857
            matcher.visit_children_set(HgPath::new(b"")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   858
            VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   859
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   860
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   861
        let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   862
        set.insert(HgPath::new(b"subdir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   863
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   864
            matcher.visit_children_set(HgPath::new(b"dir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   865
            VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   866
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   867
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   868
            matcher.visit_children_set(HgPath::new(b"dir/subdir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   869
            VisitChildrenSet::Recursive
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   870
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   871
        // OPT: This should probably be 'all' if its parent is?
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   872
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   873
            matcher.visit_children_set(HgPath::new(b"dir/subdir/x")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   874
            VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   875
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   876
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   877
            matcher.visit_children_set(HgPath::new(b"folder")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   878
            VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   879
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   880
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   881
        // VisitchildrensetRootfilesin
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   882
        let (matcher, _) = IncludeMatcher::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   883
            vec![IgnorePattern::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   884
                PatternSyntax::RootFiles,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   885
                b"dir/subdir",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   886
                Path::new(""),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   887
            )],
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   888
            "",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   889
        )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   890
        .unwrap();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   891
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   892
        let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   893
        set.insert(HgPath::new(b"dir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   894
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   895
            matcher.visit_children_set(HgPath::new(b"")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   896
            VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   897
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   898
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   899
        let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   900
        set.insert(HgPath::new(b"subdir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   901
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   902
            matcher.visit_children_set(HgPath::new(b"dir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   903
            VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   904
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   905
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   906
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   907
            matcher.visit_children_set(HgPath::new(b"dir/subdir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   908
            VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   909
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   910
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   911
            matcher.visit_children_set(HgPath::new(b"dir/subdir/x")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   912
            VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   913
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   914
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   915
            matcher.visit_children_set(HgPath::new(b"folder")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   916
            VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   917
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   918
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   919
        // VisitchildrensetGlob
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   920
        let (matcher, _) = IncludeMatcher::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   921
            vec![IgnorePattern::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   922
                PatternSyntax::Glob,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   923
                b"dir/z*",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   924
                Path::new(""),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   925
            )],
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   926
            "",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   927
        )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   928
        .unwrap();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   929
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   930
        let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   931
        set.insert(HgPath::new(b"dir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   932
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   933
            matcher.visit_children_set(HgPath::new(b"")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   934
            VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   935
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   936
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   937
            matcher.visit_children_set(HgPath::new(b"folder")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   938
            VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   939
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   940
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   941
            matcher.visit_children_set(HgPath::new(b"dir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   942
            VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   943
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   944
        // OPT: these should probably be set().
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   945
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   946
            matcher.visit_children_set(HgPath::new(b"dir/subdir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   947
            VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   948
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   949
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   950
            matcher.visit_children_set(HgPath::new(b"dir/subdir/x")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   951
            VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   952
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44521
diff changeset
   953
    }
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44006
diff changeset
   954
}