Force sort order for FileIter (now ordered by file name)

This commit is contained in:
NGnius (Graham) 2022-01-05 12:42:31 -05:00
parent c11b681d56
commit bb575d705d

View file

@ -1,5 +1,5 @@
use std::fmt::{Debug, Display, Error, Formatter}; use std::fmt::{Debug, Display, Error, Formatter};
use std::fs::ReadDir; use std::fs::{ReadDir, DirEntry};
use std::iter::Iterator; use std::iter::Iterator;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -18,10 +18,49 @@ pub struct FileIter {
root: PathBuf, root: PathBuf,
pattern: Regex, pattern: Regex,
recursive: bool, recursive: bool,
dir_iters: Vec<ReadDir>, dir_iters: Vec<SortedReadDir>,
is_complete: bool, is_complete: bool,
} }
#[derive(Debug)]
struct SortedReadDir {
dir_iter: ReadDir,
dir_iter_complete: bool,
cache: Vec<DirEntry>,
}
impl Iterator for SortedReadDir {
type Item = std::io::Result<DirEntry>;
fn next(&mut self) -> Option<Self::Item> {
if !self.dir_iter_complete {
while let Some(dir) = self.dir_iter.next() {
match dir {
Ok(f) => self.cache.push(f),
Err(e) => return Some(Err(e)),
}
}
self.dir_iter_complete = true;
self.cache.sort_by(|a, b| b.path().cmp(&a.path()));
}
if self.cache.is_empty() {
None
} else {
Some(Ok(self.cache.pop().unwrap()))
}
}
}
impl std::convert::From<ReadDir> for SortedReadDir {
fn from(item: ReadDir) -> Self {
Self {
dir_iter: item,
dir_iter_complete: false,
cache: Vec::new(),
}
}
}
impl Display for FileIter { impl Display for FileIter {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!( write!(
@ -51,7 +90,7 @@ impl FileIter {
line: 0, line: 0,
op: op(), op: op(),
msg: format!("Directory read error: {}", e), msg: format!("Directory read error: {}", e),
})?); })?.into());
vec vec
} else { } else {
Vec::with_capacity(DEFAULT_VEC_CACHE_SIZE) Vec::with_capacity(DEFAULT_VEC_CACHE_SIZE)
@ -73,7 +112,7 @@ impl FileIter {
let root_path = crate::lang::utility::music_folder(); let root_path = crate::lang::utility::music_folder();
let read_dir = root_path.read_dir().unwrap(); let read_dir = root_path.read_dir().unwrap();
let mut dir_vec = Vec::with_capacity(DEFAULT_VEC_CACHE_SIZE); let mut dir_vec = Vec::with_capacity(DEFAULT_VEC_CACHE_SIZE);
dir_vec.push(read_dir); dir_vec.push(read_dir.into());
Self { Self {
root: root_path, root: root_path,
pattern: Regex::new(DEFAULT_REGEX).unwrap(), pattern: Regex::new(DEFAULT_REGEX).unwrap(),
@ -215,7 +254,7 @@ impl Iterator for FileIter {
} else { } else {
// should be impossible to get here // should be impossible to get here
self.dir_iters.push(match self.root.read_dir() { self.dir_iters.push(match self.root.read_dir() {
Ok(x) => x, Ok(x) => x.into(),
Err(e) => return Some(Err(format!("Directory read error: {}", e))), Err(e) => return Some(Err(format!("Directory read error: {}", e))),
}); });
return self.next(); return self.next();
@ -230,7 +269,7 @@ impl Iterator for FileIter {
if self.recursive { if self.recursive {
self.dir_iters.push(dir_iter); self.dir_iters.push(dir_iter);
self.dir_iters.push(match dir_entry.path().read_dir() { self.dir_iters.push(match dir_entry.path().read_dir() {
Ok(x) => x, Ok(x) => x.into(),
Err(e) => { Err(e) => {
return Some(Err(format!( return Some(Err(format!(
"Directory read error: {}", "Directory read error: {}",