Skip to content

Commit

Permalink
Rename Pattern to Program and Combine to Pattern.
Browse files Browse the repository at this point in the history
This change renames the `Pattern` and `Combine` traits. The `Combine`
trait is used in `any` APIs, but it does not principally concern
combinators. Note too that `Pattern: Combine`. `Combine` broadly
describes pattern representations and, more specifically, types that can
be converted into a checked token tree. `Pattern` is more specific, and
describes _compiled_ pattern representations that can be matched against
paths.

As such, `Combine` is now `Pattern` and `Pattern` is now `Program`. This
actually makes `any` APIs less confusing, as they accept `Pattern`s,
whatever there form may be.

This change also alters the nomenclature in documentation and the
conceptualization of `Combine`.
  • Loading branch information
olson-sean-k committed Nov 17, 2023
1 parent 76afaa1 commit b1346f1
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 136 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ boundaries.
Match a path against a glob:

```rust
use wax::{Glob, Pattern};
use wax::{Glob, Program};

let glob = Glob::new("*.png").unwrap();
assert!(glob.is_match("logo.png"));
Expand All @@ -26,7 +26,7 @@ assert!(glob.is_match("logo.png"));
Match a path against a glob with matched text (captures):

```rust
use wax::{CandidatePath, Glob, Pattern};
use wax::{CandidatePath, Glob, Program};

let glob = Glob::new("**/{*.{go,rs}}").unwrap();

Expand Down Expand Up @@ -68,7 +68,7 @@ for entry in glob
Match a path against multiple globs:

```rust
use wax::{Glob, Pattern};
use wax::{Glob, Program};

let any = wax::any([
"src/**/*.rs",
Expand Down Expand Up @@ -273,12 +273,12 @@ the expression to match or walk overlapping trees.
## Combinators

Glob patterns can be combined and matched together using the `any` combinator.
`any` accepts an `IntoIterator` with items that are compiled `Pattern`s or `str`
slices. The output is an `Any`, which implements `Pattern` and efficiently
matches any of its input patterns.
`any` accepts an `IntoIterator` of `Pattern`s, such as compiled `Program`s like
`Glob` or pattern text like `str` slices. The output is an `Any`, which
implements `Program` and efficiently matches any of its input patterns.

```rust
use wax::{Glob, Pattern};
use wax::{Glob, Program};

let any = wax::any(["**/*.txt", "src/**/*.rs"]).unwrap();
assert!(any.is_match("src/lib.rs"));
Expand Down Expand Up @@ -403,7 +403,7 @@ combination with the glob.
```rust
use dunce; // Avoids UNC paths on Windows.
use std::path::Path;
use wax::{Glob, Pattern};
use wax::{Glob, Program};

let path: &Path = /* ... */ // Candidate path.

Expand Down
22 changes: 11 additions & 11 deletions src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ impl From<OwnedText> for MaybeOwnedText<'static> {
}
}

/// Text that has been matched by a [`Pattern`] and its captures.
/// Text that has been matched by a [`Program`] and its captures.
///
/// To match a [`Glob`] or other [`Pattern`] against a [`CandidatePath`] and get the matched text,
/// use the [`Pattern::matched`] function.
/// To match a [`Glob`] or other [`Program`] against a [`CandidatePath`] and get the matched text,
/// use the [`Program::matched`] function.
///
/// All [`Pattern`]s provide an implicit capture of the complete text of a match. This implicit
/// All [`Program`]s provide an implicit capture of the complete text of a match. This implicit
/// capture has index zero, and is exposed via the [`complete`] function as well as the [`get`]
/// function using index zero. Capturing tokens are indexed starting at one, and can be used to
/// isolate more specific sub-text.
Expand All @@ -92,7 +92,7 @@ impl From<OwnedText> for MaybeOwnedText<'static> {
/// file name of a match can be extracted using an alternative to group patterns.
///
/// ```rust
/// use wax::{CandidatePath, Glob, Pattern};
/// use wax::{CandidatePath, Glob, Program};
///
/// let glob = Glob::new("src/**/{*.{go,rs}}").unwrap();
/// let candidate = CandidatePath::from("src/graph/link.rs");
Expand All @@ -105,8 +105,8 @@ impl From<OwnedText> for MaybeOwnedText<'static> {
/// [`complete`]: crate::MatchedText::complete
/// [`get`]: crate::MatchedText::get
/// [`Glob`]: crate::Glob
/// [`Pattern`]: crate::Pattern
/// [`Pattern::matched`]: crate::Pattern::matched
/// [`Program`]: crate::Program
/// [`Program::matched`]: crate::Program::matched
#[derive(Debug)]
pub struct MatchedText<'t> {
inner: MaybeOwnedText<'t>,
Expand Down Expand Up @@ -139,18 +139,18 @@ impl<'t> MatchedText<'t> {

/// Gets the complete text of a match.
///
/// All [`Pattern`]s have an implicit capture of the complete text at index zero. This function
/// All [`Program`]s have an implicit capture of the complete text at index zero. This function
/// is therefore equivalent to unwrapping the output of the [`get`] function with index zero.
///
/// [`get`]: crate::MatchedText::get
/// [`Pattern`]: crate::Pattern
/// [`Program`]: crate::Program
pub fn complete(&self) -> &str {
self.get(0).expect("match has no complete text")
}

/// Gets the matched text of a capture at the given index.
///
/// All [`Pattern`]s have an implicit capture of the complete text at index zero. Capturing
/// All [`Program`]s have an implicit capture of the complete text at index zero. Capturing
/// tokens are indexed from one, so any capturing sub-expression will be indexed after the
/// implicit complete text. For example, the sub-expression `*` in the glob expression `*.txt`
/// is at index one and will exclude the suffix `.txt` in its matched text.
Expand All @@ -160,7 +160,7 @@ impl<'t> MatchedText<'t> {
/// group matched text, such as isolating an entire matched file name using an expression like
/// `{*.{go,rs}}`.
///
/// [`Pattern`]: crate::Pattern
/// [`Program`]: crate::Program
pub fn get(&self, index: usize) -> Option<&str> {
match self.inner {
MaybeOwnedText::Borrowed(ref captures) => {
Expand Down
Loading

0 comments on commit b1346f1

Please sign in to comment.