Repository: ParthJadhav/Rust_Search (language: rust)

Issue:
Replace `~` with home directory in unix-like system
^ Title
---

I can claim it, will work in it after merge #9 


Discussion / hints:
Awesome !! Thanks @TheAwiteb . I'll test and review #9 soon

Fix patch:
diff --git a/Cargo.toml b/Cargo.toml
index 1e1c245..ddaa1ba 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -15,4 +15,5 @@ categories = ["filesystem", "algorithms"]
 [dependencies]
 regex = "1"
 ignore = "0.4"
-num_cpus = "1.0"
\ No newline at end of file
+num_cpus = "1.0"
+dirs = "4.0.0"
diff --git a/README.md b/README.md
index 4892a97..03820b1 100644
--- a/README.md
+++ b/README.md
@@ -24,7 +24,7 @@ use rust_search::SearchBuilder;
 
 fn main(){
     let search: Vec<String> = SearchBuilder::default()
-        .location("/path/to/search")
+        .location("~/path/to/directory")
         .search_input("what to search")
         .more_locations(vec!["/anotherPath/to/search", "/keepAddingIfYouWant/"])
         .ext(".extension")
diff --git a/src/builder.rs b/src/builder.rs
index c52d4d9..5515a9e 100644
--- a/src/builder.rs
+++ b/src/builder.rs
@@ -1,6 +1,6 @@
 use std::path::{Path, PathBuf};
 
-use crate::Search;
+use crate::{utils::replace_tilde_with_home_dir, Search};
 
 /// Builder for a [`Search`] instance, allowing for more complex searches.
 pub struct SearchBuilder {
@@ -39,6 +39,8 @@ impl SearchBuilder {
     }
 
     /// Set the search location to search in.
+    /// ## Notes
+    /// - Will replace `~` with [home directory](https://en.wikipedia.org/wiki/Home_directory)
     /// ### Arguments
     /// * `location` - The location to search in.
     /// ### Examples
@@ -51,7 +53,7 @@ impl SearchBuilder {
     /// .collect();
     /// ```
     pub fn location(mut self, location: impl AsRef<Path>) -> Self {
-        self.search_location = location.as_ref().to_path_buf();
+        self.search_location = replace_tilde_with_home_dir(location);
         self
     }
 
@@ -154,6 +156,8 @@ impl SearchBuilder {
     }
 
     /// Add extra locations to search in.
+    /// ## Notes
+    /// - Will replace `~` with [home directory](https://en.wikipedia.org/wiki/Home_directory)
     /// ### Arguments
     /// * `more_locations` - Vec<> of locations to search in.
     /// ### Examples
@@ -169,7 +173,7 @@ impl SearchBuilder {
         self.more_locations = Some(
             more_locations
                 .into_iter()
-                .map(|x| x.as_ref().to_path_buf())
+                .map(replace_tilde_with_home_dir)
                 .collect(),
         );
         self
diff --git a/src/utils.rs b/src/utils.rs
index 13b458a..04824f6 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,3 +1,5 @@
+use std::path::{Path, PathBuf};
+
 use regex::Regex;
 
 pub(crate) fn build_regex_search_input(
@@ -24,3 +26,17 @@ pub(crate) fn build_regex_search_input(
 fn set_case_insensitive(formatted_search_input: &str) -> String {
     "(?i)".to_owned() + formatted_search_input
 }
+
+/// Replace the tilde with the home directory, if it exists
+/// ### Arguments
+/// * `path` - The path to replace the tilde with the home directory
+pub(crate) fn replace_tilde_with_home_dir(path: impl AsRef<Path>) -> PathBuf {
+    let path = path.as_ref();
+    if path.starts_with("~") {
+        if let Some(home_dir) = dirs::home_dir() {
+            // Remove the tilde from the path and append it to the home directory
+            return home_dir.join(path.strip_prefix("~").unwrap());
+        }
+    }
+    path.to_path_buf()
+}


Test patch: