Skip to main content

sift_core/search/options/
mod.rs

1bitflags::bitflags! {
2    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
3    pub struct SearchMatchFlags: u16 {
4        const INVERT_MATCH     = 1 << 0;
5        const FIXED_STRINGS    = 1 << 1;
6        const WORD_REGEXP      = 1 << 2;
7        const LINE_REGEXP      = 1 << 3;
8        const ONLY_MATCHING    = 1 << 4;
9        const MULTILINE        = 1 << 5;
10        const MULTILINE_DOTALL = 1 << 6;
11        const CRLF             = 1 << 7;
12    }
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
16pub enum CaseMode {
17    #[default]
18    Sensitive,
19    Insensitive,
20    Smart,
21}
22
23impl CaseMode {
24    #[must_use]
25    pub const fn is_case_insensitive(self) -> bool {
26        matches!(self, Self::Insensitive)
27    }
28}
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
31pub enum BinaryMode {
32    #[default]
33    Quit,
34    SearchBinary,
35    AsText,
36}
37
38#[derive(Debug, Clone)]
39pub struct SearchOptions {
40    pub flags: SearchMatchFlags,
41    pub case_mode: CaseMode,
42    pub max_results: Option<usize>,
43    pub before_context: usize,
44    pub after_context: usize,
45    pub binary_mode: BinaryMode,
46    pub replace: Option<String>,
47    pub unicode: bool,
48    pub regex_size_limit: usize,
49    pub dfa_size_limit: usize,
50}
51
52impl Default for SearchOptions {
53    fn default() -> Self {
54        Self {
55            flags: SearchMatchFlags::default(),
56            case_mode: CaseMode::default(),
57            max_results: None,
58            before_context: 0,
59            after_context: 0,
60            binary_mode: BinaryMode::default(),
61            replace: None,
62            unicode: true,
63            regex_size_limit: 0,
64            dfa_size_limit: 0,
65        }
66    }
67}
68
69impl SearchOptions {
70    #[must_use]
71    pub const fn case_insensitive(&self) -> bool {
72        self.case_mode.is_case_insensitive()
73    }
74
75    #[must_use]
76    pub const fn invert_match(&self) -> bool {
77        self.flags.contains(SearchMatchFlags::INVERT_MATCH)
78    }
79
80    #[must_use]
81    pub const fn fixed_strings(&self) -> bool {
82        self.flags.contains(SearchMatchFlags::FIXED_STRINGS)
83    }
84
85    #[must_use]
86    pub const fn word_regexp(&self) -> bool {
87        self.flags.contains(SearchMatchFlags::WORD_REGEXP)
88    }
89
90    #[must_use]
91    pub const fn line_regexp(&self) -> bool {
92        self.flags.contains(SearchMatchFlags::LINE_REGEXP)
93    }
94
95    #[must_use]
96    pub const fn only_matching(&self) -> bool {
97        self.flags.contains(SearchMatchFlags::ONLY_MATCHING)
98    }
99
100    #[must_use]
101    pub const fn multiline(&self) -> bool {
102        self.flags.contains(SearchMatchFlags::MULTILINE)
103    }
104
105    #[must_use]
106    pub const fn multiline_dotall(&self) -> bool {
107        self.flags.contains(SearchMatchFlags::MULTILINE_DOTALL)
108    }
109
110    #[must_use]
111    pub const fn crlf(&self) -> bool {
112        self.flags.contains(SearchMatchFlags::CRLF)
113    }
114
115    #[must_use]
116    pub const fn precludes_trigram_index(&self) -> bool {
117        self.invert_match()
118    }
119}