Finish wslpath
This commit is contained in:
parent
878297f138
commit
50605520b4
|
@ -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"]}
|
|
@ -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
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue