ParthJadhav / Rust_Search

Blazingly fast file search library built in Rust
MIT License
140 stars 15 forks source link

Add search filters (Date Created, Date Modified, File Size) #3 #25

Closed mucks closed 1 year ago

mucks commented 1 year ago

Hi, I've just implemented a way to filter by date_created, date_modified and file_size, there are possibly even more filters since I've exposed the DirEntry directly so it can be used to filter via a closure. The filters are a vec so you can add an arbitrary amount of filters. I'm aware that my solution might not be the best and I'm open to any questions and/or feedback.

ParthJadhav commented 1 year ago

Hey @mucks , that's awesome... will look into it soon

TheAwiteb commented 1 year ago

Hey @mucks, thank you for contributing, i like your code. What do you think to make it like this

use std::time::SystemTime;
use rust_search::{filter::*, SearchBuilder};
let search: Vec<String> = SearchBuilder::default()
    .filter(created_before(SystemTime::now()))
    .filter(modified_before(SystemTime::now()))
    .filter(file_size_greater(mb(20.0)))
    .build()
    .collect();
println!("{:?}", search);

Also i think filter should rename to filters

Note: We can make the filters to return a function that expects a String ref: rust-PG

mucks commented 1 year ago

Hey @mucks, thank you for contributing, i like your code. What do you think to make it like this

use std::time::SystemTime;
use rust_search::{filter::*, SearchBuilder};
let search: Vec<String> = SearchBuilder::default()
    .filter(created_before(SystemTime::now()))
    .filter(modified_before(SystemTime::now()))
    .filter(file_size_greater(mb(20.0)))
    .build()
    .collect();
println!("{:?}", search);

Also i think filter should rename to filters

Note: We can make the filters to return a function that expects a String ref: rust-PG

Hi @TheAwiteb, thank you for your feedback! I like your solution and I thought of a way to make the use of filters even more ergonomic in my opinion. I've added a trait called FilterExt that allows you to use the filter functions directly on top of the builder. Here's the example from the README:


use rust_search::{FileSize, FilterExt, SearchBuilder};
use std::time::{Duration, SystemTime};

let search: Vec<String> = SearchBuilder::default()
        .location("~/path/to/directory")
        .file_size_greater(FileSize::Kilobyte(200.0))
        .file_size_smaller(FileSize::Megabyte(10.0))
        .created_after(SystemTime::now() - Duration::from_secs(3600 * 24 * 10))
        .created_before(SystemTime::now())
        .modified_after(SystemTime::now() - Duration::from_secs(3600 * 24 * 5))
        .custom_filter(|dir| dir.metadata().unwrap().is_file())
        .custom_filter(|dir| !dir.metadata().unwrap().permissions().readonly())
        .build()
        .collect();
TheAwiteb commented 1 year ago

Hey @ParthJadhav can you review it, and also link it with #3