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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use super::config::data::PATHS_MAX;
use std::path::Path;
pub struct SearchPath {
paths: Vec<String>,
}
impl SearchPath {
pub fn new(paths: &Vec<String>) -> Self {
let cleaned_paths = clean_paths(&paths);
let paths = cleaned_paths
.into_iter()
.enumerate()
.filter(|(i, _el)| i < &PATHS_MAX)
.map(|(_i, el)| el)
.collect();
Self { paths }
}
pub fn update(&mut self, new_vec: Vec<String>) {
let cleaned_new = clean_paths(&new_vec);
self.paths = cleaned_new;
}
pub fn read(&self) -> Vec<String> {
self.paths.clone()
}
pub fn len(&self) -> usize {
self.paths.len()
}
}
fn clean_paths(unchecked_paths: &Vec<String>) -> Vec<String> {
let mut checked_paths: Vec<String> = vec![];
for unchecked in unchecked_paths {
if let Ok(path_ok) = Path::new(unchecked).canonicalize() {
if path_ok.is_dir() {
if let Some(path_str) = path_ok.to_str() {
let mut is_add_worthy = true;
let path_string = path_str.to_string();
for checked in checked_paths.iter_mut() {
let path_len = path_str.len();
let checked_len = checked.len();
if path_len < checked_len {
if checked[..path_len] == path_string {
*checked = path_string;
is_add_worthy = false;
break;
}
} else {
if path_string[..checked_len] == *checked {
is_add_worthy = false;
break;
}
}
}
if is_add_worthy {
checked_paths.push(path_str.to_string());
}
} else {
warn!(
"Path {:?} has some encoding problem, and will not be included in search!",
unchecked
)
}
} else {
warn!("Path {:?} does not exist as directory/folder, and will not be included in search!", unchecked)
}
} else {
error!(
"Path {:?} is not a valid directory/folder, and will not be included in search!",
unchecked
);
}
}
checked_paths
}
#[cfg(test)]
mod tests {
use super::*;
fn init() {
let _ = env_logger::builder().is_test(true).try_init();
}
static HAS_FS_UP: bool = false;
static _TEST_DATA: [&str; 6] = [
"/tmp/adbf/Music/audiobooks/A-D",
"/tmp/adbf/Music/audiobooks/E-F/George Orwell/Animal Farm",
"/tmp/adbf/audible/George Orwell/Animal Farm",
"/tmp/adbf/audible/..",
"/tmp/adbf/audible/Philip K. Dick/Electric Dreams",
"/tmp/adbf",
];
#[test]
fn test_clean_paths_overlap() {
init();
let testing_path = vec![
"/tmp/adbf/Music/audiobooks/A-D".to_string(),
"/tmp/adbf/Music/audiobooks/E-F/George Orwell/Animal Farm".to_string(),
"/tmp/adbf/audible/George Orwell/Animal Farm".to_string(),
"/tmp/adbf/Music/audiobooks/E-F".to_string(),
];
let return_value = clean_paths(&testing_path);
if HAS_FS_UP {
assert_eq!(
return_value,
vec![
"/tmp/adbf/Music/audiobooks/A-D".to_string(),
"/tmp/adbf/Music/audiobooks/E-F".to_string(),
"/tmp/adbf/audible/George Orwell/Animal Farm".to_string(),
]
);
}
let testing_path = vec![
"/tmp/adbf/Music/audiobooks/A-D".to_string(),
"/tmp/adbf/Music/audiobooks/E-F/George Orwell/Animal Farm".to_string(),
"/tmp/adbf/audible/George Orwell/Animal Farm".to_string(),
"/tmp/adbf/Music/audiobooks/E-F".to_string(),
"/tmp/adbf/audible/George Orwell".to_string(),
];
let return_value = clean_paths(&testing_path);
if HAS_FS_UP {
assert_eq!(
return_value,
vec![
"/tmp/adbf/Music/audiobooks/A-D".to_string(),
"/tmp/adbf/Music/audiobooks/E-F".to_string(),
"/tmp/adbf/audible/George Orwell".to_string(),
]
);
}
}
#[test]
fn test_clean_paths_parent() {
init();
let testing_path = vec![
"/tmp/adbf/Music/audiobooks/A-D".to_string(),
"/tmp/adbf/Music/audiobooks/../audiobooks/E-F/George Orwell/Animal Farm".to_string(),
"/tmp/adbf/audible/George Orwell/Animal Farm".to_string(),
];
let return_value = clean_paths(&testing_path);
if HAS_FS_UP {
assert_eq!(
return_value,
vec![
"/tmp/adbf/Music/audiobooks/A-D".to_string(),
"/tmp/adbf/Music/audiobooks/E-F/George Orwell/Animal Farm".to_string(),
"/tmp/adbf/audible/George Orwell/Animal Farm".to_string(),
]
);
}
let testing_path = vec![
"/tmp/adbf/Music/../../adbf/Music/audiobooks/A-D".to_string(),
"/tmp/adbf/Music/audiobooks".to_string(),
"/tmp/adbf/Music/audiobooks/E-F/George Orwell/Animal Farm".to_string(),
];
let return_value = clean_paths(&testing_path);
if HAS_FS_UP {
assert_eq!(
return_value,
vec!["/tmp/adbf/Music/audiobooks".to_string(),]
);
}
let testing_path = vec![
"/tmp/adbf/Music/../../adbf/Music/audiobooks/A-D".to_string(),
"/tmp/adbf/Music/audiobooks/E-F/George Orwell/Animal Farm".to_string(),
];
let return_value = clean_paths(&testing_path);
if HAS_FS_UP {
assert_eq!(
return_value,
vec![
"/tmp/adbf/Music/audiobooks/A-D".to_string(),
"/tmp/adbf/Music/audiobooks/E-F/George Orwell/Animal Farm".to_string(),
]
);
}
}
}