annotate rust/hg-core/src/matchers.rs @ 44519:52d40f8fb82d

rust-matchers: add function to generate a regex matcher function This function will be used to help build the upcoming `IncludeMatcher`. While Re2 is still used and behind a feature flag, this function returns an error meant for fallback in the default case. Differential Revision: https://phab.mercurial-scm.org/D7922
author Rapha?l Gom?s <rgomes@octobus.net>
date Fri, 17 Jan 2020 11:29:33 +0100
parents 54d185eb24b5
children d4e8cfcde012
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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::{
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
13 filepatterns::PatternResult, utils::hg_path::HgPath, DirsMultiset,
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
14 DirstateMapError, PatternError,
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
15 };
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
16 use std::collections::HashSet;
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
17 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
18 use std::ops::Deref;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
19
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
20 #[derive(Debug, PartialEq)]
43832
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43611
diff changeset
21 pub enum VisitChildrenSet<'a> {
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
22 /// Don't visit anything
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
23 Empty,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
24 /// Only visit this directory
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
25 This,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
26 /// 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
27 /// 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
28 Set(HashSet<&'a HgPath>),
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
29 /// 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
30 Recursive,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
31 }
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
32
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
33 pub trait Matcher {
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
34 /// Explicitly listed files
43832
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43611
diff changeset
35 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
36 /// 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
37 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
38 /// 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
39 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
40 /// 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
41 /// 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
42 /// 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
43 /// 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
44 /// patterns.
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
45 ///
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
46 /// # Example
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
47 ///
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
48 /// 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
49 /// 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
50 /// 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
51 /// are not).
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
52 ///
44006
72bced4f2936 rust-matchers: fixing cargo doc
Georges Racinet <georges.racinet@octobus.net>
parents: 43914
diff changeset
53 /// ```text
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
54 /// ```ignore
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
55 /// '' -> {'foo', 'qux'}
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
56 /// 'baz' -> set()
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
57 /// 'foo' -> {'bar'}
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
58 /// // 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
59 /// // 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
60 /// // 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
61 /// // `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
62 /// 'foo/bar' -> 'this'
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
63 /// 'qux' -> 'this'
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 /// # Important
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
66 ///
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
67 /// 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
68 /// 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
69 /// 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
70 /// 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
71 /// 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
72 /// it may return `VisitChildrenSet::This`.
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
73 /// 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
74 /// 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
75 /// 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
76 /// `VisitChildrenSet::This`).
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
77 fn visit_children_set(
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
78 &self,
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
79 directory: impl AsRef<HgPath>,
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
80 ) -> VisitChildrenSet;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
81 /// 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
82 /// 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
83 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
84 /// 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
85 /// might be possible.
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
86 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
87 }
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
88
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
89 /// Matches everything.
43834
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43832
diff changeset
90 ///```
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43832
diff changeset
91 /// 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
92 ///
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43832
diff changeset
93 /// let matcher = AlwaysMatcher;
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43832
diff changeset
94 ///
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
95 /// 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
96 /// 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
97 /// 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
98 /// 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
99 /// ```
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
100 #[derive(Debug)]
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
101 pub struct AlwaysMatcher;
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
102
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
103 impl Matcher for AlwaysMatcher {
43832
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43611
diff changeset
104 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
105 None
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
106 }
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
107 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
108 false
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
109 }
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
110 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
111 true
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
112 }
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
113 fn visit_children_set(
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
114 &self,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
115 _directory: impl AsRef<HgPath>,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
116 ) -> VisitChildrenSet {
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
117 VisitChildrenSet::Recursive
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
118 }
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
119 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
120 true
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
121 }
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
122 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
123 false
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
124 }
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
125 }
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
126
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
127 /// 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
128 /// patterns.
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
129 ///
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
130 ///```
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
131 /// 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
132 ///
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
133 /// 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
134 /// let matcher = FileMatcher::new(&files).unwrap();
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
135 ///
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
136 /// 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
137 /// 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
138 /// 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
139 /// 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
140 /// ```
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
141 #[derive(Debug)]
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
142 pub struct FileMatcher<'a> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
143 files: HashSet<&'a HgPath>,
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
144 dirs: DirsMultiset,
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
145 }
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 impl<'a> FileMatcher<'a> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
148 pub fn new(
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
149 files: &'a [impl AsRef<HgPath>],
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
150 ) -> Result<Self, DirstateMapError> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
151 Ok(Self {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
152 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
153 dirs: DirsMultiset::from_manifest(files)?,
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
154 })
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
155 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
156 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
157 self.files.contains(filename.as_ref())
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
158 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
159 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
160
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
161 impl<'a> Matcher for FileMatcher<'a> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
162 fn file_set(&self) -> Option<&HashSet<&HgPath>> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
163 Some(&self.files)
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
164 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
165 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
166 self.inner_matches(filename)
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
167 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
168 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
169 self.inner_matches(filename)
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
170 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
171 fn visit_children_set(
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
172 &self,
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
173 directory: impl AsRef<HgPath>,
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
174 ) -> VisitChildrenSet {
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
175 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
176 return VisitChildrenSet::Empty;
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
177 }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
178 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
179
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
180 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
181 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
182 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
183
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
184 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
185 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
186 candidates = candidates
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
187 .iter()
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
188 .filter_map(|c| {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
189 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
190 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
191 } else {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
192 None
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
193 }
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 .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
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
198 // `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
199 // 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
200 // '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
201 // '/' 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
202 // 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
203 VisitChildrenSet::Set(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
204 candidates
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
205 .iter()
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
206 .filter_map(|c| {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
207 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
208 Some(*c)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
209 } else {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
210 None
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 })
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
213 .collect(),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
214 )
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
215 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
216 fn matches_everything(&self) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
217 false
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
218 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
219 fn is_exact(&self) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
220 true
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
221 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
222 }
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
223
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
224 #[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
225 /// 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
226 /// pattern.
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
227 ///
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
228 /// 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
229 /// underlying engine `Re2`, for instance anything with back-references.
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
230 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
231 pattern: &[u8],
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
232 ) -> 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
233 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
234 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
235 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
236 }
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
237
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
238 #[cfg(not(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
239 fn re_matcher(_: &[u8]) -> PatternResult<Box<dyn 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
240 Err(PatternError::Re2NotInstalled)
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
241 }
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
242
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
243 #[cfg(test)]
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
244 mod tests {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
245 use super::*;
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
246 use pretty_assertions::assert_eq;
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
247
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
248 #[test]
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
249 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
250 // Visitchildrenset
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
251 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
252 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
253
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
254 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
255 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
256 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
257 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
258 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
259 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
260
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
261 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
262 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
263 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
264 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
265 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
266 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
267
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
268 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
269 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
270 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
271 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
272 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
273 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
274
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
275 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
276 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
277 VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
278 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
279 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
280 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
281 VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
282 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
283 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
284 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
285 VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
286 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
287 }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
288
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
289 #[test]
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
290 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
291 let files = vec![
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
292 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
293 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
294 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
295 // 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
296 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
297 ];
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
298 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
299
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
300 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
301 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
302 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
303 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
304 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
305 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
306 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
307
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
308 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
309 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
310 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
311 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
312 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
313 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
314 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
315
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
316 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
317 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
318 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
319 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
320 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
321 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
322 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
323
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
324 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
325 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
326 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
327 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
328 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
329 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
330 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
331 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
332 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
333 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
334 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
335 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
336
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
337 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
338 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
339 VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
340 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
341 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
342 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
343 VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
344 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
345 }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
346 }