summaryrefslogtreecommitdiffstats
path: root/src/filter.rs
blob: d1c23eb25794d58f5245b4b92582cd4ee3edc9a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use regex::Regex;

use crate::fileinfo::{FileInfo, FileKind};

#[derive(Clone)]
pub enum FilterKind {
    Extension(String),
    Name(String),
    Directory,
    All,
}

impl FilterKind {
    pub fn from_input(input: &str) -> Self {
        let words = input.split_whitespace().collect::<Vec<&str>>();
        if words.len() < 2 {
            return Self::All;
        }
        match words[0] {
            "d" => Self::Directory,
            "e" => Self::Extension(words[1].to_owned()),
            "n" => Self::Name(words[1].to_owned()),
            _ => Self::All,
        }
    }

    pub fn filter_by(&self, fileinfo: &FileInfo) -> bool {
        match self {
            Self::Extension(ext) => Self::filter_by_ext(fileinfo, ext.clone()),
            Self::Name(filename) => Self::filter_by_name(fileinfo, filename.clone()),
            Self::Directory => Self::filter_directory(fileinfo),
            Self::All => true,
        }
    }

    fn filter_by_ext(fileinfo: &FileInfo, ext: String) -> bool {
        fileinfo.extension == ext
    }

    fn filter_by_name(fileinfo: &FileInfo, filename: String) -> bool {
        if let Ok(re) = Regex::new(&filename) {
            re.is_match(&fileinfo.filename)
        } else {
            false
        }
    }

    fn filter_directory(fileinfo: &FileInfo) -> bool {
        matches!(fileinfo.file_kind, FileKind::Directory)
    }
}