use crate::runtime::ParallelRuntime;
use crate::walk_types::{DirectoryIdentity, FileSystemId, WalkEntry, WalkError, WalkOptions};
use crate::walker::Walker;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::Arc;
mod parallel;
mod serial;
pub use parallel::ParallelStatefulWalker;
type DirectoryProcessor<R, E> = Arc<
dyn Fn(usize, &Path, &mut R, &mut Vec<Result<StatefulWalkEntry<E>, WalkError>>)
+ Send
+ Sync
+ 'static,
>;
#[derive(Debug)]
pub struct StatefulWalkEntry<E> {
entry: WalkEntry,
pub state: E,
read_children: bool,
}
impl<E> StatefulWalkEntry<E> {
#[must_use]
pub const fn entry(&self) -> &WalkEntry {
&self.entry
}
#[must_use]
pub fn path(&self) -> &Path {
self.entry.path()
}
#[must_use]
pub const fn depth(&self) -> usize {
self.entry.depth()
}
#[must_use]
pub const fn is_file(&self) -> bool {
self.entry.is_file()
}
#[must_use]
pub const fn is_dir(&self) -> bool {
self.entry.is_dir()
}
#[must_use]
pub const fn read_children(&self) -> bool {
self.read_children
}
pub const fn set_read_children(&mut self, enabled: bool) {
self.read_children = enabled && self.entry.is_dir() && self.entry.skip_reason().is_none();
}
#[must_use]
pub fn into_parts(self) -> (WalkEntry, E) {
(self.entry, self.state)
}
}
pub struct StatefulWalkBuilder<R, E> {
root: PathBuf,
options: WalkOptions,
root_read_dir_state: R,
processor: Option<DirectoryProcessor<R, E>>,
parallelism: usize,
runtime: ParallelRuntime,
}
impl<R, E> StatefulWalkBuilder<R, E>
where
R: Clone + Send + 'static,
E: Default + Send + 'static,
{
#[must_use]
pub fn new(root: impl Into<PathBuf>, root_read_dir_state: R) -> Self {
Self {
root: root.into(),
options: WalkOptions::default(),
root_read_dir_state,
processor: None,
parallelism: 0,
runtime: ParallelRuntime::global(),
}
}
#[must_use]
pub const fn options(mut self, options: WalkOptions) -> Self {
self.options = options;
self
}
#[must_use]
pub const fn with_parallelism(mut self, parallelism: usize) -> Self {
self.parallelism = parallelism;
self
}
#[must_use]
pub fn runtime(mut self, runtime: ParallelRuntime) -> Self {
self.runtime = runtime;
self
}
#[must_use]
pub fn process_read_dir<F>(mut self, processor: F) -> Self
where
F: Fn(usize, &Path, &mut R, &mut Vec<Result<StatefulWalkEntry<E>, WalkError>>)
+ Send
+ Sync
+ 'static,
{
self.processor = Some(Arc::new(processor));
self
}
pub fn build(self) -> Result<StatefulWalker<R, E>, WalkError> {
StatefulWalker::new(self)
}
pub fn build_parallel_ordered(
self,
capacity: usize,
) -> Result<ParallelStatefulWalker<E>, WalkError> {
ParallelStatefulWalker::start(self, capacity)
}
}
struct DirectoryTask<R> {
path: PathBuf,
depth: usize,
identity: Option<DirectoryIdentity>,
ancestors: HashSet<DirectoryIdentity>,
read_state: R,
}
struct DirectoryFrame<R, E> {
entries: std::vec::IntoIter<Result<StatefulWalkEntry<E>, WalkError>>,
child_state: R,
ancestors: HashSet<DirectoryIdentity>,
}
pub struct StatefulWalker<R, E> {
root: Arc<PathBuf>,
root_file_system: Option<FileSystemId>,
options: WalkOptions,
processor: Option<DirectoryProcessor<R, E>>,
root_entry: Option<StatefulWalkEntry<E>>,
pending: Option<DirectoryTask<R>>,
frames: Vec<DirectoryFrame<R, E>>,
}