cmdx 0.1.0

Cross-platform command and path translator library for terminal emulators
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! Path translation module - converts file paths between operating systems
//!
//! This module provides bidirectional path translation between Windows and Unix-like
//! operating systems, handling path separators, drive letters, and common path mappings.

use super::os::Os;
use serde::{Deserialize, Serialize};
use std::fmt;

/// Result of a path translation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PathTranslation {
    /// The translated path
    pub path: String,
    /// Original path
    pub original: String,
    /// Source OS
    pub from_os: Os,
    /// Target OS
    pub to_os: Os,
    /// Whether drive letter was translated
    pub drive_translated: bool,
    /// Warnings about the translation
    pub warnings: Vec<String>,
}

impl PathTranslation {
    pub fn new(path: String, original: String, from_os: Os, to_os: Os) -> Self {
        Self {
            path,
            original,
            from_os,
            to_os,
            drive_translated: false,
            warnings: Vec::new(),
        }
    }
}

impl fmt::Display for PathTranslation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.path)
    }
}

/// Errors that can occur during path translation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PathError {
    /// Empty path
    EmptyPath,
    /// Invalid path format
    InvalidPath(String),
}

impl fmt::Display for PathError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PathError::EmptyPath => write!(f, "Empty path provided"),
            PathError::InvalidPath(msg) => write!(f, "Invalid path: {}", msg),
        }
    }
}

impl std::error::Error for PathError {}

/// Common drive letter to Unix path mappings
fn get_drive_mapping(drive: char) -> String {
    // Use lowercase for the mount point (WSL convention)
    format!("/mnt/{}", drive.to_ascii_lowercase())
}

/// Check if a path looks like a Windows path
pub fn is_windows_path(path: &str) -> bool {
    // Check for drive letter (e.g., C:\, D:/)
    if path.len() >= 2 {
        let chars: Vec<char> = path.chars().collect();
        if chars[0].is_ascii_alphabetic() && chars[1] == ':' {
            return true;
        }
    }
    // Check for UNC paths (\\server\share)
    if path.starts_with("\\\\") {
        return true;
    }
    // Check for backslashes
    path.contains('\\')
}

/// Check if a path looks like a Unix path
pub fn is_unix_path(path: &str) -> bool {
    path.starts_with('/') || path.starts_with("~/") || path.starts_with("./") || path.starts_with("../")
}

/// Translate a Windows path to Unix path
fn windows_to_unix(path: &str, result: &mut PathTranslation) -> String {
    let mut unix_path = path.to_string();
    
    // Handle drive letter (C:\Users -> /mnt/c/Users)
    if unix_path.len() >= 2 {
        let chars: Vec<char> = unix_path.chars().collect();
        if chars[0].is_ascii_alphabetic() && chars[1] == ':' {
            let drive = chars[0];
            let mount_point = get_drive_mapping(drive);
            unix_path = format!("{}{}", mount_point, &unix_path[2..]);
            result.drive_translated = true;
        }
    }
    
    // Handle UNC paths (\\server\share -> //server/share or /mnt/server/share)
    if unix_path.starts_with("\\\\") {
        unix_path = unix_path.replacen("\\\\", "//", 1);
        result.warnings.push("UNC path converted to network path format".to_string());
    }
    
    // Convert backslashes to forward slashes
    unix_path = unix_path.replace('\\', "/");
    
    // Normalize multiple slashes (except leading // for network paths)
    if unix_path.starts_with("//") {
        let rest = unix_path[2..].split('/').filter(|s| !s.is_empty()).collect::<Vec<_>>().join("/");
        unix_path = format!("//{}", rest);
    } else {
        let parts: Vec<_> = unix_path.split('/').filter(|s| !s.is_empty()).collect();
        unix_path = if path.starts_with('/') || path.starts_with('\\') || path.chars().nth(1) == Some(':') {
            format!("/{}", parts.join("/"))
        } else {
            parts.join("/")
        };
    }
    
    unix_path
}

