Skip to content

Refactor glob pattern handling to use globwalk crate and remove custom parsing logic #171

@sourcery-ai

Description

@sourcery-ai

The current implementation includes a significant amount of custom code for glob pattern parsing, validation, and normalization (e.g., normalize_separators, BraceValidator, process_glob_entry, and related error handling). This custom logic could be greatly simplified and made more robust by replacing it with the globwalk crate, which natively supports features such as brace expansion, escape handling, platform-native separators, and file-only filtering.

Suggested steps for refactoring:

  1. Add globwalk = "0.8" to Cargo.toml.
  2. Replace the custom parsing/validation/normalization logic with a function using GlobWalkerBuilder from globwalk, as shown below:
    use globwalk::{GlobWalkerBuilder, WalkError};
    use minijinja::{Error, ErrorKind};
    
    pub(crate) fn glob_paths(pattern: &str) -> Result<Vec<String>, Error> {
        let walker = GlobWalkerBuilder::new(".", pattern)
            .case_insensitive(false)
            .require_literal_separator(true)
            .require_literal_leading_dot(false)
            .build()
            .map_err(|e| Error::new(ErrorKind::InvalidPattern, e.to_string()))?;
    
        let paths = walker
            .filter_map(|r| r.ok())
            .filter(|e| e.file_type().is_file())
            .map(|e| e.path().to_string_lossy().replace("\\", "/"))
            .collect();
    
        Ok(paths)
    }
  3. Remove all custom glob parsing and validation code, including:
    • CharContext, BraceValidator, BraceValidationState
    • normalize_separators, force_literal_escapes, process_backslash
    • process_glob_entry, fetch_metadata, open_root_dir, and custom error plumbing

Benefits:

  • Maintains all current features (brace expansion, escape handling, separator normalization, file filtering)
  • Reduces code complexity by ~500 lines
  • Leverages a well-tested external crate

Action items:

  • Refactor glob pattern handling to use globwalk as described
  • Remove obsolete custom parsing/validation code
  • Ensure all existing tests pass and add new ones if necessary to cover edge cases

This change will simplify maintenance and improve reliability.


I created this issue for @leynos from #168 (comment).

Tips and commands

Getting Help

Metadata

Metadata

Assignees

No one assigned

    Labels

    lowAin't annoying anyone but the QA department

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions