Skip to content

Commit

Permalink
chore: add clippy linting and CI build/test (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xithrius authored Jan 3, 2023
1 parent 75c08fb commit 2403afb
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 53 deletions.
83 changes: 83 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: CI

on:
push:
branches: [ master ]
pull_request:

env:
CARGO_TERM_COLOR: always

jobs:
build-test:
strategy:
fail-fast: false

matrix:
include:
- os: windows-latest
target: x86_64-pc-windows-msvc

- os: ubuntu-latest
target: x86_64-unknown-linux-gnu

- os: macos-latest
target: x86_64-apple-darwin

name: Build & Test (${{ matrix.target }})
runs-on: ${{ matrix.os }}

env:
RA_TARGET: ${{ matrix.target }}

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.target }}
profile: minimal
override: true

- name: Install Rust library source
if: matrix.target == 'x86_64-unknown-linux-gnu'
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.target }}
profile: minimal
override: true
components: rust-src

- name: Build
run: cargo build --verbose --target ${{ matrix.target }}

- name: Run tests
run: cargo test --verbose --target ${{ matrix.target }}

lint:
name: Formatter

needs: build-test

runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install Rust
run: |
rustup update stable
rustup default stable
rustup component add rustfmt
rustup component add clippy
- name: Check formatting
run: cargo fmt --all -- --check

- name: Check code for possible improvements
run: cargo clippy -- -D warnings
12 changes: 6 additions & 6 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl SearchBuilder {
pub fn ext(mut self, ext: impl Into<String>) -> Self {
let ext: String = ext.into();
// Remove the dot if it's there.
self.file_ext = Some(ext.strip_prefix('.').map(str::to_owned).unwrap_or(ext));
self.file_ext = Some(ext.strip_prefix('.').map_or(ext.clone(), str::to_owned));
self
}

Expand Down Expand Up @@ -137,7 +137,7 @@ impl SearchBuilder {
/// .build()
/// .collect();
/// ```
pub fn depth(mut self, depth: usize) -> Self {
pub const fn depth(mut self, depth: usize) -> Self {
self.depth = Some(depth);
self
}
Expand All @@ -154,7 +154,7 @@ impl SearchBuilder {
/// .build()
/// .collect();
/// ```
pub fn limit(mut self, limit: usize) -> Self {
pub const fn limit(mut self, limit: usize) -> Self {
self.limit = Some(limit);
self
}
Expand All @@ -172,7 +172,7 @@ impl SearchBuilder {
/// .build()
/// .collect();
/// ```
pub fn strict(mut self) -> Self {
pub const fn strict(mut self) -> Self {
self.strict = true;
self
}
Expand All @@ -190,7 +190,7 @@ impl SearchBuilder {
/// .build()
/// .collect();
/// ```
pub fn ignore_case(mut self) -> Self {
pub const fn ignore_case(mut self) -> Self {
self.ignore_case = true;
self
}
Expand All @@ -205,7 +205,7 @@ impl SearchBuilder {
/// .build()
/// .collect();
/// ```
pub fn hidden(mut self) -> Self {
pub const fn hidden(mut self) -> Self {
self.hidden = true;
self
}
Expand Down
37 changes: 19 additions & 18 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ impl FilterType {
pub fn apply(&self, dir: &DirEntry) -> bool {
if let Ok(m) = dir.metadata() {
match self {
FilterType::Created(cmp, time) => {
Self::Created(cmp, time) => {
if let Ok(created) = m.created() {
return created.cmp(time) == *cmp;
}
}
FilterType::Modified(cmp, time) => {
Self::Modified(cmp, time) => {
if let Ok(modified) = m.modified() {
return modified.cmp(time) == *cmp;
}
}
FilterType::FileSize(cmp, size_in_bytes) => {
Self::FileSize(cmp, size_in_bytes) => {
return m.len().cmp(size_in_bytes) == *cmp;
}
FilterType::Custom(f) => return f(dir),
Self::Custom(f) => return f(dir),
}
}
false
}
}

/// enum to easily convert between byte_sizes
/// enum to easily convert between `byte_sizes`
#[derive(Debug, Clone)]
pub enum FileSize {
/// size in bytes
Expand All @@ -60,7 +60,8 @@ fn convert(b: f64, pow: u32) -> u64 {
#[allow(clippy::from_over_into)]
impl Into<u64> for FileSize {
fn into(self) -> u64 {
use self::FileSize::*;
use self::FileSize::{Byte, Gigabyte, Kilobyte, Megabyte, Terabyte};

match self {
Byte(b) => b,
Kilobyte(b) => convert(b, 1),
Expand All @@ -73,33 +74,33 @@ impl Into<u64> for FileSize {

/// import this trait to filter files
pub trait FilterExt {
/// files created before `t`: [SystemTime]
/// files created before `t`: [`SystemTime`]
fn created_before(self, t: SystemTime) -> Self;
/// files created at `t`: [SystemTime]
/// files created at `t`: [`SystemTime`]
fn created_at(self, t: SystemTime) -> Self;
/// files created after `t`: [SystemTime]
/// files created after `t`: [`SystemTime`]
fn created_after(self, t: SystemTime) -> Self;
/// files created before `t`: [SystemTime]
/// files created before `t`: [`SystemTime`]
fn modified_before(self, t: SystemTime) -> Self;
/// files modified at `t`: [SystemTime]
/// files modified at `t`: [`SystemTime`]
fn modified_at(self, t: SystemTime) -> Self;
/// files modified after `t`: [SystemTime]
/// files modified after `t`: [`SystemTime`]
fn modified_after(self, t: SystemTime) -> Self;
/// files smaller than `size_in_bytes`: [usize]
fn file_size_smaller(self, size: FileSize) -> Self;
/// files equal to `size_in_bytes`: [usize]
fn file_size_equal(self, size: FileSize) -> Self;
/// files greater than `size_in_bytes`: [usize]
fn file_size_greater(self, size: FileSize) -> Self;
/// custom filter that exposes the [DirEntry] directly
/// custom filter that exposes the [`DirEntry`] directly
/// ```rust
/// builder.custom_filter(|dir| dir.metadata().unwrap().is_file())
/// ```
fn custom_filter(self, f: FilterFn) -> Self;
}

use FilterType::*;
use Ordering::*;
use FilterType::{Created, Custom, FileSize as FilterFileSize, Modified};
use Ordering::{Equal, Greater, Less};
impl FilterExt for SearchBuilder {
fn created_before(self, t: SystemTime) -> Self {
self.filter(Created(Less, t))
Expand All @@ -126,15 +127,15 @@ impl FilterExt for SearchBuilder {
}

fn file_size_smaller(self, size: FileSize) -> Self {
self.filter(FileSize(Less, size.into()))
self.filter(FilterFileSize(Less, size.into()))
}

fn file_size_equal(self, size: FileSize) -> Self {
self.filter(FileSize(Equal, size.into()))
self.filter(FilterFileSize(Equal, size.into()))
}

fn file_size_greater(self, size: FileSize) -> Self {
self.filter(FileSize(Greater, size.into()))
self.filter(FilterFileSize(Greater, size.into()))
}
fn custom_filter(self, f: FilterFn) -> Self {
self.filter(Custom(f))
Expand Down
12 changes: 11 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#![warn(clippy::nursery, clippy::pedantic)]
#![allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_precision_loss,
clippy::module_name_repetitions,
clippy::unused_self,
clippy::return_self_not_must_use,
clippy::must_use_candidate
)]
#![warn(missing_docs)]
// Use the readme as the crate documentation
#![doc = include_str!("../README.md")]
Expand All @@ -13,4 +23,4 @@ pub use filter::{FileSize, FilterExt, FilterFn};
// export this in order to use it with custom filter functions
pub use ignore::DirEntry;
pub use search::Search;
pub use utils::similarity_sort;
pub use utils::similarity_sort;
6 changes: 3 additions & 3 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Search {
/// * `strict` - Whether to search for the exact word or not
/// * `ignore_case` - Whether to ignore case or not
/// * `hidden` - Whether to search hidden files or not
/// * `filters` - Vector of filters to search by DirEntry data
/// * `filters` - Vector of filters to search by `DirEntry` data
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
search_location: impl AsRef<Path>,
Expand Down Expand Up @@ -115,9 +115,9 @@ impl Search {
{
counter += 1;
return WalkState::Continue;
} else {
return WalkState::Quit;
}

return WalkState::Quit;
}
}
}
Expand Down
54 changes: 29 additions & 25 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ use regex::Regex;
use std::path::{Path, PathBuf};
use strsim::jaro_winkler;

pub(crate) fn build_regex_search_input(
const FUZZY_SEARCH: &str = r".*";

pub fn build_regex_search_input(
search_input: Option<&str>,
file_ext: Option<&str>,
strict: bool,
ignore_case: bool,
) -> Regex {
let file_type = file_ext.unwrap_or("*");
let search_input = search_input.unwrap_or(r"\w+");
const FUZZY_SEARCH: &str = r".*";
let mut formatted_search_input;
if strict {
formatted_search_input = format!(r#"{}\.{}$"#, search_input, file_type);

let mut formatted_search_input = if strict {
format!(r#"{search_input}\.{file_type}$"#)
} else {
formatted_search_input = format!(r#"{}{}\.{}$"#, search_input, FUZZY_SEARCH, file_type);
}
format!(r#"{search_input}{FUZZY_SEARCH}\.{file_type}$"#)
};

if ignore_case {
formatted_search_input = set_case_insensitive(&formatted_search_input);
}
Expand All @@ -30,7 +32,7 @@ fn set_case_insensitive(formatted_search_input: &str) -> String {
/// 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 {
pub 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() {
Expand All @@ -44,7 +46,7 @@ pub(crate) fn replace_tilde_with_home_dir(path: impl AsRef<Path>) -> PathBuf {
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();
file_name.to_string()
}

/// This function can be used to sort the given vector on basis of similarity between the input & the vector
Expand All @@ -54,29 +56,31 @@ fn file_name_from_path(path: &str) -> String {
/// ### 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();
///
/// 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);
/// }
/// 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) {
///
/// ### Panics
/// Will panic if `partial_cmp` is None
pub fn similarity_sort(vector: &mut [String], input: &str) {
vector.sort_by(|a, b| {
let input = input.to_lowercase();
let a = file_name_from_path(a).to_lowercase();
Expand Down

2 comments on commit 2403afb

@ParthJadhav
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Xithrius , Can you help me add linting & refactoring Verve ?

https://github.com/ParthJadhav/Verve

@Xithrius
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll see what I can do about the rust bit.

Please sign in to comment.