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:
- Add
globwalk = "0.8" to Cargo.toml.
- 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)
}
- 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
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 theglobwalkcrate, which natively supports features such as brace expansion, escape handling, platform-native separators, and file-only filtering.Suggested steps for refactoring:
globwalk = "0.8"to Cargo.toml.GlobWalkerBuilderfromglobwalk, as shown below:CharContext,BraceValidator,BraceValidationStatenormalize_separators,force_literal_escapes,process_backslashprocess_glob_entry,fetch_metadata,open_root_dir, and custom error plumbingBenefits:
Action items:
globwalkas describedThis change will simplify maintenance and improve reliability.
I created this issue for @leynos from #168 (comment).
Tips and commands
Getting Help