Module platform

Module platform 

Source
Expand description

Platform-specific utilities and cross-platform compatibility helpers

This module provides abstractions over platform differences to ensure AGPM works consistently across Windows, macOS, and Linux. It handles differences in:

  • Path separators and conventions
  • Home directory resolution
  • Command execution and shell interfaces
  • File system behavior and limitations
  • Environment variable handling

§Cross-Platform Design

AGPM is designed to provide identical functionality across all supported platforms while respecting platform conventions and limitations. This module encapsulates the platform-specific logic to achieve this goal.

§Examples

use agpm_cli::utils::platform::{get_home_dir, resolve_path, is_windows};

// Get platform-appropriate home directory
let home = get_home_dir()?;
println!("Home directory: {}", home.display());

// Resolve paths with tilde expansion and env vars
let config_path = resolve_path("~/.agpm/config.toml")?;

// Handle platform differences
if is_windows() {
    println!("Running on Windows");
} else {
    println!("Running on Unix-like system");
}

§Platform Support Matrix

FeatureWindowsmacOSLinux
Long paths (>260 chars)
Case sensitivityNoConfigurableYes
Tilde expansion
Environment variables%VAR%$VAR$VAR
Shell commandscmd.exeshsh
Git commandgit.exegitgit

§Security Considerations

  • Path traversal prevention in safe_join
  • Input validation in validate_path_chars
  • Safe environment variable expansion
  • Proper handling of special Windows filenames

Functions§

command_exists
Checks if a command is available in the system PATH.
get_cache_dir
Returns the platform-specific cache directory for AGPM.
get_data_dir
Returns the platform-specific data directory for AGPM.
get_git_command
Returns the appropriate Git command name for the current platform.
get_home_dir
Gets the home directory path for the current user.
get_shell_command
Returns the appropriate shell command and flag for the current platform.
is_windows
Checks if the current platform is Windows.
normalize_path_separator
Converts a path to use the correct separator for the current platform.
path_to_os_str
Returns a path as an OsStr for use in command arguments.
path_to_string
Safely converts a path to a string, handling non-UTF-8 paths gracefully.
paths_equal
Compares two paths for equality, respecting platform case sensitivity rules.
resolve_path
Resolves a path with tilde expansion and environment variable substitution.
safe_canonicalize
Canonicalizes a path with proper cross-platform handling.
safe_join
Safely joins a base path with a relative path, preventing directory traversal.
validate_path_chars
Validates that a path contains only characters valid for the current platform.
windows_long_path
No-op implementation of windows_long_path for non-Windows platforms.