use super::*;
impl GlobPattern {
pub fn compile(pattern: &str) -> Result<Self, GlobError> {
Self::compile_with_mode(pattern, true)
}
pub fn compile_flat(pattern: &str) -> Result<Self, GlobError> {
Self::compile_with_mode(pattern, false)
}
pub fn compile_with_mode(pattern: &str, path_aware: bool) -> Result<Self, GlobError> {
let expanded: Vec<String> = expand_braces(pattern)?;
let mut branches: Vec<CompiledBranch> = Vec::with_capacity(expanded.len());
for raw in expanded {
branches.push(compile_branch(&raw, path_aware)?);
}
Ok(Self {
source: pattern.to_owned(),
path_aware,
branches,
})
}
#[inline(always)]
pub fn as_str(&self) -> &str {
let Self { source, .. } = self;
source.as_str()
}
#[inline(always)]
pub fn is_path_aware(&self) -> bool {
let Self { path_aware, .. } = self;
*path_aware
}
#[inline]
pub fn is_match(&self, candidate: &str) -> bool {
let Self {
path_aware,
branches,
..
} = self;
for branch in branches {
if match_branch(branch, candidate, *path_aware) {
return true;
}
}
false
}
#[inline(always)]
pub(crate) fn into_compiled_parts(self) -> (Vec<CompiledBranch>, bool) {
let Self {
branches,
path_aware,
..
} = self;
(branches, path_aware)
}
#[inline(always)]
pub(crate) fn exact_literal(&self) -> Option<&str> {
let Self { branches, .. } = self;
if branches.len() == 1
&& let CompiledBranch::Exact(ref lit) = branches[0]
{
return Some(lit.as_str());
}
None
}
}
impl FromStr for GlobPattern {
type Err = GlobError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::compile(s)
}
}
impl fmt::Display for GlobPattern {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}