changeset 47378:777c3d231913

rust: Make some file path parameters less generic These are not widely used APIs that benefit from being maximally flexible, taking an explicit `&Path` borrow is fine and simplifies their internals. Differential Revision: https://phab.mercurial-scm.org/D10833
author Simon Sapin <simon.sapin@octobus.net>
date Wed, 02 Jun 2021 18:14:44 +0200
parents 26127236b229
children f6bb181c75f8
files rust/hg-core/src/filepatterns.rs rust/hg-core/src/matchers.rs
diffstat 2 files changed, 35 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/filepatterns.rs	Mon Apr 26 22:59:56 2021 +0200
+++ b/rust/hg-core/src/filepatterns.rs	Wed Jun 02 18:14:44 2021 +0200
@@ -318,9 +318,9 @@
     NoSuchFile(PathBuf),
 }
 
-pub fn parse_pattern_file_contents<P: AsRef<Path>>(
+pub fn parse_pattern_file_contents(
     lines: &[u8],
-    file_path: P,
+    file_path: &Path,
     warn: bool,
 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> {
     let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap();
@@ -357,7 +357,7 @@
                 current_syntax = rel_syntax;
             } else if warn {
                 warnings.push(PatternFileWarning::InvalidSyntax(
-                    file_path.as_ref().to_owned(),
+                    file_path.to_owned(),
                     syntax.to_owned(),
                 ));
             }
@@ -384,32 +384,30 @@
                 PatternError::UnsupportedSyntax(syntax) => {
                     PatternError::UnsupportedSyntaxInFile(
                         syntax,
-                        file_path.as_ref().to_string_lossy().into(),
+                        file_path.to_string_lossy().into(),
                         line_number,
                     )
                 }
                 _ => e,
             })?,
             &line,
-            &file_path,
+            file_path,
         ));
     }
     Ok((inputs, warnings))
 }
 
