| Crates.io | replace-homedir |
| lib.rs | replace-homedir |
| version | 0.1.1 |
| created_at | 2025-12-11 22:12:53.315231+00 |
| updated_at | 2025-12-11 23:32:15.649398+00 |
| description | Replace user home directory in a path with another string (Rust alternative to npm replace-homedir) |
| homepage | |
| repository | https://github.com/tupe12334/replace-homedir |
| max_upload_size | |
| id | 1980652 |
| size | 346,382 |
Replace user home directory in a path with another string. Useful for tildifying a path.
This is a Rust alternative to the npm package replace-homedir.
Add this to your Cargo.toml:
[dependencies]
replace-homedir = "0.1"
use replace_homedir::replace_homedir;
let short_path = replace_homedir("/Users/phated/myProject", "~");
// short_path == "~/myProject"
replace_homedir(path, replacement) -> StringTakes a path string and a replacement string. If the path is absolute and begins with the user's home directory, the home directory portion is replaced with the replacement string.
path (&str): The file path to processreplacement (&str): The string to substitute for the home directoryA String with the home directory replaced, or the original path if it doesn't start with the home directory.
use replace_homedir::replace_homedir;
// Assuming home directory is /home/user
let result = replace_homedir("/home/user/projects/myapp", "~");
assert_eq!(result, "~/projects/myapp");
// Non-home paths are returned unchanged
let result = replace_homedir("/tmp/something", "~");
assert_eq!(result, "/tmp/something");
replace_homedir_with(path, replacement_fn) -> StringTakes a path string and a closure for dynamic replacement logic. The closure receives the home directory as a &Path and returns the replacement string.
path (&str): The file path to processreplacement_fn (FnOnce(&Path) -> String): A closure that receives the home directory and returns the replacementuse replace_homedir::replace_homedir_with;
let result = replace_homedir_with("/home/user/docs", |_home| "HOME".to_string());
// result == "HOME/docs"
MIT