Finish wslpath

This commit is contained in:
Jaby 2023-04-18 21:58:34 +02:00
parent 660f4fe150
commit f1fa227f58
3 changed files with 54 additions and 12 deletions

View File

@ -1,8 +1,9 @@
[package] [package]
name = "wslpath" name = "wslpath"
version = "0.1.0" version = "1.0.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
clap = {version = "*", features = ["derive"]}

View File

@ -1,14 +1,39 @@
pub fn add(left: usize, right: usize) -> usize { pub fn convert(path: String) -> String {
left + right replace_drive_letter(convert_slashes(path))
} }
#[cfg(test)] fn convert_slashes(path: String) -> String {
mod tests { path.replace('\\', "/")
use super::*; }
#[test] fn replace_drive_letter(mut path: String) -> String {
fn it_works() { let has_drive_letter = {
let result = add(2, 2); let drive_letter = path.get(0..2);
assert_eq!(result, 4);
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
} }

View File

@ -1,3 +1,19 @@
fn main() { use clap::Parser;
println!("Hello Planschi");
#[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() {
match CommandLine::try_parse() {
Ok(cmd_line) => {
print!("{}", wslpath::convert(cmd_line.path_string));
},
Err(error) => {
eprintln!("{}", error);
}
}
} }