gix_glob/lib.rs
1//! Provide glob [`Patterns`][Pattern] for matching against paths or anything else.
2//!
3//! ## Examples
4//!
5//! ```
6//! use bstr::ByteSlice;
7//! use gix_glob::{pattern::Case, wildmatch, Pattern};
8//!
9//! let pattern = Pattern::from_bytes(b"src/**/*.rs").unwrap();
10//! assert!(pattern.matches_repo_relative_path(
11//! b"src/lib.rs".as_bstr(),
12//! Some(4),
13//! Some(false),
14//! Case::Sensitive,
15//! wildmatch::Mode::NO_MATCH_SLASH_LITERAL,
16//! ));
17//!
18//! assert!(gix_glob::wildmatch(
19//! b"*.rs".as_bstr(),
20//! b"lib.rs".as_bstr(),
21//! wildmatch::Mode::empty(),
22//! ));
23//! ```
24//! ## Feature Flags
25#![cfg_attr(
26 all(doc, feature = "document-features"),
27 doc = ::document_features::document_features!()
28)]
29#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg))]
30#![deny(missing_docs, rust_2018_idioms)]
31#![forbid(unsafe_code)]
32
33use bstr::BString;
34
35/// A glob pattern optimized for matching paths relative to a root directory.
36///
37/// For normal globbing, use [`wildmatch()`] instead.
38#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
39#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
40pub struct Pattern {
41 /// the actual pattern bytes
42 pub text: BString,
43 /// Additional information to help accelerate pattern matching.
44 pub mode: pattern::Mode,
45 /// The position in `text` with the first wildcard character, or `None` if there is no wildcard at all.
46 pub first_wildcard_pos: Option<usize>,
47}
48
49///
50pub mod pattern;
51
52pub mod search;
53
54///
55pub mod wildmatch;
56pub use wildmatch::function::wildmatch;
57
58mod parse;
59
60/// Create a [`Pattern`] by parsing `text` or return `None` if `text` is empty.
61///
62/// Note that
63pub fn parse(text: impl AsRef<[u8]>) -> Option<Pattern> {
64 Pattern::from_bytes(text.as_ref())
65}