Skip to content
Closed
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
6 changes: 6 additions & 0 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ mod time;

#[cfg(unix)]
mod owner;

pub trait Filter {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't really get the benefit of this trait. Does this give us anything beyond having a consistent name for the filtering methods?

Is this intended to be used more in an additional refactor?

type Item;

fn matches(&self, item: &Self::Item) -> bool;
}
11 changes: 11 additions & 0 deletions src/filter/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::sync::OnceLock;
use anyhow::anyhow;
use regex::Regex;

use super::Filter;

static SIZE_CAPTURES: OnceLock<Regex> = OnceLock::new();

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -74,6 +76,15 @@ impl SizeFilter {
}
}

impl Filter for SizeFilter {
type Item = u64;

#[inline(always)]
fn matches(&self, item: &Self::Item) -> bool {
self.is_within(*item)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here. Why not just move is_within to the body of the matches method?

}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
11 changes: 11 additions & 0 deletions src/filter/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use jiff::{Span, Timestamp, Zoned, civil::DateTime, tz::TimeZone};

use std::time::{Duration, SystemTime, UNIX_EPOCH};

use super::Filter;

/// Filter based on time ranges.
#[derive(Debug, PartialEq, Eq)]
pub enum TimeFilter {
Expand Down Expand Up @@ -62,6 +64,15 @@ impl TimeFilter {
}
}

impl Filter for TimeFilter {
type Item = SystemTime;

#[inline(always)]
fn matches(&self, item: &Self::Item) -> bool {
self.applies_to(item)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
5 changes: 3 additions & 2 deletions src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::error::print_error;
use crate::exec;
use crate::exit_codes::{ExitCode, merge_exitcodes};
use crate::filesystem;
use crate::filter::Filter;
use crate::output;

/// The receiver thread can either be buffering results or directly streaming to the console.
Expand Down Expand Up @@ -568,7 +569,7 @@ impl WorkerState {
if config
.size_constraints
.iter()
.any(|sc| !sc.is_within(file_size))
.any(|sc| !sc.matches(&file_size))
{
return WalkState::Continue;
}
Expand All @@ -589,7 +590,7 @@ impl WorkerState {
matched = config
.time_constraints
.iter()
.all(|tf| tf.applies_to(&modified));
.all(|tf| tf.matches(&modified));
}
if !matched {
return WalkState::Continue;
Expand Down