1010
1111//! Support for matching file paths against Unix shell style patterns.
1212//!
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).
1917//!
2018//! For consistency across platforms, and for Windows support, this module
2119//! is implemented entirely in Rust rather than deferring to the libc
2220//! `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+ //! ```
2355
2456#![ doc( html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png" ,
2557 html_favicon_url = "https://www.rust-lang.org/favicon.ico" ,
2658 html_root_url = "https://doc.rust-lang.org/glob/" ) ]
59+ #![ deny( missing_docs) ]
2760#![ cfg_attr( all( test, windows) , feature( std_misc) ) ]
2861
2962use std:: ascii:: AsciiExt ;
3063use std:: cmp;
3164use std:: fmt;
3265use std:: fs;
33- use std:: io:: prelude:: * ;
3466use std:: io;
3567use std:: path:: { self , Path , PathBuf , Component } ;
3668use std:: str:: FromStr ;
@@ -58,8 +90,8 @@ pub struct Paths {
5890 scope : Option < PathBuf > ,
5991}
6092
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.
6395///
6496/// This may return an error if the pattern is invalid.
6597///
@@ -76,12 +108,12 @@ pub struct Paths {
76108///
77109/// See the `Paths` documentation for more information.
78110///
79- /// # Example
111+ /// # Examples
80112///
81113/// Consider a directory `/media/pictures` containing only the files
82114/// `kittens.jpg`, `puppies.jpg` and `hamsters.gif`:
83115///
84- /// ```rust
116+ /// ```rust,no_run
85117/// use glob::glob;
86118///
87119/// for entry in glob("/media/pictures/*.jpg").unwrap() {
@@ -113,13 +145,13 @@ pub struct Paths {
113145/// println!("{}", path.display());
114146/// }
115147/// ```
116- ///
148+ /// Paths are yielded in alphabetical order.
117149pub fn glob ( pattern : & str ) -> Result < Paths , PatternError > {
118150 glob_with ( pattern, & MatchOptions :: new ( ) )
119151}
120152
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.
123155///
124156/// This may return an error if the pattern is invalid.
125157///
@@ -224,7 +256,7 @@ pub fn glob_with(pattern: &str, options: &MatchOptions) -> Result<Paths, Pattern
224256///
225257/// This is typically returned when a particular path cannot be read
226258/// 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.
228260#[ derive( Debug ) ]
229261pub struct GlobError {
230262 path : PathBuf ,
@@ -347,7 +379,7 @@ impl Iterator for Paths {
347379 if self . dir_patterns [ idx] . matches_with ( {
348380 match path. file_name ( ) . and_then ( |s| s. to_str ( ) ) {
349381 // 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
351383 // against a *
352384 None => continue ,
353385 Some ( x) => x
@@ -398,24 +430,24 @@ impl fmt::Display for PatternError {
398430
399431/// A compiled Unix shell style pattern.
400432///
401- /// `?` matches any single character
433+ /// - `?` matches any single character.
402434///
403- /// `*` matches any (possibly empty) sequence of characters
435+ /// - `*` matches any (possibly empty) sequence of characters.
404436///
405- /// `**` matches the current directory and arbitrary subdirectories. This
437+ /// - `**` matches the current directory and arbitrary subdirectories. This
406438/// sequence **must** form a single path component, so both `**a` and `b**` are
407439/// invalid and will result in an error. A sequence of more than two
408440/// consecutive `*` characters is also invalid.
409441///
410- /// `[...]` matches any character inside the brackets.
442+ /// - `[...]` matches any character inside the brackets.
411443/// Character sequences can also specify ranges
412444/// of characters, as ordered by Unicode, so e.g. `[0-9]` specifies any
413445/// character between 0 and 9 inclusive. An unclosed bracket is invalid.
414446///
415- /// `[!...]` is the negation of `[...]`, i.e. it matches any characters **not**
447+ /// - `[!...]` is the negation of `[...]`, i.e. it matches any characters **not**
416448/// in the brackets.
417449///
418- /// The metacharacters `?`, `*`, `[`, `]` can be matched by using brackets
450+ /// - The metacharacters `?`, `*`, `[`, `]` can be matched by using brackets
419451/// (e.g. `[?]`). When a `]` occurs immediately following `[` or `[!` then
420452/// it is interpreted as being part of, rather then ending, the character
421453/// set, so `]` and NOT `]` can be matched by `[]]` and `[!]]` respectively.
@@ -474,7 +506,7 @@ const ERROR_INVALID_RANGE: &'static str = "invalid range pattern";
474506impl Pattern {
475507 /// This function compiles Unix shell style patterns.
476508 ///
477- /// An invalid glob pattern will yield an error .
509+ /// An invalid glob pattern will yield a `PatternError` .
478510 pub fn new ( pattern : & str ) -> Result < Pattern , PatternError > {
479511
480512 let chars = pattern. chars ( ) . collect :: < Vec < _ > > ( ) ;
@@ -614,7 +646,7 @@ impl Pattern {
614646 /// Return if the given `str` matches this `Pattern` using the default
615647 /// match options (i.e. `MatchOptions::new()`).
616648 ///
617- /// # Example
649+ /// # Examples
618650 ///
619651 /// ```rust
620652 /// use glob::Pattern;
@@ -893,7 +925,7 @@ fn chars_eq(a: char, b: char, case_sensitive: bool) -> bool {
893925}
894926
895927
896- /// Configuration options to modify the behaviour of `Pattern::matches_with(..)`
928+ /// Configuration options to modify the behaviour of `Pattern::matches_with(..)`.
897929#[ allow( missing_copy_implementations) ]
898930#[ derive( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Default ) ]
899931pub struct MatchOptions {
@@ -903,13 +935,13 @@ pub struct MatchOptions {
903935 /// Unicode.
904936 pub case_sensitive : bool ,
905937
906- /// If this is true then path-component separator characters (e.g. `/` on
938+ /// Whether or not path-component separator characters (e.g. `/` on
907939 /// Posix) must be matched by a literal `/`, rather than by `*` or `?` or
908- /// `[...]`
940+ /// `[...]`.
909941 pub require_literal_separator : bool ,
910942
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; `*`, `?`, `**`,
913945 /// or `[...]` will not match. This is useful because such files are
914946 /// conventionally considered hidden on Unix systems and it might be
915947 /// desirable to skip them when listing files.
0 commit comments