normalize_path_separator

Function normalize_path_separator 

Source
pub fn normalize_path_separator(path: &Path) -> String
Expand description

Converts a path to use the correct separator for the current platform.

This function normalizes path separators to match platform conventions:

  • Windows: Converts / to \
  • Unix-like: Converts \ to /

This is primarily useful for display purposes or when interfacing with platform-specific APIs that expect native separators.

§Arguments

  • path - The path to normalize

§Returns

A string with platform-appropriate separators

§Examples

use ccpm::utils::platform::normalize_path_separator;
use std::path::Path;

let mixed_path = Path::new("src/utils\\platform.rs");
let normalized = normalize_path_separator(mixed_path);

#[cfg(windows)]
assert_eq!(normalized, "src\\utils\\platform.rs");

#[cfg(not(windows))]
assert_eq!(normalized, "src/utils/platform.rs");

§Platform Behavior

  • Windows: All separators become \
  • Unix-like: All separators become /

§Use Cases

  • Display paths to users in platform-native format
  • Interfacing with platform-specific APIs
  • Generating platform-specific configuration files
  • Logging and error messages

§Note

Rust’s Path and PathBuf types handle separators transparently in most cases, so this function is primarily needed for display and external interface purposes.