Finish wslpath

This commit is contained in:
Jaby 2023-04-18 21:58:34 +02:00 committed by Jaby
parent 878297f138
commit 50605520b4
3 changed files with 54 additions and 12 deletions

View File

@ -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"]}

View File

@ -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
}

View File

@ -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);
}
}
}