From 41032c5276e0cbb7583e3b56774d8fc2a422c10a Mon Sep 17 00:00:00 2001
From: Eduardo Trujillo <ed@chromabits.com>
Date: Sat, 16 Jan 2021 12:18:58 -0800
Subject: [PATCH] refactor(files): Use generic Path references where possible

---
 src/files/path.rs         |  8 ++++----
 src/files/path_context.rs | 18 ++++++++++--------
 src/files/service.rs      | 10 ++++++----
 3 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/src/files/path.rs b/src/files/path.rs
index e8e894a..e0fb0b7 100644
--- a/src/files/path.rs
+++ b/src/files/path.rs
@@ -1,5 +1,5 @@
 use snafu::{ResultExt, Snafu};
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
 
 #[derive(Snafu, Debug)]
 pub enum Error {
@@ -21,9 +21,9 @@ pub enum Error {
 /// Note: This function does not check that the path resolves somewhere outside
 /// of the base path.
 pub fn resolve_path_within_tree(
-  base_path: &PathBuf,
-  current_path: &PathBuf,
-  path: &PathBuf,
+  base_path: &Path,
+  current_path: &Path,
+  path: &Path,
 ) -> Result<PathBuf, Error> {
   let new_path = if path.is_absolute() {
     base_path.join(path.strip_prefix("/").context(StripPrefix)?)
diff --git a/src/files/path_context.rs b/src/files/path_context.rs
index 9f2bbbd..00744bd 100644
--- a/src/files/path_context.rs
+++ b/src/files/path_context.rs
@@ -10,7 +10,7 @@ use std::{
   collections::HashMap,
   convert::{TryFrom, TryInto},
   fmt::Debug,
-  path::PathBuf,
+  path::{Path, PathBuf},
 };
 
 #[derive(Snafu, Debug)]
@@ -44,7 +44,9 @@ enum PathMatcher {
 }
 
 impl PathMatcher {
-  pub fn matches_path(&self, input_path: &PathBuf) -> bool {
+  pub fn matches_path<P: AsRef<Path>>(&self, input_path: P) -> bool {
+    let input_path = input_path.as_ref();
+
     match self {
       Self::Static { path } => input_path.eq(path),
       Self::Prefix { prefix } => input_path.starts_with(prefix),
@@ -88,7 +90,7 @@ pub struct PathContext {
 }
 
 impl PathContext {
-  pub fn matches_path(&self, input_path: &PathBuf) -> bool {
+  pub fn matches_path<P: AsRef<Path>>(&self, input_path: P) -> bool {
     if let Some(matcher) = &self.matcher {
       matcher.matches_path(input_path)
     } else {
@@ -260,7 +262,7 @@ mod tests {
       PathMatcher::Static {
         path: "hello/world.png".into(),
       }
-      .matches_path(&"hello/world.png".into()),
+      .matches_path(&"hello/world.png"),
       true
     );
 
@@ -268,7 +270,7 @@ mod tests {
       PathMatcher::Static {
         path: "hello/world.tar.gz".into(),
       }
-      .matches_path(&"hello/world.png".into()),
+      .matches_path(&"hello/world.png"),
       false
     );
   }
@@ -279,7 +281,7 @@ mod tests {
       PathMatcher::Prefix {
         prefix: "hello/".into(),
       }
-      .matches_path(&"hello/world.png".into()),
+      .matches_path(&"hello/world.png"),
       true
     );
 
@@ -287,7 +289,7 @@ mod tests {
       PathMatcher::Prefix {
         prefix: "hello/bye".into(),
       }
-      .matches_path(&"hello/world.png".into()),
+      .matches_path(&"hello/world.png"),
       false
     );
   }
@@ -310,7 +312,7 @@ mod tests {
       PathMatcher::Regex {
         regex: Regex::new(r"\.png$").unwrap(),
       }
-      .matches_path(&"hello/world.tar.gz".into()),
+      .matches_path(&"hello/world.tar.gz"),
       false
     );
   }
diff --git a/src/files/service.rs b/src/files/service.rs
index 6dbaf0f..263e849 100644
--- a/src/files/service.rs
+++ b/src/files/service.rs
@@ -20,7 +20,7 @@ use snafu::Snafu;
 use std::{
   convert::TryInto,
   io,
-  path::PathBuf,
+  path::{Path, PathBuf},
   rc::Rc,
   sync::{Arc, RwLock},
   task::{Context, Poll},
@@ -169,7 +169,7 @@ impl FilesService {
       RequestContext {
         path: serve_dir.join(&path_from_request),
         serve_dir,
-        path_context: self.get_path_context(&path_from_request.into()),
+        path_context: self.get_path_context(&path_from_request),
       }
     };
 
@@ -402,7 +402,9 @@ impl FilesService {
     Ok((canonical_path, serve_dir, path_from_request))
   }
 
-  fn get_path_context(&self, path_from_request: &PathBuf) -> PathContext {
+  fn get_path_context<P: AsRef<Path>>(&self, path_from_request: P) -> PathContext {
+    let path_from_request = path_from_request.as_ref();
+
     for path_context in self.path_contexts.as_ref() {
       if path_context.matches_path(path_from_request) {
         debug!(
@@ -458,7 +460,7 @@ impl Service for FilesService {
       Err(e) => return self.handle_early_err(e, req),
     };
 
-    let path_context = self.get_path_context(&path_from_request.into());
+    let path_context = self.get_path_context(&path_from_request);
 
     let request_context = RequestContext {
       path,
-- 
GitLab