Struct wax::MatchedText
source · pub struct MatchedText<'t> { /* private fields */ }
Expand description
Text that has been matched by a Program
and its captures.
To match a Glob
or other Program
against a CandidatePath
and get the matched text,
use the Program::matched
function.
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.
Examples
Capturing tokens and matched text can be used to isolate sub-text in a match. For example, the file name of a match can be extracted using an alternative to group patterns.
use wax::{CandidatePath, Glob, Program};
let glob = Glob::new("src/**/{*.{go,rs}}").unwrap();
let candidate = CandidatePath::from("src/graph/link.rs");
let matched = glob.matched(&candidate).unwrap();
assert_eq!("link.rs", matched.get(2).unwrap());
Implementations§
source§impl<'t> MatchedText<'t>
impl<'t> MatchedText<'t>
sourcepub fn into_owned(self) -> MatchedText<'static>
pub fn into_owned(self) -> MatchedText<'static>
Clones any borrowed data into an owning instance.
sourcepub fn to_owned(&self) -> MatchedText<'static>
pub fn to_owned(&self) -> MatchedText<'static>
Clones any borrowed data to an owning instance.
This function is similar to into_owned
, but does not consume its receiver. Due to a
technical limitation, MatchedText
cannot properly implement Clone
, so this function
is provided as a stop gap that allows a distinct instance to be created that owns its data.
sourcepub fn get(&self, index: usize) -> Option<&str>
pub fn get(&self, index: usize) -> Option<&str>
Gets the matched text of a capture at the given index.
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.
Alternative and repetition patterns group their sub-globs into a single capture, so it is
not possible to isolate matched text from their sub-globs. This can be used to explicitly
group matched text, such as isolating an entire matched file name using an expression like
{*.{go,rs}}
.