This crate provides cross platform matching for globs with relative path prefixes.
For CLI utilities it can be a common pattern to operate on a set of files. Such a set of files
is either provided directly, as parameter to the tool - or via configuration files. The use of
a configuration file makes it easier to determine the location of a file since the path
can be specified relative to the configuration. Consider, e.g., the following .json input:
{
"globs": [
"../../../some/text-files/**/*.txt",
"other/inputs/*.md",
"paths/from/dir[0-9]/*.*"
]
}
Specifying these paths in a dedicated configuration file allows to resolve the paths independent of the invocation of the script operating on these files, the location of the configuration file is used as base directory.
This crate combines the features of the existing crates globset and walkdir to implement a relative glob matcher:
- A [
Builder] is created for each glob in the same style as inglobset::Glob. - A [
Matcher] is created from the [Builder] using [Builder::build]. This call resolves the relative path components within the glob by "moving" it to the specified root directory. - The [
Matcher] is then transformed into an iterator yieldingpath::PathBuf.
For the previous example it would be sufficient to use one builder per glob and to specify the root folder when building the pattern (see examples below).
Example: A simple match.
The following example uses the files stored in the test-files folder, we're trying to match
all the .txt files using the glob test-files/**/*.txt (where test-files is the only
relative path component).
use globmatch;
#
# example_a.unwrap;
Example: Specifying options and using .filter_entry.
Similar to the builder pattern in globset when using globset::GlobBuilder, this
crate allows to pass options (currently just case sensitivity) to the builder.
In addition, the filter_entry function from walkdir is accessible,
but only as a single call (this crate does not implement a recursive iterator). This function
allows filter files and folders before matching against the provided glob and therefore
to efficiently exclude files and folders, e.g., hidden folders:
use globmatch;
#
# example_b.unwrap;
Example: Filtering with .build_glob.
The above examples demonstrated how to search for paths using this crate. Two more builder functions are available for additional matching on the paths yielded by the iterator, e.g., to further limit the files (e.g., based on a global blacklist).
- [
Builder::build_glob] to create a single [Glob] (caution: the builder only checks that the pattern is not empty, but allows absolute paths). - [
Builder::build_glob_set] to create a [Glob] matcher that contains two globs[glob, **/glob]out of the specifiedglobparameter of [Builder::new]. The pattern must not be an absolute path.
use globmatch;
#
# example_c.unwrap;