pub fn tilde_with_context<SI: ?Sized, P, HD>(
    input: &SI,
    home_dir: HD
) -> Cow<'_, str> where
    SI: AsRef<str>,
    P: AsRef<Path>,
    HD: FnOnce() -> Option<P>, 
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 Path via AsRef<Path>. For example, P may be Path, PathBuf or Cow<Path>, which gives a lot of flexibility.

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<PathBuf> { Some(Path::new("/home/user").into()) }

assert_eq!(
   shellexpand::tilde_with_context("~/some/dir", home_dir),
   "/home/user/some/dir"
);