use std::{
io::{Error, ErrorKind},
mem::replace,
path::PathBuf,
};
use walkdir::IntoIter;
use crate::{dir_entry::DirEntry, Diskit};
#[allow(missing_docs)]
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone)]
pub struct WalkdirOptions
{
pub path: PathBuf,
pub min_depth: usize,
pub max_depth: usize,
pub follow_links: bool,
pub max_open: usize,
pub contents_first: bool,
pub same_file_system: bool,
}
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct WalkDir<D>
where
D: Diskit,
{
pub diskit: D,
pub options: WalkdirOptions,
}
#[allow(missing_docs)]
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub struct WalkdirIteratorInner
{
pub walkdir: Option<IntoIter>,
pub val: usize,
pub original: WalkdirOptions,
}
#[allow(missing_docs)]
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub struct WalkdirIterator<D>
where
D: Diskit,
{
pub inner: Result<(WalkdirIteratorInner, D), Error>,
}
impl<D> Iterator for WalkdirIterator<D>
where
D: Diskit,
{
type Item = Result<DirEntry, Error>;
fn next(&mut self) -> Option<Self::Item>
{
let inner = match &mut self.inner
{
Ok(inner) => inner,
Err(err) =>
{
let err = replace(
err,
Error::new(
ErrorKind::Other,
"81421d77ff6267c7de0b99591150bcf908dd116bd4fa561fdf4a232f17cdd7d3",
),
);
if err
.get_ref()
.map_or(false, |x| format!("{:?}", x) == "\"81421d77ff6267c7de0b99591150bcf908dd116bd4fa561fdf4a232f17cdd7d3\"")
{
return None;
}
return Some(Err(err));
}
};
inner.1.walkdir_next_inner(&mut inner.0)
}
}
impl<D> IntoIterator for WalkDir<D>
where
D: Diskit,
{
type Item = Result<DirEntry, Error>;
type IntoIter = WalkdirIterator<D>;
fn into_iter(self) -> Self::IntoIter
{
self.diskit.clone().into_walkdir_iterator(self)
}
}
impl<D> WalkDir<D>
where
D: Diskit,
{
pub fn to_original(&self) -> ::walkdir::WalkDir
{
::walkdir::WalkDir::new(&self.options.path)
.min_depth(self.options.min_depth)
.max_depth(self.options.max_depth)
.follow_links(self.options.follow_links)
.max_open(self.options.max_open)
.contents_first(self.options.contents_first)
.same_file_system(self.options.same_file_system)
}
pub const fn new_default(diskit: D, path: PathBuf) -> Self
{
Self {
diskit,
options: WalkdirOptions {
path,
min_depth: 0,
max_depth: std::usize::MAX,
follow_links: false,
max_open: 10,
contents_first: false,
same_file_system: false,
},
}
}
}
#[allow(clippy::return_self_not_must_use)]
impl<D> WalkDir<D>
where
D: Diskit,
{
pub fn set_min_depth(&mut self, min_depth: usize) -> Self
{
self.options.min_depth = min_depth;
self.clone()
}
pub fn set_max_depth(&mut self, max_depth: usize) -> Self
{
self.options.max_depth = max_depth;
self.clone()
}
pub fn set_follow_links(&mut self, follow_links: bool) -> Self
{
self.options.follow_links = follow_links;
self.clone()
}
pub fn set_max_open(&mut self, max_open: usize) -> Self
{
self.options.max_open = max_open;
self.clone()
}
pub fn set_contents_first(&mut self, contents_first: bool) -> Self
{
self.options.contents_first = contents_first;
self.clone()
}
pub fn set_same_file_system(&mut self, same_file_system: bool) -> Self
{
self.options.same_file_system = same_file_system;
self.clone()
}
}