-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfind.rs
36 lines (32 loc) · 1.04 KB
/
find.rs
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
use std::path::PathBuf;
use walkdir::{DirEntry, WalkDir};
fn is_hidden(entry: &DirEntry) -> bool {
entry
.file_name()
.to_str()
.map(|s| s.starts_with('.'))
.unwrap_or(false)
}
fn is_nix_file(entry: &DirEntry) -> bool {
entry
.file_name()
.to_str()
.map(|s| s.ends_with(".nix"))
.unwrap_or(false)
}
pub fn find_nix_files(path: &PathBuf) -> Vec<String> {
WalkDir::new(path)
.into_iter()
.filter_entry(|e| !is_hidden(e))
.map_while(Result::ok)
.filter(is_nix_file)
.filter(|path| path.metadata().is_ok())
// pkgs/test/nixpkgs-check-by-name/tests/symlink-invalid/pkgs/by-name/fo/foo/foo.nix
// is a broken symlink.
.filter(|path| !path.path_is_symlink())
// 'pkgs/test/nixpkgs-check-by-name/tests/package-nix-dir/pkgs/by-name/fo/foo/package.nix'
// is a directory.
.filter(|path| (!path.metadata().unwrap().is_dir()))
.map(|f| f.path().to_str().unwrap().to_owned())
.collect()
}