-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathfile_entry.rs
More file actions
216 lines (182 loc) · 5.51 KB
/
Copy pathfile_entry.rs
File metadata and controls
216 lines (182 loc) · 5.51 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::fs::DirEntry;
use std::path::{Path, PathBuf};
use std::{fs, io};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct FileEntry {
pub name: String,
pub ext: String,
pub is_dir: bool,
pub path: String,
pub relative: String,
pub children: Vec<FileEntry>,
}
impl Default for FileEntry {
fn default() -> Self {
FileEntry {
name: "".to_string(),
ext: "".to_string(),
is_dir: false,
path: "".to_string(),
relative: "".to_string(),
children: vec![],
}
}
}
#[allow(dead_code)]
impl FileEntry {
pub fn new(relative: &Path ,path: &Path) -> Self {
let mut ext = "".to_string();
let mut is_dir = true;
if relative.is_file() {
is_dir = false;
if let Some(ex) = path.extension() {
ext = ex.to_string_lossy().to_string()
}
}
let parent = relative.parent().expect("not parent");
let short = relative.strip_prefix(parent).expect("strip parent issue");
FileEntry {
name: format!("{}", short.display()),
ext,
is_dir,
path: format!("{}", path.display()),
relative: format!("{}", relative.display()),
children: vec![],
}
}
/// add FileEntry to parent by keys, and return new results.
pub fn add_child(&mut self, child_key: &str, child: &mut Vec<Self>) {
self.children.iter_mut()
.for_each(|entry| {
if entry.name == child_key {
entry.children.append(child)
}
});
}
/// add FileEntry to parent by keys, and return new results.
pub fn build_child(&mut self, child: &str) {
let root_dir = PathBuf::from(self.path.clone());
self.children.iter_mut()
.for_each(|entry| {
if entry.name == child {
let child_path = PathBuf::from(entry.path.clone());
let mut src_entries = FileEntry::level_one_with_root(&child_path, &root_dir);
entry.children.append(&mut src_entries.children);
}
});
}
// todo: a tempory ignore ways for performance simple
pub fn is_rust_target() {}
fn is_hidden(entry: &DirEntry) -> bool {
if !entry.path().is_dir() {
return entry
.file_name()
.to_str()
.map(|s| s == ".DS_Store")
.unwrap_or(false);
}
entry
.file_name()
.to_str()
.map(|s| s.starts_with("."))
.unwrap_or(false)
}
/// Returns depth 1 FileEntry with the given path.
///
/// # Arguments
///
/// * `title` - default path name
/// * `path` - code path
///
pub fn level_one(path: &Path) -> FileEntry {
let root_dir = path;
FileEntry::level_one_with_root(path, root_dir)
}
fn level_one_with_root(child: &Path, root_dir: &Path) -> FileEntry {
let mut root = FileEntry::new(child, root_dir);
let _result = FileEntry::by_depth_one(child, &mut root, root_dir);
root.children.sort_by_key(|a| {
!a.is_dir
});
root
}
fn by_depth_one(
dir: &Path,
node: &mut FileEntry,
base_dir: &Path,
) -> io::Result<()> {
if dir.is_dir() {
let entry_set = fs::read_dir(dir)?; // contains DirEntry
let mut entries = entry_set
.filter_map(|v| match v {
Ok(dir) => {
if FileEntry::is_hidden(&dir) {
return None;
}
Some(dir)
}
Err(_) => None,
})
.collect::<Vec<_>>();
entries.sort_by(|a, b| a.path().file_name().cmp(&b.path().file_name()));
for (_index, entry) in entries.iter().enumerate() {
let path = entry.path();
let relative = path.strip_prefix(base_dir).unwrap();
if path.is_dir() {
let entry = &mut FileEntry::new(relative, &path);
node.children.push(entry.to_owned());
} else {
let file = &mut FileEntry::new(relative, &path);
file.is_dir = false;
node.children.push(file.to_owned());
}
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use crate::file_entry::FileEntry;
#[test]
fn should_support_for_visitor_by_level() {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let entry = FileEntry::level_one(&path);
assert_eq!("src", entry.children[0].name);
assert_eq!(0, entry.children[0].children.len());
}
#[test]
fn should_support_for_add_children() {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let src = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src");
let mut root = FileEntry::level_one(&path);
let mut src_entries = FileEntry::level_one(&src);
root.add_child("src", &mut src_entries.children);
assert_eq!("src", root.children[0].name);
assert_eq!(3, root.children[0].children.len());
assert_eq!("domain", root.children[0].children[0].name);
}
#[test]
fn should_support_for_insert_children() {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let mut root = FileEntry::level_one(&path);
root.build_child("src");
assert_eq!("src", root.children[0].name);
assert_eq!(3, root.children[0].children.len());
assert_eq!("domain", root.children[0].children[0].name);
}
#[test]
fn should_get_realtive_dir() {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let mut root = FileEntry::level_one(&path);
root.build_child("src");
println!("{:?}", root);
assert_eq!("src", root.children[0].name);
assert_eq!(3, root.children[0].children.len());
let relative = root.children[0].children[0].relative.clone();
assert!(relative.contains("src"));
assert!(relative.contains("domain"));
}
}