Skip to main content

relativize_glob

Function relativize_glob 

Source
pub fn relativize_glob<'a>(
    glob: &'a str,
    relative_to: &FileSystemPath,
) -> Option<(&'a str, FileSystemPath)>
Expand description

Resolve the leading ./ and ../ segments of a glob pattern into a directory, so that what remains only ever traverses down the tree.

Glob matches paths relative to the directory that is scanned and has no notion of . or .., and the directory walker only descends. A pattern like ../dir/*.js therefore has to be turned into the pattern dir/*.js matched against the parent of relative_to, which is what this does.

Returns the remaining pattern together with the directory it is relative to, or None if the pattern walks above the root of the filesystem. Callers decide how to report that: it is a hard error for some and a diagnostic for others.

ⓘ
// with `relative_to` = `src/app`
relativize_glob("*.js")           // => ("*.js",    "src/app")
relativize_glob("./dir/*.js")     // => ("dir/*.js", "src/app")
relativize_glob("../dir/*.js")    // => ("dir/*.js", "src")
relativize_glob("././../x/*.js")  // => ("x/*.js",   "src")