fix_getters_rules/
dir_entry.rs1use once_cell::sync::Lazy;
4use std::{
5 collections::HashSet,
6 error::Error,
7 fmt::{self, Display},
8 fs::DirEntry,
9 io,
10 path::PathBuf,
11};
12
13pub static EXCLUDED: Lazy<HashSet<&'static str>> = Lazy::new(|| {
15 let mut excluded = HashSet::new();
16 excluded.insert(".git");
17 excluded.insert("auto");
18 excluded.insert("ci");
19 excluded.insert("docs");
20 excluded.insert("gir");
21 excluded.insert("gir-files");
22 excluded.insert("target");
23 excluded.insert("sys");
24 excluded
25});
26
27#[inline]
29pub fn check(entry: &DirEntry) -> Result<CheckOk, CheckError> {
30 let entry_type = entry
31 .file_type()
32 .map_err(|err| CheckError::DirEntry(entry.path(), err))?;
33
34 let entry_name = entry.file_name();
35 let entry_name = match entry_name.to_str() {
36 Some(entry_name) => entry_name,
37 None => return Err(CheckError::Name(entry.path(), entry_name)),
38 };
39
40 if entry_type.is_file() {
41 if entry_name.ends_with(".rs") {
42 return Ok(CheckOk::RustFile);
43 }
44 } else if entry_type.is_dir() {
45 if !EXCLUDED.contains(entry_name) {
46 return Ok(CheckOk::Directory);
47 }
48 } else {
49 return Ok(CheckOk::SkipUnspecified);
50 }
51
52 Ok(CheckOk::Skip(entry_name.to_string()))
53}
54
55#[derive(Debug)]
56pub enum CheckOk {
57 Directory,
58 RustFile,
59 Skip(String),
60 SkipUnspecified,
61}
62
63#[derive(Debug)]
64pub enum CheckError {
65 Name(PathBuf, std::ffi::OsString),
66 DirEntry(PathBuf, io::Error),
67}
68
69impl Display for CheckError {
70 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
71 use CheckError::*;
72
73 match self {
74 Name(path, name) => write!(f, "error converting dir entry name {:?} {:?}", path, name),
75 DirEntry(path, err) => write!(f, "error checking dir entry {:?}: {}", path, err),
76 }
77 }
78}
79
80impl Error for CheckError {}