Function wax::escape

source ·
pub fn escape(unescaped: &str) -> Cow<'_, str>
Expand description

Escapes text as a literal glob expression.

This function escapes any and all meta-characters in the given string, such that all text is interpreted as a literal or separator when read as a glob expression.

Examples

This function can be used to escape opaque strings, such as a string obtained from a user that must be interpreted literally.

use wax::Glob;

// An opaque file name that this code does not construct.
let name: String = {
    /* ... */
};

// Do not allow patterns in `name`.
let expression = format!("{}{}", "**/", wax::escape(&name));
if let Ok(glob) = Glob::new(&expression) { /* ... */ }

Sometimes part of a path contains numerous meta-characters. This function can be used to reliably escape them while making the unescaped part of the expression a bit easier to read.

use wax::Glob;

let expression = format!("{}{}", "logs/**/", wax::escape("ingest[01](L).txt"));
let glob = Glob::new(&expression).unwrap();