10
10
11
11
//! Support for matching file paths against Unix shell style patterns.
12
12
//!
13
- //! The `glob` and `glob_with` functions, in concert with the `Paths`
14
- //! type, allow querying the filesystem for all files that match a particular
15
- //! pattern - just like the libc `glob` function (for an example see the `glob`
16
- //! documentation). The methods on the `Pattern` type provide functionality
17
- //! for checking if individual paths match a particular pattern, in a similar
18
- //! manner to the libc `fnmatch` function.
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).
19
17
//!
20
18
//! For consistency across platforms, and for Windows support, this module
21
19
//! is implemented entirely in Rust rather than deferring to the libc
22
20
//! `glob`/`fnmatch` functions.
21
+ //!
22
+ //! # Examples
23
+ //!
24
+ //! To print all jpg files in `/media/` and all of its subdirectories.
25
+ //!
26
+ //! ```rust,no_run
27
+ //! use glob::glob;
28
+ //!
29
+ //! for entry in glob("/media/**/*.jpg").expect("Failed to read glob pattern") {
30
+ //! match entry {
31
+ //! Ok(path) => println!("{:?}", path.display()),
32
+ //! Err(e) => println!("{:?}", e),
33
+ //! }
34
+ //! }
35
+ //! ```
36
+ //!
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.
39
+ //!
40
+ //! ```rust,no_run
41
+ //! use glob::glob_with;
42
+ //! use glob::MatchOptions;
43
+ //!
44
+ //! let options = MatchOptions {
45
+ //! case_sensitive: false,
46
+ //! require_literal_separator: false,
47
+ //! require_literal_leading_dot: false,
48
+ //! };
49
+ //! for entry in glob_with("local/*a*", &options).expect("Failed to read glob pattern") {
50
+ //! if let Ok(path) = entry {
51
+ //! println!("{:?}", path.display())
52
+ //! }
53
+ //! }
54
+ //! ```
23
55
24
56
#![ doc( html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png" ,
25
57
html_favicon_url = "https://www.rust-lang.org/favicon.ico" ,
26
58
html_root_url = "https://doc.rust-lang.org/glob/" ) ]
59
+ #![ deny( missing_docs) ]
27
60
#![ cfg_attr( all( test, windows) , feature( std_misc) ) ]
28
61
29
62
use std:: ascii:: AsciiExt ;
30
63
use std:: cmp;
31
64
use std:: fmt;
32
65
use std:: fs;
33
- use std:: io:: prelude:: * ;
34
66
use std:: io;
35
67
use std:: path:: { self , Path , PathBuf , Component } ;
36
68
use std:: str:: FromStr ;
@@ -58,8 +90,8 @@ pub struct Paths {
58
90
scope : Option < PathBuf > ,
59
91
}
60
92
61
- /// Return an iterator that produces all the Paths that match the given pattern,
62
- /// which may be absolute or relative to the current working directory.
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.
63
95
///
64
96
/// This may return an error if the pattern is invalid.
65
97
///
@@ -76,12 +108,12 @@ pub struct Paths {
76
108
///
77
109
/// See the `Paths` documentation for more information.
78
110
///
79
- /// # Example
111
+ /// # Examples
80
112
///
81
113
/// Consider a directory `/media/pictures` containing only the files
82
114
/// `kittens.jpg`, `puppies.jpg` and `hamsters.gif`:
83
115
///
84
- /// ```rust
116
+ /// ```rust,no_run
85
117
/// use glob::glob;
86
118
///
87
119
/// for entry in glob("/media/pictures/*.jpg").unwrap() {
@@ -113,13 +145,13 @@ pub struct Paths {
113
145
/// println!("{}", path.display());
114
146
/// }
115
147
/// ```
116
- ///
148
+ /// Paths are yielded in alphabetical order.
117
149
pub fn glob ( pattern : & str ) -> Result < Paths , PatternError > {
118
150
glob_with ( pattern, & MatchOptions :: new ( ) )
119
151
}
120
152
121
- /// Return an iterator that produces all the Paths that match the given pattern,
122
- /// which may be absolute or relative to the current working directory.
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.
123
155
///
124
156
/// This may return an error if the pattern is invalid.
125
157
///
@@ -224,7 +256,7 @@ pub fn glob_with(pattern: &str, options: &MatchOptions) -> Result<Paths, Pattern
224
256
///
225
257
/// This is typically returned when a particular path cannot be read
226
258
/// to determine if its contents match the glob pattern. This is possible
227
- /// if the program lacks the permissions, for example.
259
+ /// if the program lacks the appropriate permissions, for example.
228
260
#[ derive( Debug ) ]
229
261
pub struct GlobError {
230
262
path : PathBuf ,
@@ -347,7 +379,7 @@ impl Iterator for Paths {
347
379
if self . dir_patterns [ idx] . matches_with ( {
348
380
match path. file_name ( ) . and_then ( |s| s. to_str ( ) ) {
349
381
// FIXME (#9639): How do we handle non-utf8 filenames?
350
- // Ignore them for now Ideally we'd still match them
382
+ // Ignore them for now; ideally we'd still match them
351
383
// against a *
352
384
None => continue ,
353
385
Some ( x) => x
@@ -398,24 +430,24 @@ impl fmt::Display for PatternError {
398
430
399
431
/// A compiled Unix shell style pattern.
400
432
///
401
- /// `?` matches any single character
433
+ /// - `?` matches any single character.
402
434
///
403
- /// `*` matches any (possibly empty) sequence of characters
435
+ /// - `*` matches any (possibly empty) sequence of characters.
404
436
///
405
- /// `**` matches the current directory and arbitrary subdirectories. This
437
+ /// - `**` matches the current directory and arbitrary subdirectories. This
406
438
/// sequence **must** form a single path component, so both `**a` and `b**` are
407
439
/// invalid and will result in an error. A sequence of more than two
408
440
/// consecutive `*` characters is also invalid.
409
441
///
410
- /// `[...]` matches any character inside the brackets.
442
+ /// - `[...]` matches any character inside the brackets.
411
443
/// Character sequences can also specify ranges
412
444
/// of characters, as ordered by Unicode, so e.g. `[0-9]` specifies any
413
445
/// character between 0 and 9 inclusive. An unclosed bracket is invalid.
414
446
///
415
- /// `[!...]` is the negation of `[...]`, i.e. it matches any characters **not**
447
+ /// - `[!...]` is the negation of `[...]`, i.e. it matches any characters **not**
416
448
/// in the brackets.
417
449
///
418
- /// The metacharacters `?`, `*`, `[`, `]` can be matched by using brackets
450
+ /// - The metacharacters `?`, `*`, `[`, `]` can be matched by using brackets
419
451
/// (e.g. `[?]`). When a `]` occurs immediately following `[` or `[!` then
420
452
/// it is interpreted as being part of, rather then ending, the character
421
453
/// set, so `]` and NOT `]` can be matched by `[]]` and `[!]]` respectively.
@@ -474,7 +506,7 @@ const ERROR_INVALID_RANGE: &'static str = "invalid range pattern";
474
506
impl Pattern {
475
507
/// This function compiles Unix shell style patterns.
476
508
///
477
- /// An invalid glob pattern will yield an error .
509
+ /// An invalid glob pattern will yield a `PatternError` .
478
510
pub fn new ( pattern : & str ) -> Result < Pattern , PatternError > {
479
511
480
512
let chars = pattern. chars ( ) . collect :: < Vec < _ > > ( ) ;
@@ -614,7 +646,7 @@ impl Pattern {
614
646
/// Return if the given `str` matches this `Pattern` using the default
615
647
/// match options (i.e. `MatchOptions::new()`).
616
648
///
617
- /// # Example
649
+ /// # Examples
618
650
///
619
651
/// ```rust
620
652
/// use glob::Pattern;
@@ -893,7 +925,7 @@ fn chars_eq(a: char, b: char, case_sensitive: bool) -> bool {
893
925
}
894
926
895
927
896
- /// Configuration options to modify the behaviour of `Pattern::matches_with(..)`
928
+ /// Configuration options to modify the behaviour of `Pattern::matches_with(..)`.
897
929
#[ allow( missing_copy_implementations) ]
898
930
#[ derive( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Default ) ]
899
931
pub struct MatchOptions {
@@ -903,13 +935,13 @@ pub struct MatchOptions {
903
935
/// Unicode.
904
936
pub case_sensitive : bool ,
905
937
906
- /// If this is true then path-component separator characters (e.g. `/` on
938
+ /// Whether or not path-component separator characters (e.g. `/` on
907
939
/// Posix) must be matched by a literal `/`, rather than by `*` or `?` or
908
- /// `[...]`
940
+ /// `[...]`.
909
941
pub require_literal_separator : bool ,
910
942
911
- /// If this is true then paths that contain components that start with a `.`
912
- /// will not match unless the `.` appears literally in the pattern: `*`, `?`, `**`,
943
+ /// Whether or not paths that contain components that start with a `.`
944
+ /// will require that `.` appears literally in the pattern; `*`, `?`, `**`,
913
945
/// or `[...]` will not match. This is useful because such files are
914
946
/// conventionally considered hidden on Unix systems and it might be
915
947
/// desirable to skip them when listing files.
0 commit comments