Expand description
The types module provides a way of associating globs on file names to file types.
This can be used to match specific types of files. For example, among
the default file types provided, the Rust file type is defined to be *.rs
with name rust
. Similarly, the C file type is defined to be *.{c,h}
with
name c
.
Note that the set of default types may change over time.
Example
This shows how to create and use a simple file type matcher using the default file types defined in this crate.
use ignore::types::TypesBuilder;
let mut builder = TypesBuilder::new();
builder.add_defaults();
builder.select("rust");
let matcher = builder.build().unwrap();
assert!(matcher.matched("foo.rs", false).is_whitelist());
assert!(matcher.matched("foo.c", false).is_ignore());
Example: negation
This is like the previous example, but shows how negating a file type works. That is, this will let us match file paths that don’t correspond to a particular file type.
use ignore::types::TypesBuilder;
let mut builder = TypesBuilder::new();
builder.add_defaults();
builder.negate("c");
let matcher = builder.build().unwrap();
assert!(matcher.matched("foo.rs", false).is_none());
assert!(matcher.matched("foo.c", false).is_ignore());
Example: custom file type definitions
This shows how to extend this library default file type definitions with your own.
use ignore::types::TypesBuilder;
let mut builder = TypesBuilder::new();
builder.add_defaults();
builder.add("foo", "*.foo");
// Another way of adding a file type definition.
// This is useful when accepting input from an end user.
builder.add_def("bar:*.bar");
// Note: we only select `foo`, not `bar`.
builder.select("foo");
let matcher = builder.build().unwrap();
assert!(matcher.matched("x.foo", false).is_whitelist());
// This is ignored because we only selected the `foo` file type.
assert!(matcher.matched("x.bar", false).is_ignore());
We can also add file type definitions based on other definitions.
use ignore::types::TypesBuilder;
let mut builder = TypesBuilder::new();
builder.add_defaults();
builder.add("foo", "*.foo");
builder.add_def("bar:include:foo,cpp");
builder.select("bar");
let matcher = builder.build().unwrap();
assert!(matcher.matched("x.foo", false).is_whitelist());
assert!(matcher.matched("y.cpp", false).is_whitelist());
Structs
- A single file type definition.
- Glob represents a single glob in a set of file type definitions.
- Types is a file type matcher.
- TypesBuilder builds a type matcher from a set of file type definitions and a set of file type selections.