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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
use {
super::*,
crate::{
app::AppContext,
errors::PatternError,
pattern::{Pattern, PatternParts},
},
bet::BeTree,
};
/// wraps both
/// - the "pattern" (which may be used to filter and rank file entries)
/// - the source raw string which was used to build it and which may
/// be put back in the input.
#[derive(Debug, Clone)]
pub struct InputPattern {
pub raw: String,
pub pattern: Pattern,
}
impl PartialEq for InputPattern {
fn eq(&self, other: &Self) -> bool {
self.raw == other.raw
}
}
impl InputPattern {
pub fn none() -> Self {
Self {
raw: String::new(),
pattern: Pattern::None,
}
}
pub fn new(
raw: String,
parts_expr: &BeTree<PatternOperator, PatternParts>,
con: &AppContext,
) -> Result<Self, PatternError> {
let pattern = Pattern::new(parts_expr, &con.search_modes)?;
Ok(Self { raw, pattern })
}
pub fn is_none(&self) -> bool {
self.pattern.is_empty()
}
pub fn is_some(&self) -> bool {
self.pattern.is_some()
}
/// empties the pattern and return it
/// Similar to Option::take
pub fn take(&mut self) -> Self {
std::mem::replace(self, Self::none())
}
pub fn as_option(self) -> Option<Self> {
if self.is_some() {
Some(self)
} else {
None
}
}
/// from a pattern used to filter a tree, build a pattern
/// which would make sense to filter a previewed file
pub fn tree_to_preview(&self) -> Self {
let regex_parts: Option<(String, String)> = match &self.pattern {
Pattern::ContentExact(cp) => Some(cp.to_regex_parts()),
Pattern::ContentRegex(rp) => Some(rp.to_regex_parts()),
Pattern::Composite(cp) => cp.expr
.iter_atoms()
.find_map(|p| match p {
Pattern::ContentExact(ce) => Some(ce.to_regex_parts()),
Pattern::ContentRegex(cp) => Some(cp.to_regex_parts()),
_ => None
}),
_ => None,
};
regex_parts
.and_then(|rp| RegexPattern::from(&rp.0, &rp.1).ok())
.map(|rp| InputPattern {
raw: rp.to_string(),
pattern: Pattern::NameRegex(rp),
})
.unwrap_or_else(InputPattern::none)
}
}
|