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
use std::fs;
use std::path::{Path, PathBuf};
pub fn first<F>(directory: &Path, condition: F) -> Option<PathBuf> where F: Fn(&Path) -> bool {
macro_rules! ok(
($result:expr) => (
match $result {
Ok(ok) => ok,
Err(_) => return None,
}
);
);
if !ok!(fs::metadata(directory)).is_dir() {
return None;
}
for entry in ok!(fs::read_dir(&directory)) {
let entry = ok!(entry);
if ok!(fs::metadata(entry.path())).is_dir() {
continue;
}
if condition(&entry.path()) {
return Some(entry.path());
}
}
None
}
pub fn with_extension(directory: &Path, extension: &str) -> Option<PathBuf> {
use std::ascii::AsciiExt;
macro_rules! ok(
($option:expr) => (
match $option {
Some(some) => some,
None => return false,
}
);
);
first(directory, |path| -> bool {
ok!(ok!(path.extension()).to_str()).to_ascii_lowercase() == extension
})
}