|
| 1 | +use std::path::{Path, PathBuf}; |
| 2 | + |
| 3 | +use crate::Search; |
| 4 | + |
| 5 | +/// Builder for a [`Search`] instance, allowing for more complex searches. |
| 6 | +pub struct SearchBuilder { |
| 7 | + /// The location to search in, defaults to the current directory. |
| 8 | + search_location: PathBuf, |
| 9 | + /// Vector of additional locations to search in. |
| 10 | + more_locations: Option<Vec<PathBuf>>, |
| 11 | + /// The search input, defaults to search for every word. |
| 12 | + search_input: Option<String>, |
| 13 | + /// The file extension to search for, defaults to any file extension. |
| 14 | + file_ext: Option<String>, |
| 15 | + /// The depth to search to, defaults to no limit. |
| 16 | + depth: Option<usize>, |
| 17 | + /// When set to true, Searches for exact match, defaults to false. |
| 18 | + strict: bool, |
| 19 | + /// Set search option to be case insensitive, defaults to false. |
| 20 | + ignore_case: bool, |
| 21 | + /// Search for hidden files, defaults to false. |
| 22 | + hidden: bool, |
| 23 | +} |
| 24 | + |
| 25 | +impl SearchBuilder { |
| 26 | + /// Build a new [`Search`] instance. |
| 27 | + #[allow(deprecated)] |
| 28 | + pub fn build(&self) -> Search { |
| 29 | + Search::new( |
| 30 | + &self.search_location, |
| 31 | + self.more_locations.clone(), |
| 32 | + self.search_input.as_deref(), |
| 33 | + self.file_ext.as_deref(), |
| 34 | + self.depth, |
| 35 | + self.strict, |
| 36 | + self.ignore_case, |
| 37 | + self.hidden, |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + /// Set the search location to search in. |
| 42 | + /// ### Arguments |
| 43 | + /// * `location` - The location to search in. |
| 44 | + /// ### Examples |
| 45 | + /// ```rust |
| 46 | + /// use rust_search::SearchBuilder; |
| 47 | + /// |
| 48 | + /// let search: Vec<String> = SearchBuilder::default() |
| 49 | + /// .location("src") |
| 50 | + /// .build() |
| 51 | + /// .collect(); |
| 52 | + /// ``` |
| 53 | + pub fn location(mut self, location: impl AsRef<Path>) -> Self { |
| 54 | + self.search_location = location.as_ref().to_path_buf(); |
| 55 | + self |
| 56 | + } |
| 57 | + |
| 58 | + /// Set the search input. |
| 59 | + /// ### Arguments |
| 60 | + /// * `input` - The search input. |
| 61 | + /// ### Examples |
| 62 | + /// ```rust |
| 63 | + /// use rust_search::SearchBuilder; |
| 64 | + /// |
| 65 | + /// let search: Vec<String> = SearchBuilder::default() |
| 66 | + /// .search_input("Search") |
| 67 | + /// .build() |
| 68 | + /// .collect(); |
| 69 | + /// ``` |
| 70 | + pub fn search_input(mut self, input: impl Into<String>) -> Self { |
| 71 | + self.search_input = Some(input.into()); |
| 72 | + self |
| 73 | + } |
| 74 | + |
| 75 | + /// Set the file extension to search for. |
| 76 | + /// ### Arguments |
| 77 | + /// * `ext` - The file extension to search for. |
| 78 | + /// ### Examples |
| 79 | + /// ```rust |
| 80 | + /// use rust_search::SearchBuilder; |
| 81 | + /// |
| 82 | + /// let search: Vec<String> = SearchBuilder::default() |
| 83 | + /// .ext(".rs") |
| 84 | + /// .build() |
| 85 | + /// .collect(); |
| 86 | + /// ``` |
| 87 | + pub fn ext(mut self, ext: impl Into<String>) -> Self { |
| 88 | + self.file_ext = Some(ext.into()); |
| 89 | + self |
| 90 | + } |
| 91 | + |
| 92 | + /// Set the depth to search to. |
| 93 | + /// ### Arguments |
| 94 | + /// * `depth` - The depth to search to. |
| 95 | + /// ### Examples |
| 96 | + /// ```rust |
| 97 | + /// use rust_search::SearchBuilder; |
| 98 | + /// |
| 99 | + /// let search: Vec<String> = SearchBuilder::default() |
| 100 | + /// .depth(1) |
| 101 | + /// .build() |
| 102 | + /// .collect(); |
| 103 | + /// ``` |
| 104 | + pub fn depth(mut self, depth: usize) -> Self { |
| 105 | + self.depth = Some(depth); |
| 106 | + self |
| 107 | + } |
| 108 | + |
| 109 | + /// Searches for exact match |
| 110 | + /// ### Examples |
| 111 | + /// ```rust |
| 112 | + /// use rust_search::SearchBuilder; |
| 113 | + /// |
| 114 | + /// let search: Vec<String> = SearchBuilder::default() |
| 115 | + /// .search_input("name") |
| 116 | + /// .strict() |
| 117 | + /// .build() |
| 118 | + /// .collect(); |
| 119 | + /// ``` |
| 120 | + pub fn strict(mut self) -> Self { |
| 121 | + self.strict = true; |
| 122 | + self |
| 123 | + } |
| 124 | + |
| 125 | + /// Set search option to be case insensitive. |
| 126 | + /// ### Examples |
| 127 | + /// ```rust |
| 128 | + /// use rust_search::SearchBuilder; |
| 129 | + /// |
| 130 | + /// let search: Vec<String> = SearchBuilder::default() |
| 131 | + /// .search_input("name") |
| 132 | + /// .ignore_case() |
| 133 | + /// .build() |
| 134 | + /// .collect(); |
| 135 | + /// ``` |
| 136 | + pub fn ignore_case(mut self) -> Self { |
| 137 | + self.ignore_case = true; |
| 138 | + self |
| 139 | + } |
| 140 | + |
| 141 | + /// Searches for hidden files |
| 142 | + /// ### Examples |
| 143 | + /// ```rust |
| 144 | + /// use rust_search::SearchBuilder; |
| 145 | + /// |
| 146 | + /// let search: Vec<String> = SearchBuilder::default() |
| 147 | + /// .with_hidden() |
| 148 | + /// .build() |
| 149 | + /// .collect(); |
| 150 | + /// ``` |
| 151 | + pub fn hidden(mut self) -> Self { |
| 152 | + self.hidden = true; |
| 153 | + self |
| 154 | + } |
| 155 | + |
| 156 | + /// Add extra locations to search in. |
| 157 | + /// ### Arguments |
| 158 | + /// * `more_locations` - Vec<> of locations to search in. |
| 159 | + /// ### Examples |
| 160 | + /// ```rust |
| 161 | + /// use rust_search::SearchBuilder; |
| 162 | + /// |
| 163 | + /// let search: Vec<String> = SearchBuilder::default() |
| 164 | + /// .more_locations(vec!["/Users/username/b/", "/Users/username/c/"]) |
| 165 | + /// .build() |
| 166 | + /// .collect(); |
| 167 | + /// ``` |
| 168 | + pub fn more_locations(mut self, more_locations: Vec<impl AsRef<Path>>) -> Self { |
| 169 | + self.more_locations = Some( |
| 170 | + more_locations |
| 171 | + .into_iter() |
| 172 | + .map(|x| x.as_ref().to_path_buf()) |
| 173 | + .collect(), |
| 174 | + ); |
| 175 | + self |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +impl Default for SearchBuilder { |
| 180 | + fn default() -> Self { |
| 181 | + Self { |
| 182 | + search_location: std::env::current_dir().expect("Failed to get current directory"), |
| 183 | + more_locations: None, |
| 184 | + search_input: None, |
| 185 | + file_ext: None, |
| 186 | + depth: None, |
| 187 | + strict: false, |
| 188 | + ignore_case: false, |
| 189 | + hidden: false, |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments