Crate globset[][src]

The globset crate provides cross platform single glob and glob set matching.

Glob set matching is the process of matching one or more glob patterns against a single candidate path simultaneously, and returning all of the globs that matched. For example, given this set of globs:

*.rs
src/lib.rs
src/**/foo.rs

and a path src/bar/baz/foo.rs, then the set would report the first and third globs as matching.

Example: one glob

This example shows how to match a single glob against a single file path.

use globset::Glob;

let glob = Glob::new("*.rs")?.compile_matcher();

assert!(glob.is_match("foo.rs"));
assert!(glob.is_match("foo/bar.rs"));
assert!(!glob.is_match("Cargo.toml"));

Example: configuring a glob matcher

This example shows how to use a GlobBuilder to configure aspects of match semantics. In this example, we prevent wildcards from matching path separators.

use globset::GlobBuilder;

let glob = GlobBuilder::new("*.rs")
    .literal_separator(true).build()?.compile_matcher();

assert!(glob.is_match("foo.rs"));
assert!(!glob.is_match("foo/bar.rs")); // no longer matches
assert!(!glob.is_match("Cargo.toml"));

Example: match multiple globs at once

This example shows how to match multiple glob patterns at once.

use globset::{Glob, GlobSetBuilder};

let mut builder = GlobSetBuilder::new();
// A GlobBuilder can be used to configure each glob's match semantics
// independently.
builder.add(Glob::new("*.rs")?);
builder.add(Glob::new("src/lib.rs")?);
builder.add(Glob::new("src/**/foo.rs")?);
let set = builder.build()?;

assert_eq!(set.matches("src/bar/baz/foo.rs"), vec![0, 2]);

Syntax

Standard Unix-style glob syntax is supported:

A GlobBuilder can be used to prevent wildcards from matching path separators, or to enable case insensitive matching.

Structs

Candidate

A candidate path for matching.

Error

Represents an error that can occur when parsing a glob pattern.

Glob

Glob represents a successfully parsed shell glob pattern.

GlobBuilder

A builder for a pattern.

GlobMatcher

A matcher for a single pattern.

GlobSet

GlobSet represents a group of globs that can be matched together in a single pass.

GlobSetBuilder

GlobSetBuilder builds a group of patterns that can be used to simultaneously match a file path.

Enums

ErrorKind

The kind of error that can occur when parsing a glob pattern.