English | 简体中文
Advanced cross-platform path handling library for perfect Windows and Unix path conversion.
- ✅ Windows ↔ Unix bidirectional path conversion
- ✅ UNC path support
- ✅ Automatic encoding detection and conversion (UTF-8, UTF-16, Windows-1252)
- ✅ Path security verification (path traversal prevention)
- ✅ Configurable drive letter mappings
- ✅ Path normalization
- ✅ Zero-cost abstractions, high performance
- ✅ Comprehensive error handling
- ✅ Serde serialization support
[dependencies]
cross_path = "0.0.1"use cross_path::CrossPath;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Windows to Unix conversion
let cp = CrossPath::new(r"C:\Users\John\file.txt")?;
println!("Unix path: {}", cp.to_unix()?); // /mnt/c/Users/John/file.txt
// Unix to Windows conversion
let cp = CrossPath::new("/home/john/file.txt")?;
println!("Windows path: {}", cp.to_windows()?); // C:\home\john\file.txt
// Direct conversion
let unix_path = r"C:\Users\test".to_unix_path()?;
println!("Direct conversion: {}", unix_path);
Ok(())
}use cross_path::{CrossPath, PathConfig, PathStyle};
let config = PathConfig {
style: PathStyle::Auto,
preserve_encoding: true,
security_check: true,
drive_mappings: vec![
("C:".to_string(), "/mnt/c".to_string()),
("D:".to_string(), "/mnt/data".to_string()),
],
normalize: true,
};
let cp = CrossPath::with_config(r"D:\Data\file.txt", config) ?;
println!("Converted: {}", cp.to_unix()?); // /mnt/data/Data/file.txtuse cross_path::security::PathSecurityChecker;
let checker = PathSecurityChecker::new();
let path = std::path::Path::new("../../etc/passwd");
match checker.check(path) {
Ok(_) => println ! ("Path is safe"),
Err(e) => println ! ("Security warning: {}", e),
}
// Sanitize dangerous paths
let safe_path = PathSecurityChecker::sanitize_path("file<>.txt");
println!("Safe path: {}", safe_path); // file__.txtuse cross_path::unicode::UnicodeHandler;
// Detect encoding
let bytes = b"C:\\Users\\\x93\x65\x88\x97\\file.txt";
let encoding = UnicodeHandler::detect_encoding(bytes);
println!("Detected encoding: {}", encoding.name());
// Convert to UTF-8
let utf8_string = UnicodeHandler::convert_to_utf8(bytes) ?;
println!("UTF-8 string: {}", utf8_string);For detailed API documentation, run:
cargo doc --open- Windows absolute path ↔ Unix absolute path
- Relative path conversion
- UNC path conversion
- Drive letter mapping
- Separator unification
- Path traversal attack detection
- Dangerous pattern detection
- Windows reserved name checking
- System directory access control
- UTF-8
- UTF-16 LE
- Windows-1252
- Automatic encoding detection
# Run all tests
cargo test
# Run specific tests
cargo test --test conversion
# Run benchmarks
cargo bench- Fork the repository
- Create a feature branch
- Commit your changes
- Push the branch
- Create a Pull Request
MIT OR Apache-2.0 (LICENSE-MIT, LICENSE-APACHE)