10
10
11
11
//! Support for matching file paths against Unix shell style patterns.
12
12
//!
13
- //! The `glob` and `glob_with` functions allow querying the filesystem for all files
14
- //! that match a particular pattern (similar to the libc `glob` function). The methods on the
15
- //! `Pattern` type provide functionality for checking if individual paths match a
16
- //! particular pattern (similar to the libc `fnmatch` function).
13
+ //! The `glob` and `glob_with` functions allow querying the filesystem for all
14
+ //! files that match a particular pattern (similar to the libc `glob` function).
15
+ //! The methods on the `Pattern` type provide functionality for checking if
16
+ //! individual paths match a particular pattern (similar to the libc `fnmatch`
17
+ //! function).
17
18
//!
18
19
//! For consistency across platforms, and for Windows support, this module
19
20
//! is implemented entirely in Rust rather than deferring to the libc
34
35
//! }
35
36
//! ```
36
37
//!
37
- //! To print all files containing the letter "a", case insensitive, in a `local` directory
38
- //! relative to the current working directory. This ignores errors instead of printing them.
38
+ //! To print all files containing the letter "a", case insensitive, in a `local`
39
+ //! directory relative to the current working directory. This ignores errors
40
+ //! instead of printing them.
39
41
//!
40
42
//! ```rust,no_run
41
43
//! use glob::glob_with;
46
48
//! require_literal_separator: false,
47
49
//! require_literal_leading_dot: false,
48
50
//! };
49
- //! for entry in glob_with("local/*a*", &options).expect("Failed to read glob pattern" ) {
51
+ //! for entry in glob_with("local/*a*", &options).unwrap( ) {
50
52
//! if let Ok(path) = entry {
51
53
//! println!("{:?}", path.display())
52
54
//! }
@@ -90,8 +92,9 @@ pub struct Paths {
90
92
scope : Option < PathBuf > ,
91
93
}
92
94
93
- /// Return an iterator that produces all the `Path`s that match the given pattern using default
94
- /// match options, which may be absolute or relative to the current working directory.
95
+ /// Return an iterator that produces all the `Path`s that match the given
96
+ /// pattern using default match options, which may be absolute or relative to
97
+ /// the current working directory.
95
98
///
96
99
/// This may return an error if the pattern is invalid.
97
100
///
@@ -150,8 +153,9 @@ pub fn glob(pattern: &str) -> Result<Paths, PatternError> {
150
153
glob_with ( pattern, & MatchOptions :: new ( ) )
151
154
}
152
155
153
- /// Return an iterator that produces all the `Path`s that match the given pattern using the
154
- /// specified match options, which may be absolute or relative to the current working directory.
156
+ /// Return an iterator that produces all the `Path`s that match the given
157
+ /// pattern using the specified match options, which may be absolute or relative
158
+ /// to the current working directory.
155
159
///
156
160
/// This may return an error if the pattern is invalid.
157
161
///
@@ -162,7 +166,8 @@ pub fn glob(pattern: &str) -> Result<Paths, PatternError> {
162
166
/// passed to this function.
163
167
///
164
168
/// Paths are yielded in alphabetical order.
165
- pub fn glob_with ( pattern : & str , options : & MatchOptions ) -> Result < Paths , PatternError > {
169
+ pub fn glob_with ( pattern : & str , options : & MatchOptions )
170
+ -> Result < Paths , PatternError > {
166
171
// make sure that the pattern is valid first, else early return with error
167
172
let _compiled = try!( Pattern :: new ( pattern) ) ;
168
173
@@ -240,7 +245,8 @@ pub fn glob_with(pattern: &str, options: &MatchOptions) -> Result<Paths, Pattern
240
245
} ) ;
241
246
}
242
247
243
- let require_dir = pattern. chars ( ) . next_back ( ) . map ( path:: is_separator) == Some ( true ) ;
248
+ let last_is_separator = pattern. chars ( ) . next_back ( ) . map ( path:: is_separator) ;
249
+ let require_dir = last_is_separator == Some ( true ) ;
244
250
let todo = Vec :: new ( ) ;
245
251
246
252
Ok ( Paths {
@@ -316,7 +322,11 @@ impl Iterator for Paths {
316
322
// Shouldn't happen, but we're using -1 as a special index.
317
323
assert ! ( self . dir_patterns. len( ) < !0 as usize ) ;
318
324
319
- fill_todo ( & mut self . todo , & self . dir_patterns , 0 , & scope, & self . options ) ;
325
+ fill_todo ( & mut self . todo ,
326
+ & self . dir_patterns ,
327
+ 0 ,
328
+ & scope,
329
+ & self . options ) ;
320
330
}
321
331
}
322
332
@@ -370,7 +380,8 @@ impl Iterator for Paths {
370
380
// advanced to the next pattern for this path
371
381
idx = next + 1 ;
372
382
} else {
373
- // not a directory and it's the last pattern, meaning no match
383
+ // not a directory and it's the last pattern, meaning no
384
+ // match
374
385
continue ;
375
386
}
376
387
}
@@ -435,24 +446,24 @@ impl fmt::Display for PatternError {
435
446
/// - `*` matches any (possibly empty) sequence of characters.
436
447
///
437
448
/// - `**` matches the current directory and arbitrary subdirectories. This
438
- /// sequence **must** form a single path component, so both `**a` and `b**` are
439
- /// invalid and will result in an error. A sequence of more than two
440
- /// consecutive `*` characters is also invalid.
449
+ /// sequence **must** form a single path component, so both `**a` and `b**`
450
+ /// are invalid and will result in an error. A sequence of more than two
451
+ /// consecutive `*` characters is also invalid.
441
452
///
442
- /// - `[...]` matches any character inside the brackets.
443
- /// Character sequences can also specify ranges
444
- /// of characters, as ordered by Unicode, so e.g. `[0-9]` specifies any
445
- /// character between 0 and 9 inclusive. An unclosed bracket is invalid.
453
+ /// - `[...]` matches any character inside the brackets. Character sequences
454
+ /// can also specify ranges of characters, as ordered by Unicode, so e.g.
455
+ /// `[0-9]` specifies any character between 0 and 9 inclusive. An unclosed
456
+ /// bracket is invalid.
446
457
///
447
- /// - `[!...]` is the negation of `[...]`, i.e. it matches any characters **not**
448
- /// in the brackets.
458
+ /// - `[!...]` is the negation of `[...]`, i.e. it matches any characters
459
+ /// **not** in the brackets.
449
460
///
450
461
/// - The metacharacters `?`, `*`, `[`, `]` can be matched by using brackets
451
- /// (e.g. `[?]`). When a `]` occurs immediately following `[` or `[!` then
452
- /// it is interpreted as being part of, rather then ending, the character
453
- /// set, so `]` and NOT `]` can be matched by `[]]` and `[!]]` respectively.
454
- /// The `-` character can be specified inside a character sequence pattern by
455
- /// placing it at the start or the end, e.g. `[abc-]`.
462
+ /// (e.g. `[?]`). When a `]` occurs immediately following `[` or `[!` then it
463
+ /// is interpreted as being part of, rather then ending, the character set, so
464
+ /// `]` and NOT `]` can be matched by `[]]` and `[!]]` respectively. The `-`
465
+ /// character can be specified inside a character sequence pattern by placing
466
+ /// it at the start or the end, e.g. `[abc-]`.
456
467
#[ derive( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Default , Debug ) ]
457
468
pub struct Pattern {
458
469
original : String ,
0 commit comments