1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//! Glob methods for [`Path`](https://doc.rust-lang.org/std/path/struct.Path.html) and friends.
//! 
//! * [GitHub repo](https://github.com/flying-sheep/path-glob)

extern crate glob;

use std::path::Path;

use glob::{glob_with,Paths,PatternError,MatchOptions};

/// A trait providing glob methods.
/// 
/// The idea is “glob starting from here”
pub trait Glob {
	/// Glob here with explicit options
	fn glob_with(&self, pattern: &str, options: &MatchOptions) -> Result<Paths, PatternError>;
	
	/// Glob here with default options
	fn glob(&self, pattern: &str) -> Result<Paths, PatternError> {
		self.glob_with(pattern, &MatchOptions::new())
	}
	
	/// Glob inside of here with explicit options (`<here>/**/<pattern>`)
	fn rglob_with(&self, pattern: &str, options: &MatchOptions) -> Result<Paths, PatternError> {
		self.glob_with(&format!("**/{}", pattern), options)
	}
	
	/// Glob inside of here with default options (`<here>/**/<pattern>`)
	fn rglob(&self, pattern: &str) -> Result<Paths, PatternError> {
		self.rglob_with(pattern, &MatchOptions::new())
	}
}

impl<P: AsRef<Path>> Glob for P {
	fn glob_with(&self, pattern: &str, options: &MatchOptions) -> Result<Paths, PatternError> {
		glob_with(self.as_ref().join(pattern).to_str().unwrap(), options)
	}
}