use std::io;
use std_prelude::*;
use path_abs::{PathDir, PathFile, PathType};
use glob_crate;
pub type GlobOptions = glob_crate::MatchOptions;
pub type GlobPatternError = glob_crate::PatternError;
#[inline(always)]
pub fn glob(pattern: &str) -> Result<GlobPathTypes, GlobPatternError> {
GlobPathTypes::new(pattern)
}
#[inline(always)]
pub fn glob_with(pattern: &str, options: &GlobOptions) -> Result<GlobPathTypes, GlobPatternError> {
GlobPathTypes::with(pattern, options)
}
pub struct GlobPathTypes {
paths: glob_crate::Paths,
}
pub struct GlobPathFiles {
types: GlobPathTypes,
}
pub struct GlobPathDirs {
types: GlobPathTypes,
}
impl GlobPathTypes {
#[inline(always)]
fn new(pattern: &str) -> Result<GlobPathTypes, glob_crate::PatternError> {
Ok(GlobPathTypes {
paths: glob_crate::glob(pattern)?,
})
}
#[inline(always)]
fn with(
pattern: &str,
options: &GlobOptions,
) -> Result<GlobPathTypes, glob_crate::PatternError> {
Ok(GlobPathTypes {
paths: glob_crate::glob_with(pattern, options)?,
})
}
#[inline(always)]
pub fn files(self) -> GlobPathFiles {
GlobPathFiles { types: self }
}
#[inline(always)]
pub fn dirs(self) -> GlobPathDirs {
GlobPathDirs { types: self }
}
}
impl Iterator for GlobPathTypes {
type Item = io::Result<PathType>;
fn next(&mut self) -> Option<io::Result<PathType>> {
if let Some(result) = self.paths.next() {
match result {
Ok(path) => Some(PathType::new(path).map_err(|err| err.into())),
Err(err) => Some(Err(io::Error::new(err.error().kind(), err))),
}
} else {
None
}
}
}
impl Iterator for GlobPathFiles {
type Item = io::Result<PathFile>;
fn next(&mut self) -> Option<io::Result<PathFile>> {
loop {
match self.types.next() {
Some(Ok(ty)) => match ty {
PathType::File(file) => return Some(Ok(file)),
PathType::Dir(_) => {}
},
Some(Err(err)) => return Some(Err(err)),
None => return None,
}
}
}
}
impl Iterator for GlobPathDirs {
type Item = io::Result<PathDir>;
fn next(&mut self) -> Option<io::Result<PathDir>> {
loop {
match self.types.next() {
Some(Ok(ty)) => match ty {
PathType::Dir(dir) => return Some(Ok(dir)),
PathType::File(_) => {}
},
Some(Err(err)) => return Some(Err(err)),
None => return None, }
}
}
}