pub fn resolve_path(path: &str) -> Result<PathBuf>Expand description
Resolves a path with tilde expansion and environment variable substitution.
This function processes path strings to handle common shell conventions:
- Tilde (
~) expansion to the user’s home directory - Environment variable substitution (
$VARor%VAR%) - Windows long path handling when necessary
§Arguments
path- The path string to resolve (may contain~or environment variables)
§Returns
A resolved PathBuf with expansions applied, or an error if expansion fails
§Examples
use agpm_cli::utils::platform::resolve_path;
// Tilde expansion
let config_path = resolve_path("~/.agpm/config.toml")?;
println!("Config: {}", config_path.display());
// Environment variable expansion (Unix)
let path_with_env = resolve_path("$HOME/Documents/project")?;
// Environment variable expansion (Windows)
let path_with_env = resolve_path("%USERPROFILE%\\Documents\\project")?;§Supported Patterns
~/path- Expands to{home}/path$VAR/path(Unix) - Expands environment variable%VAR%/path(Windows) - Expands environment variable${VAR}/path(Unix) - Alternative env var syntax
§Error Cases
- Invalid tilde usage (e.g.,
~user/pathon Windows) - Undefined environment variables
- Invalid variable syntax
- Home directory cannot be determined
§Security
This function safely handles environment variable expansion and prevents common injection attacks by using proper parsing libraries.
§See Also
get_home_dirfor home directory resolutionvalidate_path_charsfor path validation