-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathquery_part.rs
More file actions
132 lines (118 loc) · 4.27 KB
/
Copy pathquery_part.rs
File metadata and controls
132 lines (118 loc) · 4.27 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::{env::home_dir, path::PathBuf};
use crate::directory::{scored_directories, sub_directories, Directory, ScoredDirectory};
#[derive(Debug, PartialEq)]
pub enum QueryPart {
/// ~
Tilde,
/// .. (two or more dots)
Back(u32),
/// /
Root,
/// - (one or more dashes)
Skip(u32),
/// Anything else
Text(String),
}
impl From<&str> for QueryPart {
fn from(part: &str) -> Self {
match part {
"" => QueryPart::Root,
"~" => QueryPart::Tilde,
_ if part.starts_with('-') && part.replace('-', "").is_empty() => {
QueryPart::Skip(part.len() as u32 - 1)
}
_ if part.starts_with("..") && part.replace('.', "").is_empty() => {
QueryPart::Back(part.len() as u32 - 1)
}
_ => QueryPart::Text(part.to_string()),
}
}
}
impl QueryPart {
pub fn matching_directories(&self, dirs: &[Directory]) -> Vec<Directory> {
match &self {
QueryPart::Tilde => {
let Ok(dir) =
Directory::try_from(home_dir().unwrap_or(PathBuf::from("/")).as_path())
else {
return vec![];
};
vec![dir]
}
QueryPart::Root => {
let Ok(dir) = Directory::try_from(PathBuf::from("/").as_path()) else {
eprintln!("Couldn't create Directory from root!");
return vec![];
};
vec![dir]
}
QueryPart::Skip(depth) => dirs
.iter()
.flat_map(|dir| sub_directories(dir.location().as_path(), *depth))
.collect(),
QueryPart::Back(amount) => {
let Some(target_dir) = dirs.first() else {
return vec![];
};
let target_location = target_dir.location().join("../".repeat(*amount as usize));
let Ok(dir) = Directory::try_from(target_location.as_path()) else {
return vec![];
};
vec![dir]
}
QueryPart::Text(text) => {
let mut scored_dirs = scored_directories(
&dirs
.iter()
.flat_map(|dir| sub_directories(dir.location().as_path(), 0))
.collect::<Vec<_>>(),
text.as_str(),
);
let average_score: f64 = scored_dirs
.iter()
.map(|scored_dir| f64::from(scored_dir.score()))
.sum::<f64>()
/ scored_dirs.len() as f64;
let half_of_highest_score = scored_dirs
.iter()
.map(ScoredDirectory::score)
.max()
.unwrap_or(0_i32)
/ 2;
// sort by score, if scores are equal by alphabetical order
scored_dirs.sort_unstable_by(|a, b| {
a.score()
.cmp(&b.score())
.then(a.directory().location().cmp(b.directory().location()))
});
scored_dirs
.iter()
// remove dirs with low score
.filter(|scored_dir| {
f64::from(scored_dir.score()) > 0.0
&& f64::from(scored_dir.score()) >= average_score
&& scored_dir.score() >= half_of_highest_score
})
.map(|scored_dir| scored_dir.directory().clone())
.collect()
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from() {
assert_eq!(QueryPart::Tilde, QueryPart::from("~"));
assert_eq!(QueryPart::Back(1), QueryPart::from(".."));
assert_eq!(QueryPart::Back(2), QueryPart::from("..."));
assert_eq!(QueryPart::Root, QueryPart::from(""));
assert_eq!(QueryPart::Skip(0), QueryPart::from("-"));
assert_eq!(QueryPart::Skip(1), QueryPart::from("--"));
assert_eq!(
QueryPart::Text(String::from("hello")),
QueryPart::from("hello")
);
}
}