/// Translate a Unix path to Windows path
fn unix_to_windows(path: &str, result: &mut PathTranslation) -> String {
    let mut windows_path = path.to_string();
    
    // Handle /mnt/X/ paths (convert to X:\)
    if windows_path.starts_with("/mnt/") && windows_path.len() >= 6 {
        let drive_char = windows_path.chars().nth(5);
        if let Some(drive) = drive_char {
            if drive.is_ascii_alphabetic() {
                // Check if it's followed by / or end of string
                let after_drive = windows_path.chars().nth(6);
                if after_drive.is_none() || after_drive == Some('/') {
                    windows_path = format!("{}:{}", drive.to_ascii_uppercase(), &windows_path[6..]);
                    result.drive_translated = true;
                }
            }
        }
    }
    // Handle /home/username -> C:\Users\username (common mapping)
    else if windows_path.starts_with("/home/") {
        windows_path = format!("C:\\Users{}", &windows_path[5..]);
        result.drive_translated = true;
        result.warnings.push("/home mapped to C:\\Users".to_string());
    }
    // Handle ~ (home directory)
    else if windows_path.starts_with("~/") {
        windows_path = format!("%USERPROFILE%{}", &windows_path[1..]);
        result.warnings.push("~ translated to %USERPROFILE%".to_string());
    }
    else if windows_path == "~" {
        windows_path = "%USERPROFILE%".to_string();
        result.warnings.push("~ translated to %USERPROFILE%".to_string());
    }
    // Handle root paths
    else if windows_path.starts_with('/') && !windows_path.starts_with("//") {
        // Generic Unix root -> C:\
        windows_path = format!("C:{}", windows_path);
        result.drive_translated = true;
        result.warnings.push("Root path mapped to C: drive".to_string());
    }
    // Handle network paths (//server/share -> \\server\share)
    else if windows_path.starts_with("//") {
        windows_path = windows_path.replacen("//", "\\\\", 1);
    }
    
    // Convert forward slashes to backslashes
    windows_path = windows_path.replace('/', "\\");
    
    // Normalize multiple backslashes (but keep UNC prefix)
    if windows_path.starts_with("\\\\") {
        let rest: Vec<_> = windows_path[2..].split('\\').filter(|s| !s.is_empty()).collect();
        windows_path = format!("\\\\{}", rest.join("\\"));
    } else {
        let parts: Vec<_> = windows_path.split('\\').filter(|s| !s.is_empty()).collect();
        windows_path = parts.join("\\");
    }
    
    windows_path
}

/// Translate a file path between operating systems
///
/// # Arguments
///
/// * `path` - The path to translate
/// * `from_os` - The source operating system
/// * `to_os` - The target operating system
///
/// # Returns
///
/// * `Ok(PathTranslation)` - The translated path
/// * `Err(PathError)` - Error if translation fails
///
/// # Example
///
/// ```
/// use cmdx::{translate_path, Os};
///
/// // Windows to Linux
/// let result = translate_path("C:\\Users\\john\\file.txt", Os::Windows, Os::Linux);
/// assert!(result.is_ok());
/// assert_eq!(result.unwrap().path, "/mnt/c/Users/john/file.txt");
///
/// // Linux to Windows
/// let result = translate_path("/mnt/c/Users/john/file.txt", Os::Linux, Os::Windows);
/// assert!(result.is_ok());
/// assert_eq!(result.unwrap().path, "C:\\Users\\john\\file.txt");
/// ```
pub fn translate_path(
    path: &str,
    from_os: Os,
    to_os: Os,
) -> Result<PathTranslation, PathError> {
    if path.trim().is_empty() {
        return Err(PathError::EmptyPath);
    }
    
    let path = path.trim();
    
    // Same OS - just return normalized path
    if from_os == to_os {
        return Ok(PathTranslation::new(
            path.to_string(),
            path.to_string(),
            from_os,
            to_os,
        ));
    }
    
    let mut result = PathTranslation::new(
        String::new(),
        path.to_string(),
        from_os,
        to_os,
    );
    
    // Determine translation direction based on OS types
    let translated = if from_os == Os::Windows && to_os.is_unix_like() {
        // Windows -> Unix
        windows_to_unix(path, &mut result)
    } else if from_os.is_unix_like() && to_os == Os::Windows {
        // Unix -> Windows
        unix_to_windows(path, &mut result)
    } else if from_os.is_unix_like() && to_os.is_unix_like() {
        // Unix -> Unix (just normalize)
        path.to_string()
    } else {
        // Fallback: try to auto-detect and convert
        if is_windows_path(path) {
            windows_to_unix(path, &mut result)
        } else {
            unix_to_windows(path, &mut result)
        }
    };
    
    result.path = translated;
    Ok(result)
}

/// Translate a path with string OS names
pub fn translate_path_str(
    path: &str,
    from_os: &str,
    to_os: &str,
) -> Result<PathTranslation, PathError> {
    let from = Os::parse(from_os)
        .ok_or_else(|| PathError::InvalidPath(format!("Unknown OS: {}", from_os)))?;
    let to = Os::parse(to_os)
        .ok_or_else(|| PathError::InvalidPath(format!("Unknown OS: {}", to_os)))?;
    
    translate_path(path, from, to)
}

/// Auto-detect the path format and translate to the target OS
///
/// # Example
///
/// ```
/// use cmdx::{translate_path_auto, Os};
///
/// // Auto-detects Windows path and converts to Linux
/// let result = translate_path_auto("C:\\Users\\john", Os::Linux);
/// assert!(result.is_ok());
/// ```
pub fn translate_path_auto(
    path: &str,
    to_os: Os,
) -> Result<PathTranslation, PathError> {
    if path.trim().is_empty() {
        return Err(PathError::EmptyPath);
    }
    
    // Auto-detect source OS
    let from_os = if is_windows_path(path) {
        Os::Windows
    } else {
        Os::Linux // Default to Linux for Unix-like paths
    };
    
    translate_path(path, from_os, to_os)
}

