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
use anyhow::{anyhow, bail};
use std::path::Path;
pub const MOVE_EXTENSION: &str = "move";
pub const MOVE_IR_EXTENSION: &str = "mvir";
pub const MOVE_COMPILED_EXTENSION: &str = "mv";
pub const SOURCE_MAP_EXTENSION: &str = "mvsm";
pub const MOVE_ERROR_DESC_EXTENSION: &str = "errmap";
pub fn find_filenames<Predicate: FnMut(&Path) -> bool>(
paths: &[impl AsRef<Path>],
mut is_file_desired: Predicate,
) -> anyhow::Result<Vec<String>> {
let mut result = vec![];
for s in paths {
let path = s.as_ref();
if !path.exists() {
bail!("No such file or directory '{}'", path.to_string_lossy())
}
if path.is_file() && is_file_desired(path) {
result.push(path_to_string(path)?);
continue;
}
if !path.is_dir() {
continue;
}
for entry in walkdir::WalkDir::new(path)
.into_iter()
.filter_map(|e| e.ok())
{
let entry_path = entry.path();
if !entry.file_type().is_file() || !is_file_desired(entry_path) {
continue;
}
result.push(path_to_string(entry_path)?);
}
}
Ok(result)
}
pub fn find_move_filenames(
paths: &[impl AsRef<Path>],
keep_specified_files: bool,
) -> anyhow::Result<Vec<String>> {
if keep_specified_files {
let (file_paths, other_paths): (Vec<&Path>, Vec<&Path>) =
paths.iter().map(|p| p.as_ref()).partition(|s| s.is_file());
let mut files = file_paths
.into_iter()
.map(path_to_string)
.collect::<anyhow::Result<Vec<String>>>()?;
files.extend(find_filenames(&other_paths, |path| {
extension_equals(path, MOVE_EXTENSION)
})?);
Ok(files)
} else {
find_filenames(paths, |path| extension_equals(path, MOVE_EXTENSION))
}
}
pub fn path_to_string(path: &Path) -> anyhow::Result<String> {
match path.to_str() {
Some(p) => Ok(p.to_string()),
None => Err(anyhow!("non-Unicode file name")),
}
}
pub fn extension_equals(path: &Path, target_ext: &str) -> bool {
match path.extension().and_then(|s| s.to_str()) {
Some(extension) => extension == target_ext,
None => false,
}
}