-pub fn read_pattern_file<P: AsRef<Path>>(
-    file_path: P,
+pub fn read_pattern_file(
+    file_path: &Path,
     warn: bool,
 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> {
-    let mut f = match File::open(file_path.as_ref()) {
+    let mut f = match File::open(file_path) {
         Ok(f) => Ok(f),
         Err(e) => match e.kind() {
             std::io::ErrorKind::NotFound => {
                 return Ok((
                     vec![],
-                    vec![PatternFileWarning::NoSuchFile(
-                        file_path.as_ref().to_owned(),
-                    )],
+                    vec![PatternFileWarning::NoSuchFile(file_path.to_owned())],
                 ))
             }
             _ => Err(e),
@@ -431,15 +429,11 @@
 }
 
 impl IgnorePattern {
-    pub fn new(
-        syntax: PatternSyntax,
-        pattern: &[u8],
-        source: impl AsRef<Path>,
-    ) -> Self {
+    pub fn new(syntax: PatternSyntax, pattern: &[u8], source: &Path) -> Self {
         Self {
             syntax,
             pattern: pattern.to_owned(),
-            source: source.as_ref().to_owned(),
+            source: source.to_owned(),
         }
     }
 }
@@ -452,10 +446,10 @@
 /// `subinclude:` is not treated as a special pattern here: unraveling them
 /// needs to occur in the "ignore" phase.
 pub fn get_patterns_from_file(
-    pattern_file: impl AsRef<Path>,
-    root_dir: impl AsRef<Path>,
+    pattern_file: &Path,
+    root_dir: &Path,
 ) -> PatternResult<(Vec<IgnorePattern>, Vec<PatternFileWarning>)> {
-    let (patterns, mut warnings) = read_pattern_file(&pattern_file, true)?;
+    let (patterns, mut warnings) = read_pattern_file(pattern_file, true)?;
     let patterns = patterns
         .into_iter()
         .flat_map(|entry| -> PatternResult<_> {
@@ -465,11 +459,9 @@
             Ok(match syntax {
                 PatternSyntax::Include => {
                     let inner_include =
-                        root_dir.as_ref().join(get_path_from_bytes(&pattern));
-                    let (inner_pats, inner_warnings) = get_patterns_from_file(
-                        &inner_include,
-                        root_dir.as_ref(),
-                    )?;
+                        root_dir.join(get_path_from_bytes(&pattern));
+                    let (inner_pats, inner_warnings) =
+                        get_patterns_from_file(&inner_include, root_dir)?;
                     warnings.extend(inner_warnings);
                     inner_pats
                 }
@@ -496,9 +488,9 @@
 
 impl SubInclude {
     pub fn new(
-        root_dir: impl AsRef<Path>,
+        root_dir: &Path,
         pattern: &[u8],
-        source: impl AsRef<Path>,
+        source: &Path,
     ) -> Result<SubInclude, HgPathError> {
         let normalized_source =
             normalize_path_bytes(&get_bytes_from_path(source));
@@ -510,7 +502,7 @@
         let path = source_root.join(get_path_from_bytes(pattern));
         let new_root = path.parent().unwrap_or_else(|| path.deref());
 
-        let prefix = canonical_path(&root_dir, &root_dir, new_root)?;
+        let prefix = canonical_path(root_dir, root_dir, new_root)?;
 
         Ok(Self {
             prefix: path_to_hg_path_buf(prefix).and_then(|mut p| {
@@ -527,10 +519,10 @@
 
 /// Separate and pre-process subincludes from other patterns for the "ignore"
 /// phase.
-pub fn filter_subincludes(
-    ignore_patterns: &[IgnorePattern],
-    root_dir: impl AsRef<Path>,
-) -> Result<(Vec<SubInclude>, Vec<&IgnorePattern>), HgPathError> {
+pub fn filter_subincludes<'a>(
+    ignore_patterns: &'a [IgnorePattern],
+    root_dir: &Path,
+) -> Result<(Vec<SubInclude>, Vec<&'a IgnorePattern>), HgPathError> {
     let mut subincludes = vec![];
     let mut others = vec![];
 
@@ -541,7 +533,7 @@
             source,
         } = ignore_pattern;
         if *syntax == PatternSyntax::SubInclude {
-            subincludes.push(SubInclude::new(&root_dir, pattern, &source)?);
+            subincludes.push(SubInclude::new(root_dir, pattern, &source)?);
         } else {
             others.push(ignore_pattern)
         }
--- a/rust/hg-core/src/matchers.rs	Mon Apr 26 22:59:56 2021 +0200
+++ b/rust/hg-core/src/matchers.rs	Wed Jun 02 18:14:44 2021 +0200
@@ -237,7 +237,7 @@
 /// ///
 /// let ignore_patterns =
 /// vec![IgnorePattern::new(PatternSyntax::RootGlob, b"this*", Path::new(""))];
-/// let (matcher, _) = IncludeMatcher::new(ignore_patterns, "").unwrap();
+/// let (matcher, _) = IncludeMatcher::new(ignore_patterns, "".as_ref()).unwrap();
 /// ///
 /// assert_eq!(matcher.matches(HgPath::new(b"testing")), false);
 /// assert_eq!(matcher.matches(HgPath::new(b"this should work")), true);
@@ -479,7 +479,7 @@
 /// should be matched.
 fn build_match<'a, 'b>(
     ignore_patterns: &'a [IgnorePattern],
-    root_dir: impl AsRef<Path>,
+    root_dir: &Path,
 ) -> PatternResult<(
     Vec<u8>,
     Box<dyn Fn(&HgPath) -> bool + 'b + Sync>,
@@ -500,7 +500,7 @@
 
         for SubInclude { prefix, root, path } in subincludes.into_iter() {
             let (match_fn, warnings) =
-                get_ignore_function(vec![path.to_path_buf()], root)?;
+                get_ignore_function(vec![path.to_path_buf()], &root)?;
             all_warnings.extend(warnings);
             prefixes.push(prefix.to_owned());
             submatchers.insert(prefix.to_owned(), match_fn);
@@ -573,7 +573,7 @@
 /// ignored.
 pub fn get_ignore_function<'a>(
     all_pattern_files: Vec<PathBuf>,
-    root_dir: impl AsRef<Path>,
+    root_dir: &Path,
 ) -> PatternResult<(
     Box<dyn for<'r> Fn(&'r HgPath) -> bool + Sync + 'a>,
     Vec<PatternFileWarning>,
@@ -581,9 +581,9 @@
     let mut all_patterns = vec![];
     let mut all_warnings = vec![];
 
-    for pattern_file in all_pattern_files.into_iter() {
+    for pattern_file in &all_pattern_files {
         let (patterns, warnings) =
-            get_patterns_from_file(pattern_file, &root_dir)?;
+            get_patterns_from_file(pattern_file, root_dir)?;
 
         all_patterns.extend(patterns.to_owned());
         all_warnings.extend(warnings);
@@ -599,7 +599,7 @@
 impl<'a> IncludeMatcher<'a> {
     pub fn new(
         ignore_patterns: Vec<IgnorePattern>,
-        root_dir: impl AsRef<Path>,
+        root_dir: &Path,
     ) -> PatternResult<(Self, Vec<PatternFileWarning>)> {
         let (patterns, match_fn, warnings) =
             build_match(&ignore_patterns, root_dir)?;
@@ -816,7 +816,7 @@
                 b"dir/subdir",
                 Path::new(""),
             )],
-            "",
+            "".as_ref(),
         )
         .unwrap();
 
@@ -854,7 +854,7 @@
                 b"dir/subdir",
                 Path::new(""),
             )],
-            "",
+            "".as_ref(),
         )
         .unwrap();
 
@@ -892,7 +892,7 @@
                 b"dir/z*",
                 Path::new(""),
             )],
-            "",
+            "".as_ref(),
         )
         .unwrap();