Patchwork D7528: rust-matchers: add `FileMatcher` implementation

login
register
mail settings
Submitter phabricator
Date Dec. 2, 2019, 11:49 a.m.
Message ID <2f6af2a51f488866545d6cdfcdedd9ad@localhost.localdomain>
Download mbox | patch
Permalink /patch/43561/
State Not Applicable
Headers show

Comments

phabricator - Dec. 2, 2019, 11:49 a.m.
Alphare updated this revision to Diff 18418.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D7528?vs=18404&id=18418

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7528/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7528

AFFECTED FILES
  rust/hg-core/src/matchers.rs

CHANGE DETAILS




To: Alphare, #hg-reviewers
Cc: durin42, kevincox, mercurial-devel

Patch

diff --git a/rust/hg-core/src/matchers.rs b/rust/hg-core/src/matchers.rs
--- a/rust/hg-core/src/matchers.rs
+++ b/rust/hg-core/src/matchers.rs
@@ -7,8 +7,10 @@ 
 
 //! Structs and types for matching files and directories.
 
-use crate::utils::hg_path::{HgPath, HgPathBuf};
+use crate::utils::hg_path::HgPath;
+use crate::DirsMultiset;
 use std::collections::HashSet;
+use std::iter::FromIterator;
 
 pub enum VisitChildrenSet<'a> {
     /// Don't visit anything
@@ -114,3 +116,62 @@ 
         false
     }
 }
+
+/// Matches the input files exactly. They are interpreted as paths, not
+/// patterns.
+///
+///```
+/// use hg::{ matchers::{Matcher, FileMatcher}, utils::hg_path::HgPath };
+///
+/// let files = [HgPath::new(b"a.txt"), HgPath::new(br"re:.*\.c$")];
+/// let matcher = FileMatcher::new(&files);
+///
+/// assert_eq!(true, matcher.matches(HgPath::new(b"a.txt")));
+/// assert_eq!(false, matcher.matches(HgPath::new(b"b.txt")));
+/// assert_eq!(false, matcher.matches(HgPath::new(b"main.c")));
+/// assert_eq!(true, matcher.matches(HgPath::new(br"re:.*\.c$")));
+/// ```
+#[derive(Debug)]
+pub struct FileMatcher<'a> {
+    files: HashSet<&'a HgPath>,
+    dirs: DirsMultiset,
+}
+
+impl<'a> FileMatcher<'a> {
+    pub fn new(files: &'a [impl AsRef<HgPath>]) -> Self {
+        Self {
+            files: HashSet::from_iter(files.iter().map(|f| f.as_ref())),
+            dirs: DirsMultiset::from_manifest(files),
+        }
+    }
+    fn inner_matches(&self, filename: impl AsRef<HgPath>) -> bool {
+        self.files.contains(filename.as_ref())
+    }
+}
+
+impl<'a> Matcher for FileMatcher<'a> {
+    fn file_set(&self) -> Option<&HashSet<&HgPath>> {
+        Some(&self.files)
+    }
+    fn exact_match(&self, filename: impl AsRef<HgPath>) -> bool {
+        self.inner_matches(filename)
+    }
+    fn matches(&self, filename: impl AsRef<HgPath>) -> bool {
+        self.inner_matches(filename)
+    }
+    fn visit_children_set(
+        &self,
+        _directory: impl AsRef<HgPath>,
+    ) -> VisitChildrenSet {
+        // TODO implement once we have `status.traverse`
+        // This is useless until unknown files are taken into account
+        // Which will not need to happen before the `PatternMatcher`.
+        unimplemented!()
+    }
+    fn matches_everything(&self) -> bool {
+        false
+    }
+    fn is_exact(&self) -> bool {
+        true
+    }
+}