From f1fa227f5871339b627d18796d8761f1cfeaade2 Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 18 Apr 2023 21:58:34 +0200 Subject: [PATCH] Finish wslpath --- src/Tools/wslpath/Cargo.toml | 3 ++- src/Tools/wslpath/src/lib.rs | 45 +++++++++++++++++++++++++++-------- src/Tools/wslpath/src/main.rs | 18 +++++++++++++- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/Tools/wslpath/Cargo.toml b/src/Tools/wslpath/Cargo.toml index 9448e724..38c5a00d 100644 --- a/src/Tools/wslpath/Cargo.toml +++ b/src/Tools/wslpath/Cargo.toml @@ -1,8 +1,9 @@ [package] name = "wslpath" -version = "0.1.0" +version = "1.0.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = {version = "*", features = ["derive"]} \ No newline at end of file diff --git a/src/Tools/wslpath/src/lib.rs b/src/Tools/wslpath/src/lib.rs index 7d12d9af..28b2a23e 100644 --- a/src/Tools/wslpath/src/lib.rs +++ b/src/Tools/wslpath/src/lib.rs @@ -1,14 +1,39 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right +pub fn convert(path: String) -> String { + replace_drive_letter(convert_slashes(path)) } -#[cfg(test)] -mod tests { - use super::*; +fn convert_slashes(path: String) -> String { + path.replace('\\', "/") +} - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); +fn replace_drive_letter(mut path: String) -> String { + let has_drive_letter = { + let drive_letter = path.get(0..2); + + if let Some(drive_letter) = drive_letter { + let starts_with_letter = drive_letter.chars().nth(0).unwrap_or('1').is_alphabetic(); + let has_seperator = drive_letter.chars().nth(1).unwrap_or('x') == ':'; + + starts_with_letter && has_seperator + } + + else { + false + } + }; + + if has_drive_letter { + path.replace_range(1..2, ""); // Removes : + if let Some(start_char) = path.get(0..1) { // Convert drive letter to lower case + path.replace_range(0..1, start_char.to_lowercase().as_str()); + } + + if path.len() == 3 { + path.push('/'); + } + + path.insert_str(0, "/mnt/"); } -} + + path +} \ No newline at end of file diff --git a/src/Tools/wslpath/src/main.rs b/src/Tools/wslpath/src/main.rs index 90550e0f..c9f05efe 100644 --- a/src/Tools/wslpath/src/main.rs +++ b/src/Tools/wslpath/src/main.rs @@ -1,3 +1,19 @@ +use clap::Parser; + +#[derive(Parser)] +#[clap(about = "A copy of the wslpath tool from wsl for faster execution", long_about = None)] +struct CommandLine { + #[clap(value_parser, help="The input path to convert to WSL")] + path_string: String +} + fn main() { - println!("Hello Planschi"); + match CommandLine::try_parse() { + Ok(cmd_line) => { + print!("{}", wslpath::convert(cmd_line.path_string)); + }, + Err(error) => { + eprintln!("{}", error); + } + } } \ No newline at end of file