-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b0df4a
commit ebf9789
Showing
14 changed files
with
1,013 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use rayon::prelude::*; | ||
use std::fs::{self, FileType}; | ||
use std::io::{Error, Result}; | ||
use std::path::{Path, PathBuf}; | ||
|
||
//use super::WorkContext; | ||
use crate::core::DirEntry; | ||
|
||
pub struct WorkContext<D> | ||
where | ||
D: Delegate, | ||
{ | ||
items: Vec<WorkContextItem<D>>, | ||
} | ||
|
||
enum WorkContextItem<D> | ||
where | ||
D: Delegate, | ||
{ | ||
Item(D::Item), | ||
Work(D::Work), | ||
} | ||
|
||
impl<D> WorkContext<D> | ||
where | ||
D: Delegate, | ||
{ | ||
fn send_item(&mut self, item: D::Item) { | ||
self.items.push(WorkContextItem::Item(item)); | ||
} | ||
fn schedule_work(&mut self, work: D::Work) { | ||
self.items.push(WorkContextItem::Work(work)); | ||
} | ||
} | ||
|
||
pub trait Delegate: Clone + Send { | ||
type State; // don't need... include state in "work" | ||
type Item; | ||
type Work; | ||
|
||
fn process_work(&self, work: Self::Work, context: &mut WorkContext<Self>) -> Result<()>; | ||
|
||
fn handle_error(&self, path: &Path, error: &Error) -> bool; | ||
fn process_entries( | ||
&self, | ||
path: &Path, | ||
state: Self::State, | ||
entries: Vec<DirEntry>, | ||
) -> (Self::State, Vec<DirEntry>); | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct DefaultDelegate {} | ||
|
||
impl Delegate for DefaultDelegate { | ||
type State = usize; | ||
type Item = PathBuf; | ||
type Work = PathBuf; | ||
|
||
fn process_work(&self, work: Self::Work, context: &mut WorkContext<Self>) -> Result<()> { | ||
fs::read_dir(&work)?.for_each(|entry_result| { | ||
let entry = match entry_result { | ||
Ok(entry) => entry, | ||
Err(_) => { | ||
return; | ||
} | ||
}; | ||
|
||
let file_type = match entry.file_type() { | ||
Ok(file_type) => file_type, | ||
Err(_) => { | ||
return; | ||
} | ||
}; | ||
|
||
context.send_item(entry.path()); | ||
|
||
if file_type.is_dir() { | ||
context.schedule_work(entry.path()); | ||
} | ||
}); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn handle_error(&self, path: &Path, error: &Error) -> bool { | ||
eprintln!("{} {}", path.display(), error); | ||
true | ||
} | ||
fn process_entries( | ||
&self, | ||
_path: &Path, | ||
state: Self::State, | ||
mut entries: Vec<DirEntry>, | ||
) -> (Self::State, Vec<DirEntry>) { | ||
entries.par_sort_by(|a, b| a.file_name().cmp(b.file_name())); | ||
(state, entries) | ||
} | ||
} |
Oops, something went wrong.