author | Raphaël Gomès <rgomes@octobus.net> |
Fri, 29 Nov 2019 18:54:06 +0100 | |
changeset 43914 | 69c4f3cf2cdf |
parent 43863 | bc7d8f45c3b6 |
child 44006 | 72bced4f2936 |
permissions | -rw-r--r-- |
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 |
|
43914
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
10 |
use crate::{utils::hg_path::HgPath, DirsMultiset, DirstateMapError}; |
43438
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
11 |
use std::collections::HashSet; |
43914
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
12 |
use std::iter::FromIterator; |
43438
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
13 |
|
43832
1bb4e9b02984
rust-matchers: improve `Matcher` trait ergonomics
Raphaël Gomès <rgomes@octobus.net>
parents:
43611
diff
changeset
|
14 |
pub enum VisitChildrenSet<'a> { |
43438
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
15 |
/// Don't visit anything |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
16 |
Empty, |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
17 |
/// Only visit this directory |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
18 |
This, |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
19 |
/// 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
|
20 |
/// 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
|
21 |
Set(HashSet<&'a HgPath>), |
43438
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
22 |
/// 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
|
23 |
Recursive, |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
24 |
} |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
25 |
|
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
26 |
pub trait Matcher { |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
27 |
/// Explicitly listed files |
43832
1bb4e9b02984
rust-matchers: improve `Matcher` trait ergonomics
Raphaël Gomès <rgomes@octobus.net>
parents:
43611
diff
changeset
|
28 |
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
|
29 |
/// 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
|
30 |
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
|
31 |
/// 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
|
32 |
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
|
33 |
/// 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
|
34 |
/// 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
|
35 |
/// 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
|
36 |
/// 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
|
37 |
/// patterns. |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
38 |
/// |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
39 |
/// # Example |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
40 |
/// |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
41 |
/// 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
|
42 |
/// 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
|
43 |
/// 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
|
44 |
/// are not). |
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 |
/// ```ignore |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
47 |
/// '' -> {'foo', 'qux'} |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
48 |
/// 'baz' -> set() |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
49 |
/// 'foo' -> {'bar'} |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
50 |
/// // 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
|
51 |
/// // 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
|
52 |
/// // 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
|
53 |
/// // `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
|
54 |
/// 'foo/bar' -> 'this' |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
55 |
/// 'qux' -> 'this' |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
56 |
/// ``` |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
57 |
/// # Important |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
58 |
/// |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
59 |
/// 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
|
60 |
/// 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
|
61 |
/// 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
|
62 |
/// 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
|
63 |
/// 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
|
64 |
/// it may return `VisitChildrenSet::This`. |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
65 |
/// 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
|
66 |
/// 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
|
67 |
/// 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
|
68 |
/// `VisitChildrenSet::This`). |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
69 |
fn visit_children_set( |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
70 |
&self, |
43611
27c25c0dc967
rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents:
43438
diff
changeset
|
71 |
directory: impl AsRef<HgPath>, |
27c25c0dc967
rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents:
43438
diff
changeset
|
72 |
) -> VisitChildrenSet; |
43438
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
73 |
/// 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
|
74 |
/// 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
|
75 |
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
|
76 |
/// 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
|
77 |
/// might be possible. |
43611
27c25c0dc967
rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents:
43438
diff
changeset
|
78 |
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
|
79 |
} |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
80 |
|
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
81 |
/// Matches everything. |
43834
542c8b277261
rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
43832
diff
changeset
|
82 |
///``` |
542c8b277261
rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
43832
diff
changeset
|
83 |
/// 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
|
84 |
/// |
542c8b277261
rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
43832
diff
changeset
|
85 |
/// let matcher = AlwaysMatcher; |
542c8b277261
rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
43832
diff
changeset
|
86 |
/// |
43914
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
87 |
/// 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
|
88 |
/// 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
|
89 |
/// 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
|
90 |
/// 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
|
91 |
/// ``` |
43438
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
92 |
#[derive(Debug)] |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
93 |
pub struct AlwaysMatcher; |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
94 |
|
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
95 |
impl Matcher for AlwaysMatcher { |
43832
1bb4e9b02984
rust-matchers: improve `Matcher` trait ergonomics
Raphaël Gomès <rgomes@octobus.net>
parents:
43611
diff
changeset
|
96 |
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
|
97 |
None |
43438
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
98 |
} |
43611
27c25c0dc967
rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents:
43438
diff
changeset
|
99 |
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
|
100 |
false |
27c25c0dc967
rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents:
43438
diff
changeset
|
101 |
} |
27c25c0dc967
rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents:
43438
diff
changeset
|
102 |
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
|
103 |
true |
27c25c0dc967
rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents:
43438
diff
changeset
|
104 |
} |
43438
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
105 |
fn visit_children_set( |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
106 |
&self, |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
107 |
_directory: impl AsRef<HgPath>, |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
108 |
) -> VisitChildrenSet { |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
109 |
VisitChildrenSet::Recursive |
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
110 |
} |
43611
27c25c0dc967
rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents:
43438
diff
changeset
|
111 |
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
|
112 |
true |
27c25c0dc967
rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents:
43438
diff
changeset
|
113 |
} |
27c25c0dc967
rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents:
43438
diff
changeset
|
114 |
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
|
115 |
false |
27c25c0dc967
rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents:
43438
diff
changeset
|
116 |
} |
43438
a77d4fe347a4
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
117 |
} |
43914
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
118 |
|
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
119 |
/// 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
|
120 |
/// patterns. |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
121 |
/// |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
122 |
///``` |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
123 |
/// 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
|
124 |
/// |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
125 |
/// 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
|
126 |
/// let matcher = FileMatcher::new(&files).unwrap(); |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
127 |
/// |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
128 |
/// 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
|
129 |
/// 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
|
130 |
/// 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
|
131 |
/// 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
|
132 |
/// ``` |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
133 |
#[derive(Debug)] |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
134 |
pub struct FileMatcher<'a> { |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
135 |
files: HashSet<&'a HgPath>, |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
136 |
dirs: DirsMultiset, |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
137 |
} |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
138 |
|
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
139 |
impl<'a> FileMatcher<'a> { |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
140 |
pub fn new( |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
141 |
files: &'a [impl AsRef<HgPath>], |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
142 |
) -> Result<Self, DirstateMapError> { |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
143 |
Ok(Self { |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
144 |
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
|
145 |
dirs: DirsMultiset::from_manifest(files)?, |
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 |
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
|
149 |
self.files.contains(filename.as_ref()) |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
150 |
} |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
151 |
} |
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 |
impl<'a> Matcher for FileMatcher<'a> { |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
154 |
fn file_set(&self) -> Option<&HashSet<&HgPath>> { |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
155 |
Some(&self.files) |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
156 |
} |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
157 |
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
|
158 |
self.inner_matches(filename) |
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 |
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
|
161 |
self.inner_matches(filename) |
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 |
fn visit_children_set( |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
164 |
&self, |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
165 |
_directory: impl AsRef<HgPath>, |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
166 |
) -> VisitChildrenSet { |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
167 |
// TODO implement once we have `status.traverse` |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
168 |
// This is useless until unknown files are taken into account |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
169 |
// Which will not need to happen before the `IncludeMatcher`. |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
170 |
unimplemented!() |
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 |
fn matches_everything(&self) -> bool { |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
173 |
false |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
174 |
} |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
175 |
fn is_exact(&self) -> bool { |
69c4f3cf2cdf
rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
43863
diff
changeset
|
176 |
true |
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 |
} |