wildcard-glob 11.2.0

Zero-dependency, zero-allocation compiled wildcard and path glob pattern matcher supporting *, **, ?, character classes, brace alternation, and multi-pattern GlobSet.
Documentation
use super::*;

/// Compilation and matching methods for [`GlobPattern`].
impl GlobPattern {
    /// Compiles a path-aware glob pattern where `*` and `?` do not match `/`.
    ///
    /// # Arguments
    ///
    /// - `&str` - Glob pattern string.
    ///
    /// # Returns
    ///
    /// - `Result<Self, GlobError>` - Compiled matcher or syntax error.
    pub fn compile(pattern: &str) -> Result<Self, GlobError> {
        Self::compile_with_mode(pattern, true)
    }

    /// Compiles a flat wildcard pattern where `*` and `?` match any character including `/`.
    ///
    /// # Arguments
    ///
    /// - `&str` - Glob pattern string.
    ///
    /// # Returns
    ///
    /// - `Result<Self, GlobError>` - Compiled matcher or syntax error.
    pub fn compile_flat(pattern: &str) -> Result<Self, GlobError> {
        Self::compile_with_mode(pattern, false)
    }

    /// Compiles a glob pattern with explicit path-separator awareness.
    ///
    /// # Arguments
    ///
    /// - `&str` - Glob pattern string.
    /// - `bool` - When `true`, `*` and `?` stop at `/`; `**` crosses `/`.
    ///
    /// # Returns
    ///
    /// - `Result<Self, GlobError>` - Compiled matcher or syntax error.
    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,
        })
    }

    /// Returns the original pattern text.
    ///
    /// # Returns
    ///
    /// - `&str` - Original source pattern.
    #[inline(always)]
    pub fn as_str(&self) -> &str {
        let Self { source, .. } = self;
        source.as_str()
    }

    /// Returns whether single-segment wildcards stop at `/`.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` for path-aware matching.
    #[inline(always)]
    pub fn is_path_aware(&self) -> bool {
        let Self { path_aware, .. } = self;
        *path_aware
    }

    /// Returns whether `candidate` matches this compiled glob pattern.
    ///
    /// # Arguments
    ///
    /// - `&str` - Input string to match.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` if any compiled branch matches `candidate`.
    #[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
    }

    /// Consumes this pattern and returns its compiled branches and path-awareness flag.
    ///
    /// # Returns
    ///
    /// - `(Vec<CompiledBranch>, bool)` - `(branches, path_aware)`.
    #[inline(always)]
    pub(crate) fn into_compiled_parts(self) -> (Vec<CompiledBranch>, bool) {
        let Self {
            branches,
            path_aware,
            ..
        } = self;
        (branches, path_aware)
    }

    /// Returns `Some(&str)` if this pattern consists of a single pure literal branch.
    ///
    /// # Returns
    ///
    /// - `Option<&str>` - Exact literal text when no wildcards or alternations exist.
    #[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
    }
}

/// Parses a path-aware [`GlobPattern`] from a string slice.
impl FromStr for GlobPattern {
    type Err = GlobError;

    /// Compiles `s` as a path-aware glob pattern.
    ///
    /// # Arguments
    ///
    /// - `&str` - Source glob pattern.
    ///
    /// # Returns
    ///
    /// - `Result<Self, Self::Err>` - Compiled matcher or syntax error.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::compile(s)
    }
}

/// Formats [`GlobPattern`] as its original pattern string.
impl fmt::Display for GlobPattern {
    /// Writes the original pattern string into `f`.
    ///
    /// # Arguments
    ///
    /// - `&mut fmt::Formatter<'_>` - Destination formatter.
    ///
    /// # Returns
    ///
    /// - `fmt::Result` - Formatting outcome.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}