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
use std::{fs, io, path};
fn is_regular_file(path: &path::Path) -> Result<bool, io::Error> {
Ok(path.symlink_metadata()?.is_file())
}
fn is_hidden(path: &path::Path) -> bool {
match path.file_name() {
Some(name) => name.to_os_string().to_string_lossy().starts_with('.'),
None => false,
}
}
fn is_json(path: &path::Path) -> bool {
match path.file_name() {
Some(name) => name.to_os_string().to_string_lossy().ends_with(".json"),
None => false,
}
}
pub fn remove_schemas(schemas_dir: &path::Path) -> Result<(), io::Error> {
let file_paths = fs::read_dir(schemas_dir)?
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|path| is_regular_file(path).unwrap_or(false))
.filter(|path| !is_hidden(path))
.filter(|path| is_json(path))
;
for file_path in file_paths {
println!("Removing {:?} …", file_path);
fs::remove_file(file_path)?;
}
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
use std::ffi::OsStr;
use std::path::Path;
#[test]
fn is_hidden_works() {
assert_eq!(is_hidden(Path::new("/foo")), false);
assert_eq!(is_hidden(Path::new("/foo/bar")), false);
assert_eq!(is_hidden(Path::new("/foo/bar.txt")), false);
assert_eq!(is_hidden(Path::new("~foo")), false);
assert_eq!(is_hidden(Path::new("foo")), false);
assert_eq!(is_hidden(Path::new("/.foo")), true);
assert_eq!(is_hidden(Path::new("/foo/.bar")), true);
assert_eq!(is_hidden(Path::new("/foo/.bar.txt")), true);
assert_eq!(is_hidden(Path::new(".foo")), true);
assert_eq!(is_hidden(Path::new("/")), false);
assert_eq!(is_hidden(Path::new("")), false);
#[cfg(any(unix, target_os = "redox"))]
{
use std::os::unix::ffi::OsStrExt;
let non_hidden = OsStr::from_bytes(&[0x66, 0x6f, 0x80, 0x6f]);
assert_eq!(is_hidden(Path::new(non_hidden)), false);
let hidden = OsStr::from_bytes(&[0x2e, 0x66, 0x6f, 0x80, 0x6f]);
assert_eq!(is_hidden(Path::new(hidden)), true);
}
}
#[test]
fn is_json_works() {
assert_eq!(is_json(Path::new("/foo")), false);
assert_eq!(is_json(Path::new("/foo/bar")), false);
assert_eq!(is_json(Path::new("/foo/bar.txt")), false);
assert_eq!(is_json(Path::new("~foo")), false);
assert_eq!(is_json(Path::new("foo")), false);
assert_eq!(is_json(Path::new("foo.json5")), false);
assert_eq!(is_json(Path::new("/.json")), true);
assert_eq!(is_json(Path::new("/foo/.bar.json")), true);
assert_eq!(is_json(Path::new("/foo/bar.json")), true);
assert_eq!(is_json(Path::new("foo.json")), true);
assert_eq!(is_json(Path::new("/")), false);
assert_eq!(is_json(Path::new("")), false);
#[cfg(any(unix, target_os = "redox"))]
{
use std::os::unix::ffi::OsStrExt;
let non_hidden = OsStr::from_bytes(&[0x66, 0x6f, 0x80, 0x6f]);
assert_eq!(is_json(Path::new(non_hidden)), false);
let hidden = OsStr::from_bytes(&[0x66, 0x6f, 0x80, 0x6f, 0x2e, 0x6a, 0x73, 0x6f, 0x6e]);
assert_eq!(is_json(Path::new(hidden)), true);
}
}
}