diff options
author | Lars Wirzenius <liw@liw.fi> | 2018-10-02 09:30:37 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2018-10-02 09:30:37 +0300 |
commit | 8e186e739f133297cb2dd4761dbfae5566f7b75e (patch) | |
tree | d6423b14a7ad5836342ee92720c45c9e9b8057a8 /src | |
parent | 6e9650e14d92bb114345553cabc5ba6ec5070193 (diff) | |
download | summainrs-8e186e739f133297cb2dd4761dbfae5566f7b75e.tar.gz |
Change: use the walkdir crate instead of my own code
Diffstat (limited to 'src')
-rw-r--r-- | src/fswalk.rs | 125 |
1 files changed, 0 insertions, 125 deletions
diff --git a/src/fswalk.rs b/src/fswalk.rs deleted file mode 100644 index d9ecb87..0000000 --- a/src/fswalk.rs +++ /dev/null @@ -1,125 +0,0 @@ -use std::fs; -use std::io; -use std::path::PathBuf; -use std::cmp::Ordering; - -// A filesystem entry: directory, file, symlink, etc -#[derive(Debug)] -pub struct Entry { - path: PathBuf, - meta: fs::Metadata, -} - -impl Entry { - fn new(path: &PathBuf, meta: &fs::Metadata) -> Entry { - Entry { - path: path.clone(), - meta: meta.clone(), - } - } - - pub fn path(&self) -> String { - self.path.to_string_lossy().to_string() - } - - pub fn metadata(&self) -> fs::Metadata { - self.meta.clone() - } -} - -impl Eq for Entry {} - -impl PartialEq for Entry { - fn eq(&self, other: &Entry) -> bool { - self.path == other.path - } -} - -impl Ord for Entry { - fn cmp(&self, other: &Entry) -> Ordering { - self.path.cmp(&other.path) - } -} - -impl PartialOrd for Entry { - fn partial_cmp(&self, other: &Entry) -> Option<Ordering> { - Some(self.path.cmp(&other.path)) - } -} - -// Keep two stacks of Entry values, sorted in ascending order. The -// first stack has non-directories. When we iterate, we return a -// directory, then of its non-dirs, then recurse each of its subdirs. -#[derive(Debug)] -pub struct DirTree { - entries: Vec<Entry>, - dirs: Vec<Entry>, -} - -impl DirTree { - pub fn new(path: &str) -> DirTree { - let mut dt = DirTree::empty(); - let meta = fs::symlink_metadata(path).expect("can't stat dir"); - dt.add_dir(&PathBuf::from(path), &meta); - dt - } - - fn empty() -> DirTree { - DirTree { - entries: Vec::new(), - dirs: Vec::new(), - } - } - - fn add_dir(&mut self, path: &PathBuf, meta: &fs::Metadata) { - let entry = Entry::new(path, meta); - self.dirs.push(entry); - } - - fn add_entries(&mut self, path: &PathBuf) -> io::Result<()> { - let mut entries = Vec::new(); - let mut dirs = Vec::new(); - for dir_entry in path.read_dir()? { - let dir_entry = dir_entry?; - let metadata = dir_entry.metadata()?; - let entry = Entry::new(&dir_entry.path(), &metadata); - if metadata.is_dir() { - dirs.push(entry); - } else { - entries.push(entry); - } - } - - entries.sort(); - entries.reverse(); - for e in entries { - self.entries.push(e); - } - - dirs.sort(); - dirs.reverse(); - for e in dirs { - self.dirs.push(e); - } - - Ok(()) - } -} - -impl Iterator for DirTree { - type Item = Entry; - - fn next(&mut self) -> Option<Entry> { - if let Some(e) = self.entries.pop() { - Some(e) - } else if let Some(e) = self.dirs.pop() { - if let Ok(_) = self.add_entries(&e.path) { - // We ignore any errors from adding entries. Boo. Not - // sure how I'd propagate errors, from a next method. - } - Some(e) - } else { - None - } - } -} |