Skip to content

Commit 050faeb

Browse files
committed
Revamped file loading
1 parent 5bd5927 commit 050faeb

File tree

7 files changed

+280
-146
lines changed

7 files changed

+280
-146
lines changed

config/self.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# this is the config file for namelint itself
3+
#
4+
dirs: [ "." ]
5+
tests:
6+
- paths:
7+
- "**/*"
8+
rules:
9+
- no-msdos-reserved
10+
- no-whitespace
11+
rulesets:
12+
- linux

run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ fi
2424
cargo install --locked --quiet --version 3.12.0 bacon
2525

2626
# run the app
27-
bacon --headless run -- --bin namelint -- --rules ./test/custom_rules.yaml --rules ./test/alt_rules.yaml ./src ./docs
27+
bacon --headless run -- --bin namelint -- --rules ./test/custom_rules.yaml --config ./config/self.yaml

src/load_dir.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
2+
use std::{collections::VecDeque, fs, path::{Path, PathBuf}};
3+
4+
use crate::structs::FileData;
5+
6+
pub fn load_dir(passed_dir:String, files: &mut Vec<FileData>) -> bool {
7+
8+
let mut dir = passed_dir;
9+
10+
if dir == "." {
11+
println!("DEBUG: using current directory");
12+
let current_dir = std::env::current_dir();
13+
if current_dir.is_err() {
14+
println!("ERROR: Unable to get current directory: {}", current_dir.err().unwrap());
15+
return false;
16+
}
17+
let current_dir = current_dir.unwrap();
18+
let current_dir = current_dir.to_str();
19+
if current_dir.is_none() {
20+
println!("ERROR: Unable to convert current directory to string");
21+
return false;
22+
}
23+
dir = current_dir.unwrap().to_string();
24+
} else if dir == "" {
25+
println!("WARNING: skipping empty directory name");
26+
return true;
27+
}
28+
let path = Path::new(&dir);
29+
if path.exists() == false {
30+
println!("ERROR: path does not exist: '{}'", dir);
31+
return false;
32+
}
33+
if path.is_dir() == false {
34+
//DISCUSS: maybe a warning and process it?
35+
println!("ERROR: path is not a directory: '{}'", dir);
36+
return false;
37+
}
38+
39+
let cpath = path.canonicalize();
40+
if cpath.is_err() {
41+
println!("ERROR: Unable to canonicalize directory {}: {}", dir, cpath.err().unwrap());
42+
return false;
43+
}
44+
let cpath = cpath.unwrap();
45+
if cpath != path {
46+
println!("WARNING: canonicalized path is different: '{}'", cpath.to_string_lossy());
47+
}
48+
49+
let mut path_queue:VecDeque::<PathBuf> = VecDeque::new();
50+
path_queue.push_back(path.to_path_buf());
51+
52+
while path_queue.len() > 0 {
53+
let next_path = path_queue.pop_front();
54+
if next_path.is_none() {
55+
break;
56+
}
57+
let next_path = next_path.unwrap();
58+
59+
let new_paths = visit_dir(&next_path, files);
60+
61+
for new_path in new_paths.iter() {
62+
path_queue.push_back(new_path.clone());
63+
}
64+
}
65+
return true;
66+
}
67+
68+
fn fatal_file(dir: &PathBuf, message: &str) -> FileData {
69+
return FileData {
70+
path: dir.to_path_buf(),
71+
passed: Vec::new(),
72+
failed: vec![message.to_string()],
73+
fatal: true,
74+
};
75+
}
76+
77+
78+
/* load all files in a directory, and return a list (possibly empty) of subdirectories */
79+
fn visit_dir(dir: &PathBuf, files: &mut Vec<FileData>) -> Vec<PathBuf> {
80+
81+
let mut new_dirs:Vec::<PathBuf> = Vec::new();
82+
83+
let dir_str = dir.to_str();
84+
if dir_str.is_none() {
85+
files.push(fatal_file(dir, "not-utf8"));
86+
return new_dirs;
87+
}
88+
89+
let path = dir.as_path();
90+
91+
if path.exists() == false {
92+
// this should never occur, unless someone messed with the directory while we were reading it
93+
files.push(fatal_file(dir, "does-not-exist"));
94+
return new_dirs;
95+
}
96+
97+
if path.is_dir() == false {
98+
// this should never occur, unless someone messed with the directory while we were reading it
99+
files.push(fatal_file(dir, "not-a-directory"));
100+
return new_dirs;
101+
}
102+
103+
let dir_entry = fs::read_dir(path);
104+
if dir_entry.is_err() {
105+
files.push(fatal_file(dir, "unable-to-read-directory"));
106+
return new_dirs;
107+
}
108+
let dir_entry = dir_entry.unwrap();
109+
110+
for entry in dir_entry {
111+
if entry.is_err() {
112+
println!("ERROR: Unable to read directory entry: {}", entry.err().unwrap());
113+
//LATER: should dir be something else?
114+
files.push(fatal_file(dir, "unable-to-read-directory-entry"));
115+
continue;
116+
}
117+
let entry = entry.unwrap();
118+
let entry_path = entry.path();
119+
if entry_path.is_dir() {
120+
new_dirs.push(entry_path);
121+
} else {
122+
let file_data = FileData {
123+
path: entry_path.clone(),
124+
passed: Vec::new(),
125+
failed: Vec::new(),
126+
fatal: false,
127+
};
128+
files.push(file_data);
129+
}
130+
}
131+
return new_dirs;
132+
}

0 commit comments

Comments
 (0)