/// Batch translate multiple paths
pub fn translate_paths(
    paths: &[&str],
    from_os: Os,
    to_os: Os,
) -> Vec<Result<PathTranslation, PathError>> {
    paths
        .iter()
        .map(|path| translate_path(path, from_os, to_os))
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_is_windows_path() {
        assert!(is_windows_path("C:\\Users\\john"));
        assert!(is_windows_path("D:/Documents"));
        assert!(is_windows_path("\\\\server\\share"));
        assert!(is_windows_path("folder\\file.txt"));
        assert!(!is_windows_path("/home/john"));
        assert!(!is_windows_path("./file.txt"));
    }

    #[test]
    fn test_is_unix_path() {
        assert!(is_unix_path("/home/john"));
        assert!(is_unix_path("~/Documents"));
        assert!(is_unix_path("./file.txt"));
        assert!(is_unix_path("../parent/file"));
        assert!(!is_unix_path("C:\\Users"));
    }

    #[test]
    fn test_windows_to_linux_basic() {
        let result = translate_path("C:\\Users\\john\\file.txt", Os::Windows, Os::Linux);
        assert!(result.is_ok());
        let result = result.unwrap();
        assert_eq!(result.path, "/mnt/c/Users/john/file.txt");
        assert!(result.drive_translated);
    }

    #[test]
    fn test_windows_to_linux_drive_d() {
        let result = translate_path("D:\\Documents\\report.pdf", Os::Windows, Os::Linux);
        assert!(result.is_ok());
        let result = result.unwrap();
        assert_eq!(result.path, "/mnt/d/Documents/report.pdf");
    }

    #[test]
    fn test_linux_to_windows_mnt() {
        let result = translate_path("/mnt/c/Users/john/file.txt", Os::Linux, Os::Windows);
        assert!(result.is_ok());
        let result = result.unwrap();
        assert_eq!(result.path, "C:\\Users\\john\\file.txt");
        assert!(result.drive_translated);
    }

    #[test]
    fn test_linux_to_windows_home() {
        let result = translate_path("/home/john/Documents", Os::Linux, Os::Windows);
        assert!(result.is_ok());
        let result = result.unwrap();
        assert_eq!(result.path, "C:\\Users\\john\\Documents");
    }

    #[test]
    fn test_linux_to_windows_tilde() {
        let result = translate_path("~/Documents", Os::Linux, Os::Windows);
        assert!(result.is_ok());
        let result = result.unwrap();
        assert_eq!(result.path, "%USERPROFILE%\\Documents");
    }

    #[test]
    fn test_linux_to_windows_root() {
        let result = translate_path("/etc/config", Os::Linux, Os::Windows);
        assert!(result.is_ok());
        let result = result.unwrap();
        assert_eq!(result.path, "C:\\etc\\config");
    }

    #[test]
    fn test_unc_path_to_unix() {
        let result = translate_path("\\\\server\\share\\file.txt", Os::Windows, Os::Linux);
        assert!(result.is_ok());
        let result = result.unwrap();
        assert_eq!(result.path, "//server/share/file.txt");
    }

    #[test]
    fn test_network_path_to_windows() {
        let result = translate_path("//server/share/file.txt", Os::Linux, Os::Windows);
        assert!(result.is_ok());
        let result = result.unwrap();
        assert_eq!(result.path, "\\\\server\\share\\file.txt");
    }

    #[test]
    fn test_same_os_passthrough() {
        let result = translate_path("/home/john", Os::Linux, Os::Linux);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().path, "/home/john");
    }

    #[test]
    fn test_empty_path_error() {
        let result = translate_path("", Os::Windows, Os::Linux);
        assert!(result.is_err());
    }

    #[test]
    fn test_translate_path_str() {
        let result = translate_path_str("C:\\Users", "windows", "linux");
        assert!(result.is_ok());
        assert_eq!(result.unwrap().path, "/mnt/c/Users");
    }

    #[test]
    fn test_translate_path_auto() {
        let result = translate_path_auto("C:\\Users\\john", Os::Linux);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().path, "/mnt/c/Users/john");
    }

    #[test]
    fn test_translate_paths_batch() {
        let paths = vec!["C:\\Users", "D:\\Documents"];
        let results = translate_paths(&paths, Os::Windows, Os::Linux);
        assert_eq!(results.len(), 2);
        assert!(results.iter().all(|r| r.is_ok()));
    }

    #[test]
    fn test_unix_to_unix() {
        let result = translate_path("/home/john", Os::Linux, Os::MacOS);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().path, "/home/john");
    }

    #[test]
    fn test_macos_to_windows() {
        let result = translate_path("/Users/john/Documents", Os::MacOS, Os::Windows);
        assert!(result.is_ok());
        // macOS /Users maps to C:\Users on Windows
        assert!(result.unwrap().path.contains("Users"));
    }
}