pub fn tilde_with_context<SI, P, HD>(input: &SI, home_dir: HD) -> Cow<'_, str>
Expand description
Performs the tilde expansion using the provided context.
This function expands tilde (~
) character in the beginning of the input string into contents
of the path returned by home_dir
function. If the input string does not contain a tilde, or
if it is not followed either by a slash (/
) or by the end of string, then it is also left as
is. This means, in particular, that expansions like ~anotheruser/directory
are not supported.
The context function may also return a None
, in that case even if the tilde is present in the
input in the correct place, it won’t be replaced (there is nothing to replace it with, after
all).
This function has three generic type parameters: SI
represents the input string, P
is the
output of a context lookup, and HD
is the context closure. SI
must be a type, a reference
to which can be converted to a string slice via AsRef<str>
, and P
must be a type, a
reference to which can be converted to a str
via AsRef<str>
.
Home directories which are available only as a Path
are not supported here,
because they cannot be represented in the output string.
If you wish to support home directories which are not valid Unicode,
use the path
module.
If you need to expand the tilde into the actual user home directory, you can use tilde()
or
full()
functions.
§Examples
use std::path::{PathBuf, Path};
fn home_dir() -> Option<String> { Some("/home/user".into()) }
assert_eq!(
shellexpand::tilde_with_context("~/some/dir", home_dir),
"/home/user/some/dir"
);