Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add sorting technique #24

Merged
merged 1 commit into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ regex = "1"
ignore = "0.4"
num_cpus = "1.0"
dirs = "4.0.0"
strsim = "0.10.0"
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ rust_search = "2.0.0"

## Examples

General use
- General use

```rust
use rust_search::SearchBuilder;
Expand All @@ -48,7 +48,34 @@ fn main(){
}
```

To get all the files with a specific extension in a directory, use:
- Sort the output by similarity with the input

```rust
use rust_search::{SearchBuilder, similarity_sort};
fn main() {
let search_input = "fly";
let mut search: Vec<String> = SearchBuilder::default()
.location("~/Desktop/")
.search_input(search_input)
.depth(1)
.ignore_case()
.build()
.collect();

similarity_sort(&mut search, &search_input);
for path in search {
println!("{:?}", path);
}
}

```
> search **without** similarity sort
`["afly.txt", "bfly.txt", "flyer.txt", "fly.txt"]`

> search **with** similarity sort
`["fly.txt", "flyer.txt", "afly.txt", "bfly.txt",]`

- To get all the files with a specific extension in a directory, use:

```rust
use rust_search::SearchBuilder;
Expand All @@ -60,7 +87,7 @@ let files: Vec<String> = SearchBuilder::default()
.collect();
```

To get all the files in a directory, use:
- To get all the files in a directory, use:

```rust
use rust_search::SearchBuilder;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ mod utils;

pub use builder::SearchBuilder;
pub use search::Search;
pub use utils::similarity_sort;
50 changes: 48 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};

use regex::Regex;
use std::path::{Path, PathBuf};
use strsim::jaro_winkler;

pub(crate) fn build_regex_search_input(
search_input: Option<&str>,
Expand Down Expand Up @@ -40,3 +40,49 @@ pub(crate) fn replace_tilde_with_home_dir(path: impl AsRef<Path>) -> PathBuf {
}
path.to_path_buf()
}

fn file_name_from_path(path: &str) -> String {
let path = Path::new(path);
let file_name = path.file_name().unwrap().to_str().unwrap();
return file_name.to_string();
}

/// This function can be used to sort the given vector on basis of similarity between the input & the vector
///
/// ### Arguments
/// * `&mut vector` - it needs a mutable reference to the vector
/// ### Examples
/// ```rust
/// use rust_search::{SearchBuilder, similarity_sort};
/// fn main() {
/// let search_input = "fly";
/// let mut search: Vec<String> = SearchBuilder::default()
/// .location("~/Desktop/")
/// .search_input(search_input)
/// .depth(1)
/// .ignore_case()
/// .build()
/// .collect();

/// similarity_sort(&mut search, &search_input);
/// for path in search {
/// println!("{:?}", path);
/// }
/// }
/// ```
///
/// search **without** similarity sort
/// `["afly.txt", "bfly.txt", "flyer.txt", "fly.txt"]`
///
/// search **with** similarity sort
/// `["fly.txt", "flyer.txt", "afly.txt", "bfly.txt",]`
pub fn similarity_sort(vector: &mut Vec<String>, input: &str) {
vector.sort_by(|a, b| {
let input = input.to_lowercase();
let a = file_name_from_path(a).to_lowercase();
let b = file_name_from_path(b).to_lowercase();
let a = jaro_winkler(a.as_str(), input.as_str());
let b = jaro_winkler(b.as_str(), input.as_str());
b.partial_cmp(&a).unwrap()
});
}