Skip to content

Commit 4200127

Browse files
authored
chore: add sorting technique (#24)
1 parent a0261a7 commit 4200127

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ regex = "1"
1717
ignore = "0.4"
1818
num_cpus = "1.0"
1919
dirs = "4.0.0"
20+
strsim = "0.10.0"

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ rust_search = "2.0.0"
2323

2424
## Examples
2525

26-
General use
26+
- General use
2727

2828
```rust
2929
use rust_search::SearchBuilder;
@@ -48,7 +48,34 @@ fn main(){
4848
}
4949
```
5050

51-
To get all the files with a specific extension in a directory, use:
51+
- Sort the output by similarity with the input
52+
53+
```rust
54+
use rust_search::{SearchBuilder, similarity_sort};
55+
fn main() {
56+
let search_input = "fly";
57+
let mut search: Vec<String> = SearchBuilder::default()
58+
.location("~/Desktop/")
59+
.search_input(search_input)
60+
.depth(1)
61+
.ignore_case()
62+
.build()
63+
.collect();
64+
65+
similarity_sort(&mut search, &search_input);
66+
for path in search {
67+
println!("{:?}", path);
68+
}
69+
}
70+
71+
```
72+
> search **without** similarity sort
73+
`["afly.txt", "bfly.txt", "flyer.txt", "fly.txt"]`
74+
75+
> search **with** similarity sort
76+
`["fly.txt", "flyer.txt", "afly.txt", "bfly.txt",]`
77+
78+
- To get all the files with a specific extension in a directory, use:
5279

5380
```rust
5481
use rust_search::SearchBuilder;
@@ -60,7 +87,7 @@ let files: Vec<String> = SearchBuilder::default()
6087
.collect();
6188
```
6289

63-
To get all the files in a directory, use:
90+
- To get all the files in a directory, use:
6491

6592
```rust
6693
use rust_search::SearchBuilder;

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ pub use filter::{FileSize, FilterExt, FilterFn};
1313
// export this in order to use it with custom filter functions
1414
pub use ignore::DirEntry;
1515
pub use search::Search;
16+
pub use utils::similarity_sort;

src/utils.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::path::{Path, PathBuf};
2-
31
use regex::Regex;
2+
use std::path::{Path, PathBuf};
3+
use strsim::jaro_winkler;
44

55
pub(crate) fn build_regex_search_input(
66
search_input: Option<&str>,
@@ -40,3 +40,49 @@ pub(crate) fn replace_tilde_with_home_dir(path: impl AsRef<Path>) -> PathBuf {
4040
}
4141
path.to_path_buf()
4242
}
43+
44+
fn file_name_from_path(path: &str) -> String {
45+
let path = Path::new(path);
46+
let file_name = path.file_name().unwrap().to_str().unwrap();
47+
return file_name.to_string();
48+
}
49+
50+
/// This function can be used to sort the given vector on basis of similarity between the input & the vector
51+
///
52+
/// ### Arguments
53+
/// * `&mut vector` - it needs a mutable reference to the vector
54+
/// ### Examples
55+
/// ```rust
56+
/// use rust_search::{SearchBuilder, similarity_sort};
57+
/// fn main() {
58+
/// let search_input = "fly";
59+
/// let mut search: Vec<String> = SearchBuilder::default()
60+
/// .location("~/Desktop/")
61+
/// .search_input(search_input)
62+
/// .depth(1)
63+
/// .ignore_case()
64+
/// .build()
65+
/// .collect();
66+
67+
/// similarity_sort(&mut search, &search_input);
68+
/// for path in search {
69+
/// println!("{:?}", path);
70+
/// }
71+
/// }
72+
/// ```
73+
///
74+
/// search **without** similarity sort
75+
/// `["afly.txt", "bfly.txt", "flyer.txt", "fly.txt"]`
76+
///
77+
/// search **with** similarity sort
78+
/// `["fly.txt", "flyer.txt", "afly.txt", "bfly.txt",]`
79+
pub fn similarity_sort(vector: &mut Vec<String>, input: &str) {
80+
vector.sort_by(|a, b| {
81+
let input = input.to_lowercase();
82+
let a = file_name_from_path(a).to_lowercase();
83+
let b = file_name_from_path(b).to_lowercase();
84+
let a = jaro_winkler(a.as_str(), input.as_str());
85+
let b = jaro_winkler(b.as_str(), input.as_str());
86+
b.partial_cmp(&a).unwrap()
87+
});
88+
}

0 commit comments

Comments
 (0)