Repository: sharkdp/fd
File: src/internal.rs (Rust)

Code before:
        Some(match limit_kind {
            "+" => SizeFilter::Min(size),
            _ => SizeFilter::Max(size),
        })
    }

    pub fn is_within(&self, size: u64) -> bool {
        match self {
            &SizeFilter::Max(limit) => size <= limit,
            &SizeFilter::Min(limit) => size >= limit,
        }
    }
}

/// Filter based on time ranges
#[derive(Debug, PartialEq)]
pub enum TimeFilter {
    Before(SystemTime),
    After(SystemTime),
}

impl TimeFilter {
    fn from_str(s: &str) -> Option<SystemTime> {
        use humantime;
        humantime::parse_duration(s)
            .map(|duration| SystemTime::now() - duration)
            .or_else(|_| humantime::parse_rfc3339_weak(s))
            .ok()
    }

    pub fn before(s: &str) -> Option<TimeFilter> {
        Some(TimeFilter::Before(TimeFilter::from_str(s)?))
    }

    pub fn after(s: &str) -> Option<TimeFilter> {
        Some(TimeFilter::After(TimeFilter::from_str(s)?))
    }

    pub fn is_within(&self, t: &SystemTime) -> bool {
        match self {
            TimeFilter::Before(limit) => t <= limit,
            TimeFilter::After(limit) => t >= limit,
        }
    }
}

/// Configuration options for *fd*.
pub struct FdOptions {
    /// Whether the search is case-sensitive or case-insensitive.
    pub case_sensitive: bool,


Review comment (suggestion):
I think it would be cleaner if we could pass `now: SystemTime` into this function in order to make it side-effect free (or *pure*). This would make testing deterministic.

By getting `SystemTime::now()` just once in the main-function, we would also prevent *really* unlikely situations where we would end up with two different `SystemTime::now()` values in multiple `TimeFilter`s.

Code after:
        Some(match limit_kind {
            "+" => SizeFilter::Min(size),
            _ => SizeFilter::Max(size),
        })
    }

    pub fn is_within(&self, size: u64) -> bool {
        match self {
            &SizeFilter::Max(limit) => size <= limit,
            &SizeFilter::Min(limit) => size >= limit,
        }
    }
}

/// Filter based on time ranges
#[derive(Debug, PartialEq)]
pub enum TimeFilter {
    Before(SystemTime),
    After(SystemTime),
}

impl TimeFilter {
    fn from_str(ref_time: &SystemTime, s: &str) -> Option<SystemTime> {
        use humantime;
        humantime::parse_duration(s)
            .map(|duration| *ref_time - duration)
            .or_else(|_| humantime::parse_rfc3339_weak(s))
            .ok()
    }

    pub fn before(ref_time: &SystemTime, s: &str) -> Option<TimeFilter> {
        TimeFilter::from_str(ref_time, s).map(TimeFilter::Before)
    }

    pub fn after(ref_time: &SystemTime, s: &str) -> Option<TimeFilter> {
        TimeFilter::from_str(ref_time, s).map(TimeFilter::After)
    }

    pub fn applies_to(&self, t: &SystemTime) -> bool {
        match self {
            TimeFilter::Before(limit) => t <= limit,
            TimeFilter::After(limit) => t >= limit,
        }
    }
}

/// Configuration options for *fd*.
pub struct FdOptions {
    /// Whether the search is case-sensitive or case-insensitive.
    pub case_sensitive: bool,