Skip to content

Commit 2403afb

Browse files
authored
chore: add clippy linting and CI build/test (#30)
1 parent 75c08fb commit 2403afb

File tree

6 files changed

+151
-53
lines changed

6 files changed

+151
-53
lines changed

.github/workflows/ci.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
8+
env:
9+
CARGO_TERM_COLOR: always
10+
11+
jobs:
12+
build-test:
13+
strategy:
14+
fail-fast: false
15+
16+
matrix:
17+
include:
18+
- os: windows-latest
19+
target: x86_64-pc-windows-msvc
20+
21+
- os: ubuntu-latest
22+
target: x86_64-unknown-linux-gnu
23+
24+
- os: macos-latest
25+
target: x86_64-apple-darwin
26+
27+
name: Build & Test (${{ matrix.target }})
28+
runs-on: ${{ matrix.os }}
29+
30+
env:
31+
RA_TARGET: ${{ matrix.target }}
32+
33+
steps:
34+
- name: Checkout repository
35+
uses: actions/checkout@v3
36+
37+
- name: Install Rust toolchain
38+
uses: actions-rs/toolchain@v1
39+
with:
40+
toolchain: stable
41+
target: ${{ matrix.target }}
42+
profile: minimal
43+
override: true
44+
45+
- name: Install Rust library source
46+
if: matrix.target == 'x86_64-unknown-linux-gnu'
47+
uses: actions-rs/toolchain@v1
48+
with:
49+
toolchain: stable
50+
target: ${{ matrix.target }}
51+
profile: minimal
52+
override: true
53+
components: rust-src
54+
55+
- name: Build
56+
run: cargo build --verbose --target ${{ matrix.target }}
57+
58+
- name: Run tests
59+
run: cargo test --verbose --target ${{ matrix.target }}
60+
61+
lint:
62+
name: Formatter
63+
64+
needs: build-test
65+
66+
runs-on: ubuntu-latest
67+
68+
steps:
69+
- name: Checkout repository
70+
uses: actions/checkout@v3
71+
72+
- name: Install Rust
73+
run: |
74+
rustup update stable
75+
rustup default stable
76+
rustup component add rustfmt
77+
rustup component add clippy
78+
79+
- name: Check formatting
80+
run: cargo fmt --all -- --check
81+
82+
- name: Check code for possible improvements
83+
run: cargo clippy -- -D warnings

src/builder.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl SearchBuilder {
9696
pub fn ext(mut self, ext: impl Into<String>) -> Self {
9797
let ext: String = ext.into();
9898
// Remove the dot if it's there.
99-
self.file_ext = Some(ext.strip_prefix('.').map(str::to_owned).unwrap_or(ext));
99+
self.file_ext = Some(ext.strip_prefix('.').map_or(ext.clone(), str::to_owned));
100100
self
101101
}
102102

@@ -137,7 +137,7 @@ impl SearchBuilder {
137137
/// .build()
138138
/// .collect();
139139
/// ```
140-
pub fn depth(mut self, depth: usize) -> Self {
140+
pub const fn depth(mut self, depth: usize) -> Self {
141141
self.depth = Some(depth);
142142
self
143143
}
@@ -154,7 +154,7 @@ impl SearchBuilder {
154154
/// .build()
155155
/// .collect();
156156
/// ```
157-
pub fn limit(mut self, limit: usize) -> Self {
157+
pub const fn limit(mut self, limit: usize) -> Self {
158158
self.limit = Some(limit);
159159
self
160160
}
@@ -172,7 +172,7 @@ impl SearchBuilder {
172172
/// .build()
173173
/// .collect();
174174
/// ```
175-
pub fn strict(mut self) -> Self {
175+
pub const fn strict(mut self) -> Self {
176176
self.strict = true;
177177
self
178178
}
@@ -190,7 +190,7 @@ impl SearchBuilder {
190190
/// .build()
191191
/// .collect();
192192
/// ```
193-
pub fn ignore_case(mut self) -> Self {
193+
pub const fn ignore_case(mut self) -> Self {
194194
self.ignore_case = true;
195195
self
196196
}
@@ -205,7 +205,7 @@ impl SearchBuilder {
205205
/// .build()
206206
/// .collect();
207207
/// ```
208-
pub fn hidden(mut self) -> Self {
208+
pub const fn hidden(mut self) -> Self {
209209
self.hidden = true;
210210
self
211211
}

src/filter.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@ impl FilterType {
1717
pub fn apply(&self, dir: &DirEntry) -> bool {
1818
if let Ok(m) = dir.metadata() {
1919
match self {
20-
FilterType::Created(cmp, time) => {
20+
Self::Created(cmp, time) => {
2121
if let Ok(created) = m.created() {
2222
return created.cmp(time) == *cmp;
2323
}
2424
}
25-
FilterType::Modified(cmp, time) => {
25+
Self::Modified(cmp, time) => {
2626
if let Ok(modified) = m.modified() {
2727
return modified.cmp(time) == *cmp;
2828
}
2929
}
30-
FilterType::FileSize(cmp, size_in_bytes) => {
30+
Self::FileSize(cmp, size_in_bytes) => {
3131
return m.len().cmp(size_in_bytes) == *cmp;
3232
}
33-
FilterType::Custom(f) => return f(dir),
33+
Self::Custom(f) => return f(dir),
3434
}
3535
}
3636
false
3737
}
3838
}
3939

40-
/// enum to easily convert between byte_sizes
40+
/// enum to easily convert between `byte_sizes`
4141
#[derive(Debug, Clone)]
4242
pub enum FileSize {
4343
/// size in bytes
@@ -60,7 +60,8 @@ fn convert(b: f64, pow: u32) -> u64 {
6060
#[allow(clippy::from_over_into)]
6161
impl Into<u64> for FileSize {
6262
fn into(self) -> u64 {
63-
use self::FileSize::*;
63+
use self::FileSize::{Byte, Gigabyte, Kilobyte, Megabyte, Terabyte};
64+
6465
match self {
6566
Byte(b) => b,
6667
Kilobyte(b) => convert(b, 1),
@@ -73,33 +74,33 @@ impl Into<u64> for FileSize {
7374

7475
/// import this trait to filter files
7576
pub trait FilterExt {
76-
/// files created before `t`: [SystemTime]
77+
/// files created before `t`: [`SystemTime`]
7778
fn created_before(self, t: SystemTime) -> Self;
78-
/// files created at `t`: [SystemTime]
79+
/// files created at `t`: [`SystemTime`]
7980
fn created_at(self, t: SystemTime) -> Self;
80-
/// files created after `t`: [SystemTime]
81+
/// files created after `t`: [`SystemTime`]
8182
fn created_after(self, t: SystemTime) -> Self;
82-
/// files created before `t`: [SystemTime]
83+
/// files created before `t`: [`SystemTime`]
8384
fn modified_before(self, t: SystemTime) -> Self;
84-
/// files modified at `t`: [SystemTime]
85+
/// files modified at `t`: [`SystemTime`]
8586
fn modified_at(self, t: SystemTime) -> Self;
86-
/// files modified after `t`: [SystemTime]
87+
/// files modified after `t`: [`SystemTime`]
8788
fn modified_after(self, t: SystemTime) -> Self;
8889
/// files smaller than `size_in_bytes`: [usize]
8990
fn file_size_smaller(self, size: FileSize) -> Self;
9091
/// files equal to `size_in_bytes`: [usize]
9192
fn file_size_equal(self, size: FileSize) -> Self;
9293
/// files greater than `size_in_bytes`: [usize]
9394
fn file_size_greater(self, size: FileSize) -> Self;
94-
/// custom filter that exposes the [DirEntry] directly
95+
/// custom filter that exposes the [`DirEntry`] directly
9596
/// ```rust
9697
/// builder.custom_filter(|dir| dir.metadata().unwrap().is_file())
9798
/// ```
9899
fn custom_filter(self, f: FilterFn) -> Self;
99100
}
100101

101-
use FilterType::*;
102-
use Ordering::*;
102+
use FilterType::{Created, Custom, FileSize as FilterFileSize, Modified};
103+
use Ordering::{Equal, Greater, Less};
103104
impl FilterExt for SearchBuilder {
104105
fn created_before(self, t: SystemTime) -> Self {
105106
self.filter(Created(Less, t))
@@ -126,15 +127,15 @@ impl FilterExt for SearchBuilder {
126127
}
127128

128129
fn file_size_smaller(self, size: FileSize) -> Self {
129-
self.filter(FileSize(Less, size.into()))
130+
self.filter(FilterFileSize(Less, size.into()))
130131
}
131132

132133
fn file_size_equal(self, size: FileSize) -> Self {
133-
self.filter(FileSize(Equal, size.into()))
134+
self.filter(FilterFileSize(Equal, size.into()))
134135
}
135136

136137
fn file_size_greater(self, size: FileSize) -> Self {
137-
self.filter(FileSize(Greater, size.into()))
138+
self.filter(FilterFileSize(Greater, size.into()))
138139
}
139140
fn custom_filter(self, f: FilterFn) -> Self {
140141
self.filter(Custom(f))

src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
#![warn(clippy::nursery, clippy::pedantic)]
2+
#![allow(
3+
clippy::cast_possible_truncation,
4+
clippy::cast_sign_loss,
5+
clippy::cast_precision_loss,
6+
clippy::module_name_repetitions,
7+
clippy::unused_self,
8+
clippy::return_self_not_must_use,
9+
clippy::must_use_candidate
10+
)]
111
#![warn(missing_docs)]
212
// Use the readme as the crate documentation
313
#![doc = include_str!("../README.md")]
@@ -13,4 +23,4 @@ pub use filter::{FileSize, FilterExt, FilterFn};
1323
// export this in order to use it with custom filter functions
1424
pub use ignore::DirEntry;
1525
pub use search::Search;
16-
pub use utils::similarity_sort;
26+
pub use utils::similarity_sort;

src/search.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl Search {
5858
/// * `strict` - Whether to search for the exact word or not
5959
/// * `ignore_case` - Whether to ignore case or not
6060
/// * `hidden` - Whether to search hidden files or not
61-
/// * `filters` - Vector of filters to search by DirEntry data
61+
/// * `filters` - Vector of filters to search by `DirEntry` data
6262
#[allow(clippy::too_many_arguments)]
6363
pub(crate) fn new(
6464
search_location: impl AsRef<Path>,
@@ -115,9 +115,9 @@ impl Search {
115115
{
116116
counter += 1;
117117
return WalkState::Continue;
118-
} else {
119-
return WalkState::Quit;
120118
}
119+
120+
return WalkState::Quit;
121121
}
122122
}
123123
}

src/utils.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ use regex::Regex;
22
use std::path::{Path, PathBuf};
33
use strsim::jaro_winkler;
44

5-
pub(crate) fn build_regex_search_input(
5+
const FUZZY_SEARCH: &str = r".*";
6+
7+
pub fn build_regex_search_input(
68
search_input: Option<&str>,
79
file_ext: Option<&str>,
810
strict: bool,
911
ignore_case: bool,
1012
) -> Regex {
1113
let file_type = file_ext.unwrap_or("*");
1214
let search_input = search_input.unwrap_or(r"\w+");
13-
const FUZZY_SEARCH: &str = r".*";
14-
let mut formatted_search_input;
15-
if strict {
16-
formatted_search_input = format!(r#"{}\.{}$"#, search_input, file_type);
15+
16+
let mut formatted_search_input = if strict {
17+
format!(r#"{search_input}\.{file_type}$"#)
1718
} else {
18-
formatted_search_input = format!(r#"{}{}\.{}$"#, search_input, FUZZY_SEARCH, file_type);
19-
}
19+
format!(r#"{search_input}{FUZZY_SEARCH}\.{file_type}$"#)
20+
};
21+
2022
if ignore_case {
2123
formatted_search_input = set_case_insensitive(&formatted_search_input);
2224
}
@@ -30,7 +32,7 @@ fn set_case_insensitive(formatted_search_input: &str) -> String {
3032
/// Replace the tilde with the home directory, if it exists
3133
/// ### Arguments
3234
/// * `path` - The path to replace the tilde with the home directory
33-
pub(crate) fn replace_tilde_with_home_dir(path: impl AsRef<Path>) -> PathBuf {
35+
pub fn replace_tilde_with_home_dir(path: impl AsRef<Path>) -> PathBuf {
3436
let path = path.as_ref();
3537
if path.starts_with("~") {
3638
if let Some(home_dir) = dirs::home_dir() {
@@ -44,7 +46,7 @@ pub(crate) fn replace_tilde_with_home_dir(path: impl AsRef<Path>) -> PathBuf {
4446
fn file_name_from_path(path: &str) -> String {
4547
let path = Path::new(path);
4648
let file_name = path.file_name().unwrap().to_str().unwrap();
47-
return file_name.to_string();
49+
file_name.to_string()
4850
}
4951

5052
/// This function can be used to sort the given vector on basis of similarity between the input & the vector
@@ -54,29 +56,31 @@ fn file_name_from_path(path: &str) -> String {
5456
/// ### Examples
5557
/// ```rust
5658
/// 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();
59+
///
60+
/// let search_input = "fly";
61+
/// let mut search: Vec<String> = SearchBuilder::default()
62+
/// .location("~/Desktop/")
63+
/// .search_input(search_input)
64+
/// .depth(1)
65+
/// .ignore_case()
66+
/// .build()
67+
/// .collect();
6668
67-
/// similarity_sort(&mut search, &search_input);
68-
/// for path in search {
69-
/// println!("{:?}", path);
70-
/// }
69+
/// similarity_sort(&mut search, &search_input);
70+
/// for path in search {
71+
/// println!("{:?}", path);
7172
/// }
7273
/// ```
73-
///
74+
///
7475
/// search **without** similarity sort
7576
/// `["afly.txt", "bfly.txt", "flyer.txt", "fly.txt"]`
76-
///
77+
///
7778
/// search **with** similarity sort
7879
/// `["fly.txt", "flyer.txt", "afly.txt", "bfly.txt",]`
79-
pub fn similarity_sort(vector: &mut Vec<String>, input: &str) {
80+
///
81+
/// ### Panics
82+
/// Will panic if `partial_cmp` is None
83+
pub fn similarity_sort(vector: &mut [String], input: &str) {
8084
vector.sort_by(|a, b| {
8185
let input = input.to_lowercase();
8286
let a = file_name_from_path(a).to_lowercase();

0 commit comments

Comments
 (0)